Skip to content

Conversation

@joheredi
Copy link
Member

Summary

This PR adds full XML serialization and deserialization support for the TypeSpec TypeScript emitter, enabling generated clients to handle XML request and response bodies.

Changes

New Static Helpers

  • xml-helpers.ts - Runtime XML serialization/deserialization utilities using fast-xml-parser
    • serializeToXml() - Converts TypeScript models to XML strings
    • deserializeXml() - Parses XML responses into TypeScript models
    • Support for XML attributes, namespaces, wrapped/unwrapped arrays, dictionaries, and text content
    • Proper whitespace preservation in text nodes

Code Generation

  • buildXmlSerializerFunction.ts - New serializer generator for XML operations
  • operationHelpers.ts - Extended to detect XML content types and generate appropriate serialization calls
  • emitModels.ts - Updated to emit XML serialization metadata
  • buildOperations.ts - Updated operation generation for XML handling

Dependencies

  • Added fast-xml-parser dependency for XML parsing/building

Test Coverage

  • 24 integration tests covering all Payload.Xml spector test scenarios from the spec:

    • SimpleModel (GET/PUT)
    • ModelWithSimpleArrays (GET/PUT)
    • ModelWithArrayOfModel (GET/PUT)
    • ModelWithOptionalField (GET/PUT)
    • ModelWithAttributes (GET/PUT)
    • ModelWithUnwrappedArray (GET/PUT)
    • ModelWithRenamedArrays (GET/PUT)
    • ModelWithRenamedFields (GET/PUT)
    • ModelWithEmptyArray (GET/PUT)
    • ModelWithText (GET/PUT)
    • ModelWithDictionary (GET/PUT)
    • ModelWithEncodedNames (GET/PUT)
  • 54 unit tests for the XML helper functions covering:

    • Primitive serialization (strings, numbers, booleans, dates, bytes)
    • Array serialization (wrapped and unwrapped)
    • Attribute handling
    • Namespace support
    • Dictionary serialization/deserialization
    • Content type detection
    • Round-trip serialization
    • Edge cases (empty arrays, null values, special characters)

XML Features Supported

Feature Serialization Deserialization
Simple models
Nested models
Wrapped arrays
Unwrapped arrays
XML attributes
XML namespaces
Dictionary types
Text content
Renamed elements
Date encoding (RFC3339, RFC7231, Unix)
Base64 bytes

Breaking Changes

None

@jeremymeng
Copy link
Member

jeremymeng commented Jan 21, 2026

note: storage needs to configure the xml char key because the default one is _ which is an allowed character in storage identifiers metadata. https://github.com/Azure/azure-sdk-for-js/blob/3c50f9567bd325c7b3264ac02442685849f2bbfc/sdk/storage/storage-blob/src/Pipeline.ts#L296

@joheredi
Copy link
Member Author

note: storage needs to configure the xml char key because the default one is _ which is an allowed character in storage identifiers metadata. https://github.com/Azure/azure-sdk-for-js/blob/3c50f9567bd325c7b3264ac02442685849f2bbfc/sdk/storage/storage-blob/src/Pipeline.ts#L296

fast-xml-parser seems to use #text so it wouldn't collide with storage. I added test cases for this to validate this.

@joheredi joheredi merged commit 0f3d27d into Azure:main Jan 21, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants