Skip to content

[python] Fix XML deserialization for enum/datetime types and add mock API tests#9724

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/add-test-case-for-pull-9660
Draft

[python] Fix XML deserialization for enum/datetime types and add mock API tests#9724
Copilot wants to merge 3 commits intomainfrom
copilot/add-test-case-for-pull-9660

Conversation

Copy link
Contributor

Copilot AI commented Feb 14, 2026

PR #9660 added Spector test cases for XML models with enum and datetime properties (ModelWithEnum, ModelWithDatetime). This PR adds the corresponding Python SDK mock API tests and fixes XML deserialization bugs uncovered by them.

Bug fix: XML deserialization of enum and datetime values

_deserialize_with_callable in model_base.py.jinja2 failed to extract text from ET.Element for non-primitive types. Enum values hit the CaseInsensitiveEnumMeta path which passed the raw Element to the enum constructor, caught the ValueError, and returned the Element as the "unknown value". DateTime values similarly passed raw Elements to _deserialize_datetime which expects strings.

# Before: raw Element passed to enum/datetime deserializers
if isinstance(deserializer, CaseInsensitiveEnumMeta):
    try:
        return deserializer(value)        # value is ET.Element → ValueError
    except ValueError:
        return value                       # returns raw Element

# After: extract text for Element values
if isinstance(deserializer, CaseInsensitiveEnumMeta):
    try:
        return deserializer(value.text if isinstance(value, ET.Element) else value)
    except ValueError:
        return value.text if isinstance(value, ET.Element) else value

Also added early-return paths for _DESERIALIZE_MAPPING and _DESERIALIZE_MAPPING_WITHFORMAT functions (datetime, duration, bytes, etc.) within the ET.Element block.

Tests

  • Added test_model_with_enum — GET/PUT round-trip for ModelWithEnum with extensible union status field
  • Added test_model_with_datetime — GET validation for ModelWithDatetime with rfc3339/rfc7231 encoded fields (PUT omitted due to pre-existing millisecond precision mismatch in isoformat() serialization)
  • Both sync and async variants in generic_mock_api_tests/

Dependency

  • Bumped @typespec/http-specs to 0.1.0-alpha.33-dev.2 to pick up the new XML enum/datetime Spector cases

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…n for enum/datetime types

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 Feb 14, 2026
Copilot AI changed the title [WIP] Add test case for Typespec pull request 9660 Fix XML deserialization for enum/datetime types and add mock API tests Feb 14, 2026
Copilot AI requested a review from msyyc February 14, 2026 06:09
@msyyc msyyc changed the title Fix XML deserialization for enum/datetime types and add mock API tests [python] Fix XML deserialization for enum/datetime types and add mock API tests Feb 14, 2026
@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 14, 2026

Open in StackBlitz

npm i https://pkg.pr.new/microsoft/typespec/@typespec/http@9724
npm i https://pkg.pr.new/microsoft/typespec/@typespec/http-client@9724

commit: 0b121c0

@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 tests for XML model with enum and datetime properties, and fix XML deserialization for enum and datetime types

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.

2 participants