Skip to content

Use content-type aware deserialization for error responses#3754

Merged
joheredi merged 4 commits intoAzure:mainfrom
joheredi:joheredi/error-xml-deserialization
Feb 12, 2026
Merged

Use content-type aware deserialization for error responses#3754
joheredi merged 4 commits intoAzure:mainfrom
joheredi:joheredi/error-xml-deserialization

Conversation

@joheredi
Copy link
Copy Markdown
Member

Fix: [Copilot PR] Use content-type aware deserialization for error responses

Problem

When an operation returns an XML error response (e.g., Azure Blob Storage's StorageError), the generated deserialize function always used the JSON deserializer (storageErrorDeserializer(result.body)), ignoring the response's content type. This caused deserialization failures for services that return XML error bodies.

Solution

Updated the error deserialization code generation in operationHelpers.ts to detect whether exception types support XML serialization and generate the appropriate deserializer:

  • XML-only exceptions: Directly uses the XML deserializer (e.g., storageErrorXmlDeserializer(result.body))
  • Dual-format exceptions (both XML and JSON): Generates a runtime content-type check using isXmlContentType to select the correct deserializer
  • JSON-only exceptions (default): Behavior unchanged — uses the JSON deserializer

Example generated output

Before:

const error = createRestError(result);
error.details = storageErrorDeserializer(result.body);
throw error;

After (dual-format):

const error = createRestError(result);
const responseContentType = result.headers?.["content-type"] ?? "";
error.details = isXmlContentType(responseContentType)
  ? storageErrorXmlDeserializer(result.body)
  : storageErrorDeserializer(result.body);
throw error;

Changes

  • operationHelpers.ts
    • Added xmlDeserializer and isXmlOnly fields to ExceptionThrowDetail
    • Added defaultXmlDeserializer and defaultIsXmlOnly fields to OperationExceptionDetails
    • Updated getExceptionDetails to check exception contentTypes and model XML serialization metadata, building XML deserializers when applicable
    • Added getExceptionDeserializeExpr helper to generate the correct deserialization expression based on format support
    • Updated getExceptionThrowStatement to emit content-type–aware deserialization for exceptions with XML support

@timovv
Copy link
Copy Markdown
Member

timovv commented Feb 12, 2026

Seems ok in principle. The code seems to be combined with the accept header PR work, could you separate it out?

@timovv
Copy link
Copy Markdown
Member

timovv commented Feb 12, 2026

Ah or I can just review commit by commit

Copy link
Copy Markdown
Member

@timovv timovv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test case? would be good to see what each of these cases actually look like

@joheredi joheredi force-pushed the joheredi/error-xml-deserialization branch from 583a581 to f745a12 Compare February 12, 2026 19:52
Copy link
Copy Markdown
Member

@timovv timovv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@joheredi joheredi merged commit e5a0af2 into Azure:main Feb 12, 2026
16 checks passed
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.

2 participants