Skip to content

fix(csharp): only generate XML serialization helpers when a model has XML usage#11341

Merged
jorgerangel-msft merged 4 commits into
mainfrom
copilot/fix-writeobjectvalue-extension-method
Jul 22, 2026
Merged

fix(csharp): only generate XML serialization helpers when a model has XML usage#11341
jorgerangel-msft merged 4 commits into
mainfrom
copilot/fix-writeobjectvalue-extension-method

Conversation

Copilot AI commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

The XML WriteObjectValue<T> extension method emits a case IPersistableModel<T> that calls ModelReaderWriter.Write(..., <Namespace>Context.Default). When the generated ModelReaderWriterContext registers no buildable types, the source generator fails to produce .Default, breaking compilation (observed in Azure.Storage.Blobs.Batch).

Root cause: InputLibrary.GetHasXmlModelSerialization() treated an enum with XML usage as XML serialization support. Enums do not implement IPersistableModel and are never registered with the context, so an XML-usage enum alone enabled the XML helpers while leaving the context empty.

Changes

  • InputLibrary.GetHasXmlModelSerialization: only consider models (which implement IPersistableModel); removed the enum branch. This gates all XML serialization helpers — including WriteObjectValue and the BinaryContentHelper XML overload — on the presence of at least one XML model. Legitimate XML enums are already covered, since such enums only exist as properties of an XML model.
  • Tests: added ValidateXmlMethodsAreNotGeneratedWhenOnlyEnumHasXmlUsage, asserting no XmlWriter/XElement helpers are emitted when only an enum carries XML usage.
// Before: enum-only XML usage returned true -> XML helpers emitted, empty context -> build break
// After: only models with XML usage count
foreach (var model in InputNamespace.Models)
{
    if (model.Usage.HasFlag(InputModelTypeUsage.Xml))
    {
        return true;
    }
}
return false;

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

… XML usage

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label Jul 21, 2026
Copilot AI changed the title [WIP] Fix writeobjectvalue extension method generation when no xml models exist fix(csharp): only generate XML serialization helpers when a model has XML usage Jul 21, 2026
Copilot AI requested a review from jorgerangel-msft July 21, 2026 20:54
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
@pkg-pr-new

pkg-pr-new Bot commented Jul 21, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-csharp@11341

commit: bebeeef

…detection

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
@jorgerangel-msft
jorgerangel-msft added this pull request to the merge queue Jul 22, 2026
Merged via the queue into main with commit 46ea81d Jul 22, 2026
29 checks passed
@jorgerangel-msft
jorgerangel-msft deleted the copilot/fix-writeobjectvalue-extension-method branch July 22, 2026 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Do Not Generate WriteObjectValue Extension Method Case If No XmlModels Exist

3 participants