Skip to content

[csharp] Make ModelProvider.BuildBaseModelProvider virtual to keep BaseType and BaseModelProvider consistent#10600

Draft
Copilot wants to merge 6 commits intomainfrom
copilot/fix-mismatched-base-type-values
Draft

[csharp] Make ModelProvider.BuildBaseModelProvider virtual to keep BaseType and BaseModelProvider consistent#10600
Copilot wants to merge 6 commits intomainfrom
copilot/fix-mismatched-base-type-values

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 5, 2026

ModelProvider.BuildBaseType is virtual and emitters override it to redirect the C# base class (e.g., to a framework type they own), but BuildBaseModelProvider was private, so BaseModelProvider kept walking InputModelType.BaseModel and diverged from the generated class hierarchy. Visitors traversing BaseModelProvider would see "inherited" properties from a parent that is no longer the actual C# base.

Changes

  • ModelProvider.BuildBaseModelProvider: privateprotected virtual.
  • Added an XML doc comment pointing emitters at the recommended override pattern: when overriding BuildBaseType to redirect the generated C# base class, also override BuildBaseModelProvider so that BaseType and BaseModelProvider stay consistent.
  • Test in ModelProviderTests covering the override pattern.

Example: emitter redirecting to a framework base

internal class MyModelProvider : ModelProvider
{
    private readonly CSharpType _frameworkBase;

    public MyModelProvider(InputModelType inputModel, CSharpType frameworkBase)
        : base(inputModel) => _frameworkBase = frameworkBase;

    protected override CSharpType? BuildBaseType() => _frameworkBase;
    protected override ModelProvider? BuildBaseModelProvider() => null;
}

BaseType and BaseModelProvider now stay in lock-step — BaseModelProvider is null (since the framework base isn't a ModelProvider) instead of leaking the original input-model parent.

Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/96294dad-fa6c-4b21-a8c7-8fcfb0107c92

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 May 5, 2026
Copilot AI changed the title [WIP] Fix mismatched values returned by ModelProvider.BaseType and BaseModelProvider [csharp] Make ModelProvider base resolution methods virtual to keep BaseType and BaseModelProvider consistent May 5, 2026
Copilot AI requested a review from jorgerangel-msft May 5, 2026 19:48
Comment thread .chronus/changes/fix-modelprovider-base-virtual-2026-5-5-19-21-0.md Outdated
Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/60279f78-9af0-440e-97b7-7026b39783c9

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 5, 2026

Open in StackBlitz

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

commit: b0cf418

Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/5b6fcac7-58a2-4f8e-be3e-2b2870fbeed0

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Copilot AI changed the title [csharp] Make ModelProvider base resolution methods virtual to keep BaseType and BaseModelProvider consistent [csharp] Make ModelProvider.BuildBaseModelProvider virtual to keep BaseType and BaseModelProvider consistent May 5, 2026
Copilot AI requested a review from jorgerangel-msft May 5, 2026 20:12
Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/cab911a0-953a-4889-b96d-18aaccf1af83

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 5, 2026

No changes needing a change description found.

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.

[csharp] ModelProvider.BaseType and BaseModelProvider can return mismatched values

2 participants