Skip to content

Always Initialize Non-Optional Unwrapped Items in XML Deserialization #9967

@jorgerangel-msft

Description

@jorgerangel-msft

Consider this tsp:

@Xml.name("EnumerationResults")
model ListBlobsResponse {
  /** The service endpoint. */
  @Xml.attribute
  @Xml.name("ServiceEndpoint")
  serviceEndpoint: string;

  /** The container name. */
  @Xml.attribute
  @Xml.name("ContainerName")
  containerName: string;

  /** The prefix of the blobs. */
  @Xml.name("Prefix") prefix?: string;

  /** The marker of the blobs. */
  @Xml.name("Marker") marker?: string;

  /** The max results of the blobs. */
  @Xml.name("MaxResults") maxResults?: int32;

  /** The blob segment. */
  @Xml.name("Blobs")
  segment: BlobFlatListSegment;

  /** The next marker of the blobs. */
  @continuationToken
  @Xml.name("NextMarker")
  nextMarker?: string;
}

/** The blob flat list segment. */
model BlobFlatListSegment {
  /** The blob items. */
  @pageItems
  @Xml.name("Blob")
  @Xml.unwrapped
  blobItems: BlobItem[];
}

The service can return <Blobs /> and the current XML deserialization code will deserialize segment into null since there are no Blob elements in Blobs. Since blobItems is unwrapped and non-nullable, we should be initializing it to an empty list by default.

We should update the xml deserialization to initialize collection properties to an empty collection when they are non-nullable, required, and unwrapped.

Metadata

Metadata

Labels

emitter:client:csharpIssue for the C# client emitter: @typespec/http-client-csharp

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions