Skip to content

Commit

Permalink
Fix for construct headers and queries when build request (#2681)
Browse files Browse the repository at this point in the history
  • Loading branch information
msyyc committed Jul 8, 2024
1 parent 79d7617 commit 507d8ca
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
8 changes: 8 additions & 0 deletions .chronus/changes/fix-bug-header-2024-6-5-13-41-56.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
changeKind: fix
packages:
- "@autorest/python"
- "@azure-tools/typespec-python"
---

Fix for construct headers and queries when build request
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ def build_put_referenced_constant_request(**kwargs: Any) -> HttpRequest:

content_type: Optional[str] = kwargs.pop("content_type", _headers.pop("Content-Type", None))
accept = _headers.pop("Accept", "application/json")
color_constant = "green-color"

# Construct URL
_url = kwargs.pop("template_url", "/string/enum/ReferencedConstant")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,14 +410,19 @@ def _json_response_template_name(self) -> str:
return "response.json()"

@staticmethod
def declare_non_inputtable_constants(builder: RequestBuilderType) -> List[str]:
def declare_non_inputtable_headers_queries(builder: RequestBuilderType) -> List[str]:
def _get_value(param):
declaration = param.get_declaration() if param.constant else None
if param.location in [ParameterLocation.HEADER, ParameterLocation.QUERY]:
kwarg_dict = "headers" if param.location == ParameterLocation.HEADER else "params"
return f"_{kwarg_dict}.pop('{param.wire_name}', {param.get_declaration()})"
return f"{param.get_declaration()}"
return f"_{kwarg_dict}.pop('{param.wire_name}', {declaration})"
return declaration

return [f"{p.client_name} = {_get_value(p)}" for p in builder.parameters.constant if not p.in_method_signature]
return [
f"{p.client_name} = {_get_value(p)}"
for p in (builder.parameters.headers + builder.parameters.query)
if not p.in_method_signature
]

@property
def _function_def(self) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def serialize_query_header(
param.wire_name,
ParameterSerializer.serialize_parameter(param, serializer_name),
)
if not param.optional:
if not param.optional and (param.in_method_signature or param.constant):
retval = [set_parameter]
else:
retval = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
{% if request_builder_serializer.pop_kwargs_from_signature(request_builder) %}
{{ op_tools.serialize(request_builder_serializer.pop_kwargs_from_signature(request_builder)) | indent }}
{%- endif -%}
{% if request_builder_serializer.declare_non_inputtable_constants(request_builder) %}
{{ op_tools.serialize(request_builder_serializer.declare_non_inputtable_constants(request_builder)) | indent }}
{% if request_builder_serializer.declare_non_inputtable_headers_queries(request_builder) %}
{{ op_tools.serialize(request_builder_serializer.declare_non_inputtable_headers_queries(request_builder)) | indent }}
{% endif %}
# Construct URL
{{ request_builder_serializer.construct_url(request_builder) }}
Expand Down

0 comments on commit 507d8ca

Please sign in to comment.