[python] Add mock API tests for XML scenarios from PR #10063 and fix XML serialization bugs (WIP)#10098
[python] Add mock API tests for XML scenarios from PR #10063 and fix XML serialization bugs (WIP)#10098
Conversation
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
commit: |
|
All changed packages have been documented.
Show changes
|
|
You can try these changes here
|
| 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") |
There was a problem hiding this comment.
@copilot Try to fix the bug so that we could remove all the skip of related test case of this PR.
There was a problem hiding this comment.
Fixed in 3bbc458. Four bugs were addressed in model_base.py.jinja2:
- Unwrapped model arrays —
_get_elementnow usesparent_meta.get("itemsName")when present, so<items>/<ModelItem>tags are emitted instead of the model's type name. - 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). - Namespace propagation — model namespace is only propagated to child-element lookups when the model uses a default namespace (no prefix), matching actual XML semantics.
- Python ET reserved prefix —
ET.register_namespacerejects prefixes matchingns\d+(e.g."ns2"); the fix falls back toET._namespace_mapviagetattrso 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>
Adds Python mock API test coverage for the new XML handling scenarios introduced in
packages/http-specsPR #10063, and fixes the underlying generator bugs that were blocking 4 of those tests.Dependency update
@typespec/http-specs0.1.0-alpha.35-dev.4→0.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.pyand its async counterpart — all 26 tests pass:ModelWithRenamedProperty—@nameon scalar fieldModelWithNestedModel— nested model propertyModelWithRenamedNestedModel— nested model whose type has@Xml.nameModelWithWrappedPrimitiveCustomItemNames— custom wrapper + item names on primitive arrayModelWithRenamedWrappedModelArray— custom wrapper name on model arrayModelWithRenamedWrappedAndItemModelArray— custom wrapper + item names on model arrayModelWithRenamedAttribute—@attributewith renamed XML nameModelWithUnwrappedModelArray— unwrapped model array with custom item nameModelWithRenamedUnwrappedModelArray— unwrapped model array with renamed item nameModelWithNamespace— XML namespace on modelModelWithNamespaceOnProperties— per-property XML namespacesGenerator bug fixes (
model_base.py.jinja2)Four bugs in the XML runtime template were fixed:
Unwrapped model array serialization:
_get_elementwas ignoringitemsNamefrom parent property metadata when serializing model items in array context, emitting<SimpleModel>instead of<items>. Fixed by usingparent_meta.get("itemsName")when present.Namespace key mismatch: DPG-generated models store namespace as
"namespace"key in_xmldicts, but the runtime only read"ns". Deserialization now reads both keys with correct prefix-aware propagation semantics.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).Python ET reserved prefix:
ET.register_namespace("ns2", ...)raisesValueErrorbecause Python reservesns\d+patterns. The fix wraps the call intry/exceptand falls back togetattr(ET, "_namespace_map")to honour the intended prefix.Original prompt
🔒 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.