diff --git a/.chronus/changes/fix-bug-header-2024-6-5-13-41-56.md b/.chronus/changes/fix-bug-header-2024-6-5-13-41-56.md new file mode 100644 index 00000000000..9e98e4dc876 --- /dev/null +++ b/.chronus/changes/fix-bug-header-2024-6-5-13-41-56.md @@ -0,0 +1,8 @@ +--- +changeKind: fix +packages: + - "@autorest/python" + - "@azure-tools/typespec-python" +--- + +Fix for construct headers and queries when build request \ No newline at end of file diff --git a/packages/autorest.python/test/vanilla/legacy/Expected/AcceptanceTests/BodyString/bodystring/operations/_enum_operations.py b/packages/autorest.python/test/vanilla/legacy/Expected/AcceptanceTests/BodyString/bodystring/operations/_enum_operations.py index bdc396f950d..5d167786cd5 100644 --- a/packages/autorest.python/test/vanilla/legacy/Expected/AcceptanceTests/BodyString/bodystring/operations/_enum_operations.py +++ b/packages/autorest.python/test/vanilla/legacy/Expected/AcceptanceTests/BodyString/bodystring/operations/_enum_operations.py @@ -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") diff --git a/packages/typespec-python/generator/pygen/codegen/serializers/builder_serializer.py b/packages/typespec-python/generator/pygen/codegen/serializers/builder_serializer.py index e65439ea156..c22eef08a88 100644 --- a/packages/typespec-python/generator/pygen/codegen/serializers/builder_serializer.py +++ b/packages/typespec-python/generator/pygen/codegen/serializers/builder_serializer.py @@ -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: diff --git a/packages/typespec-python/generator/pygen/codegen/serializers/parameter_serializer.py b/packages/typespec-python/generator/pygen/codegen/serializers/parameter_serializer.py index 8a41c850a7d..bad98faa5be 100644 --- a/packages/typespec-python/generator/pygen/codegen/serializers/parameter_serializer.py +++ b/packages/typespec-python/generator/pygen/codegen/serializers/parameter_serializer.py @@ -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 = [ diff --git a/packages/typespec-python/generator/pygen/codegen/templates/request_builder.py.jinja2 b/packages/typespec-python/generator/pygen/codegen/templates/request_builder.py.jinja2 index f4810000e7b..ed9af24814d 100644 --- a/packages/typespec-python/generator/pygen/codegen/templates/request_builder.py.jinja2 +++ b/packages/typespec-python/generator/pygen/codegen/templates/request_builder.py.jinja2 @@ -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) }}