From 9f4d67f055c9f2bf880eb152d62dfa7e68af65f7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 5 Nov 2026 02:38:13 +0000 Subject: [PATCH 1/4] Initial plan From c5206b7f71a22981cde15c3b6533ec0785d06095 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 5 Nov 2026 02:43:28 +0000 Subject: [PATCH 2/4] fix(http-client-python): preserve quoted ETags in generated samples Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com> --- .../codegen/serializers/sample_serializer.py | 4 -- .../tests/unit/test_string_literals.py | 47 +++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) 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", From 80b1fecf0866c13759f0d88611ece82b90a9fb8a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 5 Nov 2026 02:44:45 +0000 Subject: [PATCH 3/4] chore(http-client-python): add quoted ETag sample fix changelog Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com> --- ...pilot-fix-python-sample-serializer-2026-10-5-2-43-37.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .chronus/changes/copilot-fix-python-sample-serializer-2026-10-5-2-43-37.md 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 From 38092bc9668c158005115e1b329d5e2367fa4da3 Mon Sep 17 00:00:00 2001 From: msyyc <70930885+msyyc@users.noreply.github.com> Date: Mon, 30 Nov 2026 09:36:23 +0000 Subject: [PATCH 4/4] test(http-client-python): cover multiline sample content Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- .../tests/unit/test_string_literals.py | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) 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 f83844e50a5..2454db8a1f1 100644 --- a/packages/http-client-python/tests/unit/test_string_literals.py +++ b/packages/http-client-python/tests/unit/test_string_literals.py @@ -104,14 +104,24 @@ def test_sample_parameter_preserves_string_literal(code_model, value): assert ast.literal_eval(ast.parse(source).body[0].value) == value -@pytest.mark.parametrize("value", ['"1"', 'W/"1"', '"one", "two"']) +@pytest.mark.parametrize( + "wire_name, client_name, location, value", + [ + ("If-Match", "if_match", "header", '"1"'), + ("If-Match", "if_match", "header", 'W/"1"'), + ("If-Match", "if_match", "header", '"one", "two"'), + ("runbookContent", "runbook_content", "body", 'Write-Output "Hello"\r\nWrite-Output "world"\r\n'), + ], +) @pytest.mark.parametrize("positional", [False, True]) -def test_sample_preserves_required_if_match(code_model, env, value, positional): +def test_sample_preserves_required_string_parameter( + code_model, env, wire_name, client_name, location, value, positional +): parameter = Parameter( { - "wireName": "If-Match", - "clientName": "if_match", - "location": "header", + "wireName": wire_name, + "clientName": client_name, + "location": location, "implementation": "Method", "optional": False, }, @@ -129,14 +139,14 @@ def test_sample_preserves_required_if_match(code_model, env, value, positional): ) group = SimpleNamespace(client=code_model.clients[0], is_mixin=True) serializer = SampleSerializer( - code_model, env, group, operation, {"parameters": {"If-Match": value}}, "add_binding.py" + code_model, env, group, operation, {"parameters": {wire_name: 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 call.keywords[0].arg == client_name assert ast.literal_eval(call.keywords[0].value) == value