Prevent C# model factories from instantiating abstract unknown variants - #11878
Open
Jorge Rangel (jorgerangel-msft) with Copilot wants to merge 2 commits into
Open
Prevent C# model factories from instantiating abstract unknown variants#11878Jorge Rangel (jorgerangel-msft) with Copilot wants to merge 2 commits into
Jorge Rangel (jorgerangel-msft) with Copilot wants to merge 2 commits into
Conversation
|
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. |
Copilot started work on behalf of
Jorge Rangel (jorgerangel-msft)
September 8, 2026 16:15
View session
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix back compat model factory method to prevent instantiation of abstract types
Prevent C# model factories from instantiating abstract unknown variants
Sep 8, 2026
commit: |
Jorge Rangel (jorgerangel-msft)
approved these changes
Sep 8, 2026
Jorge Rangel (jorgerangel-msft)
marked this pull request as ready for review
September 8, 2026 19:16
Jorge Rangel (jorgerangel-msft)
requested review from
JoshLove-msft,
Jose Arriaga Maldonado (joseharriaga),
Jesse Squire (jsquire) and
m-nash
as code owners
September 8, 2026 19:16
Copilot started reviewing on behalf of
Jorge Rangel (jorgerangel-msft)
September 8, 2026 19:17
View session
Jorge Rangel (jorgerangel-msft)
enabled auto-merge
September 8, 2026 19:17
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The change is narrowly scoped to constructor target selection, and the added regression tests cover the key failure modes described in the issue.
Pull request overview
This PR fixes C# model factory generation to avoid instantiating abstract model types when producing factory methods (including back-compat overloads), which can otherwise emit invalid C# (CS0144). It aligns factory selection with the requirement that abstract models should be constructed via a concrete Unknown* discriminator variant when available, or skipped when not.
Changes:
- Updated the shared model-to-instantiate selector to exclude abstract
Unknown*discriminator variants. - Added regression tests for (1) back-compat method generation when there is no current overload and the return type is abstract, and (2) skipping generation when no concrete unknown variant is available (including when the unknown variant is customized as abstract).
File summaries
| File | Description |
|---|---|
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ModelFactoryProvider.cs | Ensures abstract models only select a concrete unknown discriminator model for instantiation; otherwise no model is selected. |
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelFactories/ModelFactoryProviderTests.cs | Adds regression coverage for abstract return types in back-compat factories, including skip behavior when no concrete unknown exists. |
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelFactories/TestData/ModelFactoryProviderTests/BackCompatibility_AbstractReturnTypeWithoutCurrentOverload/SampleNamespaceModelFactory.cs | Adds last-contract source for the “no current overload” back-compat scenario. |
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelFactories/TestData/ModelFactoryProviderTests/BackCompatibility_AbstractReturnTypeWithoutCurrentOverload.cs | Adds expected emitted model factory output validating concrete unknown instantiation. |
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelFactories/TestData/ModelFactoryProviderTests/BackCompatibility_AbstractReturnTypeWithoutConcreteUnknownModelIsSkipped/AbstractModel.cs | Adds customization source making the unknown variant abstract to validate the “skip method” behavior. |
Review details
- Files reviewed: 5/5 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
JoshLove-msft
approved these changes
Sep 8, 2026
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Sep 8, 2026
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.
Back-compat model factories must return a concrete
Unknown*variant for abstract models or omit the method when none is available. Instantiating an abstract type produces invalid C# (CS0144).