[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
|
packages/http-client-python/generator/test/generic_mock_api_tests/test_payload_xml.py
Outdated
Show resolved
Hide resolved
…eserved prefix Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds Python mock API coverage for new @typespec/http-specs XML scenarios and updates the Python generator’s XML runtime template to fix serialization/deserialization issues needed by those scenarios.
Changes:
- Bump
@typespec/http-specsto0.1.0-alpha.35-dev.5to pick up new XML scenarios. - Add new sync + async mock API tests covering renamed properties/attributes, nested models, array wrapping/unwrapping, and namespaces.
- Fix XML runtime template behaviors around namespace handling and element naming for unwrapped model arrays.
Reviewed changes
Copilot reviewed 5 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/http-client-python/package.json | Bumps @typespec/http-specs dependency version. |
| packages/http-client-python/package-lock.json | Locks updated @typespec/http-specs tarball + integrity. |
| packages/http-client-python/generator/test/generic_mock_api_tests/test_payload_xml.py | Adds new sync mock API XML scenarios. |
| packages/http-client-python/generator/test/generic_mock_api_tests/asynctests/test_payload_xml_async.py | Adds async equivalents of new XML scenarios. |
| packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 | Fixes XML namespace reading and array-item element naming; adds safe namespace registration fallback in element creation. |
| .gitignore | Ignores .venv_test. |
| .chronus/changes/python-xml-test-cases-2026-3-19-22-47-48.md | Adds changelog entry for the internal change. |
Files not reviewed (1)
- packages/http-client-python/package-lock.json: Language not supported
packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2
Outdated
Show resolved
Hide resolved
…model_base.py.jinja2 Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
… generator fixes Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com> Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/8a7540f8-aabf-47ac-9847-98e810038b5e
msyyc
left a comment
There was a problem hiding this comment.
Code Review
Overall the tests are well-structured and consistent with existing patterns. The 4 bug fixes in model_base.py.jinja2 are well-reasoned. A few issues worth addressing:
🔴 Bug: itemNs vs itemsNs key mismatch (deserialization ↔ serialization)
File: model_base.py.jinja2 — deserialization path (~line 648) vs serialization path (~line 1330)
Serialization consistently uses plural itemsNs/itemsName/itemsPrefix, but the existing deserialization code reads itemNs (singular). This PR didn't introduce the bug but touches the surrounding code and would be a good opportunity to fix it. If a model declares itemsNs metadata, deserialization will silently ignore it.
🟡 Empty-string namespace treated as falsy in or chains
Multiple new lines use patterns like:
xml_ns = prop_meta.get("ns") or prop_meta.get("namespace")
parent_meta.get("itemsNs") or parent_meta.get("ns") or parent_meta.get("namespace")If a key is explicitly set to "" (valid in XML — means "no namespace"), the or chain falls through to the next value instead of honoring the empty string. Safer pattern:
xml_ns = prop_meta.get("ns")
if xml_ns is None:
xml_ns = prop_meta.get("namespace")🟡 Private API ET._namespace_map in _create_xml_element
The try/except ValueError fallback uses getattr(ET, "_namespace_map", None) — a private CPython implementation detail. This works today but could break in future Python versions. If _namespace_map is absent, the prefix silently becomes auto-generated (ns0, ns1…) with no warning. Consider adding a log or comment about the silent fallback behavior.
Minor
- Changelog entry still says "skipped pending generator bug fixes" even though the bugs were subsequently fixed and all skips removed. Should be updated to reflect the current state.
.gitignoreaddition of.venv_test— is this intentional or a leftover from local development?
|
@copilot analyze the review comment #10098 (review). |
…cks, improve namespace_map comment, remove .venv_test from .gitignore Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com> Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/0b94f8d4-fb48-435f-a266-158971884321
Addressed in e02d171:
All four points from the review have been addressed in e02d171. See the reply on the review comment for details. |
itemNs→itemsNskey mismatch in deserialization (red bug)orchains for namespace lookups withis Nonechecks (handles empty-string namespaces correctly)_get_element/_get_wrapped_elementET._namespace_mapsilent fallback behavior.venv_testfrom.gitignore(local dev leftover)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.