Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle required fields properly in query_params #1068

Merged
merged 2 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,31 @@ class {{service.name}}RestTransport({{service.name}}Transport):
"""

http_options = [
{%- for rule in method.http_options %}{
'method': '{{ rule.method }}',
'uri': '{{ rule.uri }}',
{%- if rule.body %}
'body': '{{ rule.body }}',
{%- endif %}
},
{%- endfor %}]
{% for rule in method.http_options %}
{
'method': '{{ rule.method }}',
'uri': '{{ rule.uri }}',
{% if rule.body %}
'body': '{{ rule.body }}',
{% endif %}
},
{% endfor %}
]

{% if method.input.required_fields %}
required_fields = [
# (snake_case_name, camel_case_name)
{% for req_field in method.input.required_fields %}
{% if req_field.is_primitive %}
(
"{{ req_field.name | snake_case }}",
"{{ req_field.name | camel_case }}"
),
{% endif %}{# is primitive #}
{% endfor %}{# required fields #}
]

{% endif %}

request_kwargs = {{method.input.ident}}.to_dict(request)
transcoded_request = path_template.transcode(
Expand Down Expand Up @@ -229,6 +246,18 @@ class {{service.name}}RestTransport({{service.name}}Transport):
use_integers_for_enums=False
))

{% if method.input.required_fields %}
# Ensure required fields have values in query_params.
# If a required field has a default value, it can get lost
# by the to_json call above.
orig_query_params = transcoded_request["query_params"]
for snake_case_name, camel_case_name in required_fields:
if snake_case_name in orig_query_params:
if camel_case_name not in query_params:
query_params[camel_case_name] = orig_query_params[snake_case_name]

{% endif %}

# Send the request
headers = dict(metadata)
headers['Content-Type'] = 'application/json'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ def test_{{ method.name|snake_case }}_rest_flattened(transport: str = 'rest'):
assert len(req.mock_calls) == 1
_, args, _ = req.mock_calls[0]
{% with uri = method.http_options[0].uri %}
assert path_template.validate("{{ uri }}", args[1])
assert path_template.validate("https://{{ service.host }}{{ uri }}", args[1])
{% endwith %}
{# TODO(kbandes) - reverse-transcode request args to check all request fields #}

Expand Down