Skip to content

[python] Add mock API tests for XML scenarios from PR #10063 and fix XML serialization bugs (WIP)#10098

Open
Copilot wants to merge 5 commits intomainfrom
copilot/add-test-case-typespec-10063
Open

[python] Add mock API tests for XML scenarios from PR #10063 and fix XML serialization bugs (WIP)#10098
Copilot wants to merge 5 commits intomainfrom
copilot/add-test-case-typespec-10063

Conversation

Copy link
Contributor

Copilot AI commented Mar 19, 2026

Adds Python mock API test coverage for the new XML handling scenarios introduced in packages/http-specs PR #10063, and fixes the underlying generator bugs that were blocking 4 of those tests.

Dependency update

  • Bumps @typespec/http-specs 0.1.0-alpha.35-dev.40.1.0-alpha.35-dev.5 (first release containing the new scenarios)

New test cases (sync + async)

11 new scenarios added to generic_mock_api_tests/test_payload_xml.py and its async counterpart — all 26 tests pass:

Scenario Status
ModelWithRenamedProperty@name on scalar field ✅ passes
ModelWithNestedModel — nested model property ✅ passes
ModelWithRenamedNestedModel — nested model whose type has @Xml.name ✅ passes
ModelWithWrappedPrimitiveCustomItemNames — custom wrapper + item names on primitive array ✅ passes
ModelWithRenamedWrappedModelArray — custom wrapper name on model array ✅ passes
ModelWithRenamedWrappedAndItemModelArray — custom wrapper + item names on model array ✅ passes
ModelWithRenamedAttribute@attribute with renamed XML name ✅ passes
ModelWithUnwrappedModelArray — unwrapped model array with custom item name ✅ passes
ModelWithRenamedUnwrappedModelArray — unwrapped model array with renamed item name ✅ passes
ModelWithNamespace — XML namespace on model ✅ passes
ModelWithNamespaceOnProperties — per-property XML namespaces ✅ passes

Generator bug fixes (model_base.py.jinja2)

Four bugs in the XML runtime template were fixed:

  1. Unwrapped model array serialization: _get_element was ignoring itemsName from parent property metadata when serializing model items in array context, emitting <SimpleModel> instead of <items>. Fixed by using parent_meta.get("itemsName") when present.

  2. Namespace key mismatch: DPG-generated models store namespace as "namespace" key in _xml dicts, but the runtime only read "ns". Deserialization now reads both keys with correct prefix-aware propagation semantics.

  3. Default vs prefixed namespace propagation: XML default namespace (xmlns="...") is inherited by child elements; prefixed namespace (xmlns:smp="...") is not. Deserialization now only propagates the model namespace to property lookups when the model has no prefix (default namespace).

  4. Python ET reserved prefix: ET.register_namespace("ns2", ...) raises ValueError because Python reserves ns\d+ patterns. The fix wraps the call in try/except and falls back to getattr(ET, "_namespace_map") to honour the intended prefix.

Original prompt

This section details on the original issue you should resolve

<issue_title>[python] add test case for #10063
</issue_title>
<issue_description>follow skill https://github.com/microsoft/typespec/blob/main/.github/skills/python-sdk-spector-mock-api-tests/SKILL.md to write test case for #10063
</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI and others added 2 commits March 19, 2026 22:48
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:python Issue for the Python client emitter: @typespec/http-client-python label Mar 19, 2026
Copilot AI changed the title [WIP] Add test case for typespec pull request 10063 [python] Add mock API tests for XML scenarios from PR #10063 Mar 19, 2026
Copilot AI requested a review from msyyc March 19, 2026 22:53
@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 20, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-python@10098

commit: 9540ee7

@github-actions
Copy link
Contributor

All changed packages have been documented.

  • @typespec/http-client-python
Show changes

@typespec/http-client-python - internal ✏️

Add mock API test cases for XML scenarios introduced in #10063, covering: renamed property, nested model, renamed nested model, wrapped primitive with custom item names, model array variants (wrapped/unwrapped/renamed), renamed attribute, namespace, and namespace-on-properties. Tests for unwrapped model array serialization and namespace handling are skipped pending generator bug fixes.

@azure-sdk
Copy link
Collaborator

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

client.model_with_array_of_model_value.put(model)


@pytest.mark.skip(reason="Generator bug: unwrapped model arrays use type name instead of itemsName for serialization")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Try to fix the bug so that we could remove all the skip of related test case of this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 3bbc458. Four bugs were addressed in model_base.py.jinja2:

  1. Unwrapped model arrays_get_element now uses parent_meta.get("itemsName") when present, so <items> / <ModelItem> tags are emitted instead of the model's type name.
  2. Namespace key mismatch — DPG-generated models use "namespace" key; deserialization now reads both "ns" and "namespace" (with prefix-aware propagation so default-namespace models still work).
  3. Namespace propagation — model namespace is only propagated to child-element lookups when the model uses a default namespace (no prefix), matching actual XML semantics.
  4. Python ET reserved prefixET.register_namespace rejects prefixes matching ns\d+ (e.g. "ns2"); the fix falls back to ET._namespace_map via getattr so the intended prefix is used when serialising.

All 26 mock API tests and 35 XML unit tests now pass with no skips.

…eserved prefix

Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Copilot AI changed the title [python] Add mock API tests for XML scenarios from PR #10063 [python] Add mock API tests for XML scenarios from PR #10063 and fix XML serialization bugs Mar 20, 2026
Copilot AI requested a review from msyyc March 20, 2026 02:54
@msyyc msyyc changed the title [python] Add mock API tests for XML scenarios from PR #10063 and fix XML serialization bugs [python] Add mock API tests for XML scenarios from PR #10063 and fix XML serialization bugs (WIP) Mar 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:python Issue for the Python client emitter: @typespec/http-client-python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[python] add test case for https://github.com/microsoft/typespec/pull/10063

3 participants