Skip to content

Comments

Fix XML serialization/deserialization of additionalProperties#3781

Open
joheredi wants to merge 2 commits intoAzure:mainfrom
joheredi:fix/xml-additional-properties-3779
Open

Fix XML serialization/deserialization of additionalProperties#3781
joheredi wants to merge 2 commits intoAzure:mainfrom
joheredi:fix/xml-additional-properties-3779

Conversation

@joheredi
Copy link
Member

Fixes #3779

Problem

Models with additionalProperties (e.g., BlobMetadata which uses ...Record<string>) were not correctly handled in XML serialization/deserialization:

  • XML deserializers ignored undeclared XML elements entirely, returning empty objects. For example, <Metadata><a>c</a></Metadata> would deserialize to {} instead of { additionalProperties: { a: "c" } }.
  • XML object serializer incorrectly spread the entire client model (...item) instead of just the additionalProperties Record entries, producing malformed XML output.
  • XML string serializer silently dropped all additional properties since they weren't in the metadata array.

The JSON deserializer handled this correctly via serializeRecord(), but the XML path had no equivalent logic.

Solution

Introduced XmlAdditionalPropertiesConfig — a metadata-driven approach consistent with the existing XML serialization patterns (arrays, dicts, dates, etc.):

interface XmlAdditionalPropertiesConfig {
  propertyName: string;      // e.g., "additionalProperties"
  excludeNames: string[];    // XML names of declared properties to skip
  deserializer?: (value: any) => any;
}

Changes

Runtime helpers (static/static-helpers/serialization/xml-helpers.ts):

  • Added XmlAdditionalPropertiesConfig interface
  • Extended deserializeXmlObject — after processing declared properties, collects remaining XML elements (excluding attributes, #text, and declared names) into a Record
  • Extended serializeModelToXml — reads item[config.propertyName] and emits entries as sibling XML elements
  • Threaded the config through deserializeFromXml, deserializeXmlToModel, and serializeToXml

Code generator (src/modular/serialization/buildXmlSerializerFunction.ts):

  • Added buildAdditionalPropertiesConfigExpr() that checks getAdditionalPropertiesType(type) and generates the config object with correct property name and exclude list
  • Updated buildXmlModelDeserializer and buildXmlObjectModelDeserializer to pass config to deserialize calls
  • Updated buildXmlModelSerializer to pass config to serializeToXml
  • Fixed buildXmlObjectModelSerializer to spread item["additionalProperties"] instead of ...item

Tests:

  • Added scenario test xmlAdditionalProperties.md with 5 test cases covering both serialization and deserialization
  • Added 7 unit tests in xml-helpers.test.ts including a full round-trip test

Models with additionalProperties (Record spread) were not correctly
handled in XML serialization and deserialization:

- XML deserializers ignored undeclared XML elements, returning empty
  objects for models like BlobMetadata
- XML object serializer incorrectly spread the entire client model
  instead of just the additionalProperties Record entries
- XML string serializer silently dropped all additional properties

Changes:
- Add XmlAdditionalPropertiesConfig interface to xml-helpers.ts
- Extend deserializeXmlObject/deserializeFromXml to collect undeclared
  XML elements into the additionalProperties field
- Extend serializeModelToXml/serializeToXml to emit additionalProperties
  entries as sibling XML elements
- Fix buildXmlObjectModelSerializer to spread item["additionalProperties"]
  instead of ...item
- Update buildXmlModelSerializer/buildXmlModelDeserializer/
  buildXmlObjectModelDeserializer generators to pass config when model
  has additional properties
- Register XmlAdditionalPropertiesConfig in static-helpers-metadata.ts

Fixes Azure#3779

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XML: Container.listBlobsFlat BlobMetadata isn't deserialized

1 participant