Skip to content

Prevent C# model factories from instantiating abstract unknown variants - #11878

Open
Jorge Rangel (jorgerangel-msft) with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-back-compat-model-factory
Open

Prevent C# model factories from instantiating abstract unknown variants#11878
Jorge Rangel (jorgerangel-msft) with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-back-compat-model-factory

Conversation

Copilot AI commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

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).

  • Factory selection: Exclude abstract unknown variants from the shared selector used by regular and back-compat factories.
  • Regression coverage: Cover direct construction when no current overload exists, missing unknown variants, and unknown variants customized as abstract.

@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.

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 Sep 8, 2026
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
@pkg-pr-new

pkg-pr-new Bot commented Sep 8, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: d20e4de

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 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.

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 8, 2026
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.

Back Compat Model Factory Method Should Not Instantiate Abstract Model Types

4 participants