fix(http-client-csharp): forward discriminator to base ctor for external base models#11002
Merged
JoshLove-msft merged 3 commits intoJun 17, 2026
Conversation
…nal base models External base models marked via @alternateType now map to a SystemObjectModelProvider (a ModelProvider) in TypeFactory.CreateModel, so derived discriminated types emit the correct base constructor call with the discriminator value, and the external base type itself is not generated. Handling this in CreateModel (rather than the overridable CreateModelCore) ensures all generators, including the Scm client generator, share the behavior. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
commit: |
Contributor
|
No changes needing a change description found. |
|
You can try these changes here
|
…r forwarding - ScmTypeFactoryTests: verify an external base model maps to SystemObjectModelProvider and the discriminator is forwarded through the Scm factory (which overrides CreateModelCore) - guards against regressing the fix back into CreateModelCore only. - SystemObjectModelProviderTests: external model that cannot be resolved falls back to normal generation; a property typed as an external model resolves to the external type while the external model itself is not generated. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
jsquire
approved these changes
Jun 17, 2026
chidozieononiwu
pushed a commit
to chidozieononiwu/typespec
that referenced
this pull request
Jun 22, 2026
…e declares them (microsoft#11044) Fixes microsoft#11042 ## Problem Models derived from an external/referenced MRW-generated base type (e.g. `Azure.AI.Extensions.OpenAI` types deriving from OpenAI's `ResponseItem`) generated `PersistableModelCreateCore` and the other serialization `*Core` methods as `protected virtual` instead of `protected override`. The compiler then reports CS0114 (`hides inherited member ... add the override keyword`). ## Root cause PR microsoft#11002 routes external `InputModelType`s through `SystemObjectModelProvider` so derived models get a real `ModelProvider` base. However `SystemObjectModelProvider.ShouldSkipDerivedSerializationMethodOverrides` unconditionally returns `true` (introduced in microsoft#9862 for hand-authored ARM bases like `ResourceData` that do **not** declare the generated `*Core` methods). Reusing it for *all* external bases forced derived models to hide rather than override even when the external base is itself MRW-generated and exposes the virtual `*Core` methods. ## Fix `MrwSerializationTypeDefinition` now decides whether to skip the derived overrides by reflecting on the wrapped framework type when the base is a `SystemObjectModelProvider`: if it declares all four generated `*Core` methods (`JsonModelWriteCore`, `JsonModelCreateCore`, `PersistableModelWriteCore`, `PersistableModelCreateCore`), the derived model emits `protected override`; otherwise existing behavior is preserved (bases without those methods, e.g. `object`/`ResourceData`, still emit `virtual`). ## Tests Added regression tests in `SystemObjectModelSerializationTests` covering an external base that follows the generated MRW pattern, asserting the four `*Core` methods are emitted as `override`. Existing tests (object base -> virtual) remain unchanged. Full ClientModel suite (1435 tests) passes. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Discriminated subtypes whose base model is marked as an external type (via the TCGC
@alternateTypedecorator) did not forward the discriminator value in the generated base constructor call. When the base was external, it became a bareTypeProviderthat cannot serve as aBaseModelProvider, so no: base(...)initializer was emitted and the discriminator literal was dropped.Fix
Route external
InputModelTypes throughSystemObjectModelProvider(a realModelProvider) so the existing constructor / discriminator-forwarding logic applies:TypeFactory.CreateModel: external models now map to aSystemObjectModelProviderwrapping the resolved framework/referenced type, handled before the overridableCreateModelCoreso all generators (including the Scm client generator, whoseScmTypeFactory.CreateModelCoreoverride previously bypassed it) share the behavior.OutputLibrary.BuildModels: external bases mapped toSystemObjectModelProviderare excluded from emission (the external type already exists in a referenced assembly).Result
For a discriminated base
Animalmarked external, derived types now emit:and the external base type itself is not generated.
Validation
SystemObjectModelProviderTests.Microsoft.TypeSpec.Generator.Tests(1536) andMicrosoft.TypeSpec.Generator.ClientModel.Tests(1429); C# formatting clean.