Skip to content

Fix MRW JSON write core override for custom bases#11098

Open
live1206 wants to merge 1 commit into
mainfrom
live1206/fix-mrw-custom-base-json-write-core
Open

Fix MRW JSON write core override for custom bases#11098
live1206 wants to merge 1 commit into
mainfrom
live1206/fix-mrw-custom-base-json-write-core

Conversation

@live1206

@live1206 live1206 commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes MRW JSON serialization generation when a serialization provider is constructed before its model's BaseModelProvider is available.

MrwSerializationTypeDefinition previously cached the JSON override decision in the constructor. In customization/visitor scenarios, the model can later resolve to a SystemObjectModelProvider base. One example is an Azure management model customized to inherit from TrackedResourceData: the generated model declaration can correctly inherit from the system base, but the serialization provider still uses the stale constructor-time decision and emits protected virtual JsonModelWriteCore(...), hiding the inherited base method.

This keeps the existing BaseModelProvider != null behavior and evaluates it when JSON serialization methods are built, after the delayed base provider can be resolved. It also makes the JSON/Persistable serialization interface CSharpTypes lazy so the constructor does not force _model.Type and cache a type before the delayed base is available.

The XML path intentionally keeps its existing constructor-scoped behavior instead of broadly late-emitting internal override XmlModelWriteCore(...) for external/system bases, where that XML core method may not exist or be accessible.

Validation

  • dotnet test /home/huwe/src/typespec/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Microsoft.TypeSpec.Generator.ClientModel.Tests.csproj --filter JsonModelWriteCore_IsOverride_WhenBaseProviderIsResolvedAfterSerialization --no-restore
  • dotnet test /home/huwe/src/typespec/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Microsoft.TypeSpec.Generator.ClientModel.Tests.csproj --filter "SystemObjectModelSerializationTests|XmlSerializationTests|XmlSerializationCustomizationTests" --no-restore
  • Verified with the Azure SDK MPG regression by temporarily referencing this local MTG project: dotnet test eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/test/Azure.Generator.Mgmt.Tests.csproj --filter ResourceDataModelWithCustomTrackedResourceBaseOverridesSerialization

@pkg-pr-new

pkg-pr-new Bot commented Jun 26, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: d0c358f

@github-actions

Copy link
Copy Markdown
Contributor

No changes needing a change description found.

@live1206 live1206 force-pushed the live1206/fix-mrw-custom-base-json-write-core branch 4 times, most recently from 60f496e to fe42ffe Compare June 26, 2026 08:35
Detect overridable JsonModelWriteCore methods on custom and framework base types so derived model serialization emits override instead of hiding the base member.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@live1206 live1206 force-pushed the live1206/fix-mrw-custom-base-json-write-core branch from fe42ffe to d0c358f Compare June 26, 2026 08: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.

1 participant