diff --git a/.chronus/changes/copilot-fix-python-sample-serializer-2026-10-5-2-43-37.md b/.chronus/changes/copilot-fix-python-sample-serializer-2026-10-5-2-43-37.md new file mode 100644 index 00000000000..2bb6577803b --- /dev/null +++ b/.chronus/changes/copilot-fix-python-sample-serializer-2026-10-5-2-43-37.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/http-client-python" +--- + +Fix invalid string literals in generated Python samples for quoted If-Match ETags, preserving the exact header value. \ No newline at end of file diff --git a/packages/http-client-python/generator/pygen/codegen/serializers/sample_serializer.py b/packages/http-client-python/generator/pygen/codegen/serializers/sample_serializer.py index 733209b8396..175449968b6 100644 --- a/packages/http-client-python/generator/pygen/codegen/serializers/sample_serializer.py +++ b/packages/http-client-python/generator/pygen/codegen/serializers/sample_serializer.py @@ -104,10 +104,6 @@ def _client_params(self) -> dict[str, Any]: @staticmethod def handle_param(param: Union[Parameter, BodyParameter], param_value: Any) -> str: - if isinstance(param_value, str): - if any(i in param_value for i in '\r\n"'): - return f'"""{param_value}"""' - return param.type.serialize_sample_value(param_value) # prepare operation parameters diff --git a/packages/http-client-python/tests/unit/test_string_literals.py b/packages/http-client-python/tests/unit/test_string_literals.py index 7d0dc9c3188..b7aa8afce67 100644 --- a/packages/http-client-python/tests/unit/test_string_literals.py +++ b/packages/http-client-python/tests/unit/test_string_literals.py @@ -11,6 +11,7 @@ from typing import Literal, Optional, get_args import pytest +import black from jinja2 import Environment, PackageLoader from pygen import OptionsDict @@ -31,6 +32,7 @@ from pygen.codegen.serializers.builder_serializer import OperationSerializer, _get_polymorphic_subtype_template from pygen.codegen.serializers.model_serializer import DpgModelSerializer from pygen.codegen.serializers.general_serializer import GeneralSerializer +from pygen.codegen.serializers.sample_serializer import SampleSerializer from pygen.codegen.serializers.types_serializer import TypesSerializer from pygen.codegen.serializers.utils import json_dumps_template @@ -92,6 +94,51 @@ def env(): return Environment(loader=PackageLoader("pygen.codegen", "templates"), trim_blocks=True, lstrip_blocks=True) +@pytest.mark.parametrize("value", VALUES + ['"1"', 'W/"1"', '"one", "two"', "trailing\\"]) +def test_sample_parameter_preserves_string_literal(code_model, value): + parameter = SimpleNamespace(type=StringType({"type": "string"}, code_model)) + literal = SampleSerializer.handle_param(parameter, value) + assert ast.literal_eval(literal) == value + source = black.format_str(f"if_match = {literal}\n", mode=black.Mode(line_length=120)) + assert ast.literal_eval(ast.parse(source).body[0].value) == value + + +@pytest.mark.parametrize("value", ['"1"', 'W/"1"', '"one", "two"']) +@pytest.mark.parametrize("positional", [False, True]) +def test_sample_preserves_required_if_match(code_model, env, value, positional): + parameter = Parameter( + { + "wireName": "If-Match", + "clientName": "if_match", + "location": "header", + "implementation": "Method", + "optional": False, + }, + code_model, + type=StringType({"type": "string"}, code_model), + ) + operation = SimpleNamespace( + name="add_binding", + operation_type="operation", + parameters=SimpleNamespace( + positional=[parameter] if positional else [], + keyword_only=[] if positional else [parameter], + ), + response_type_annotation=lambda **kwargs: "None", + ) + group = SimpleNamespace(client=code_model.clients[0], is_mixin=True) + serializer = SampleSerializer( + code_model, env, group, operation, {"parameters": {"If-Match": value}}, "add_binding.py" + ) + source = black.format_str(serializer.serialize(), mode=black.Mode(line_length=120)) + function = next(node for node in ast.parse(source).body if isinstance(node, ast.FunctionDef)) + call = function.body[1].value + assert call.func.attr == "add_binding" + assert len(call.keywords) == 1 + assert call.keywords[0].arg == "if_match" + assert ast.literal_eval(call.keywords[0].value) == value + + @pytest.mark.parametrize("async_mode", [False, True]) @pytest.mark.parametrize( "description, expected",