[csharp] Derive ModelProvider.BaseModelProvider from BaseType#10486
Draft
ArcturusZhang wants to merge 1 commit intomicrosoft:mainfrom
Draft
[csharp] Derive ModelProvider.BaseModelProvider from BaseType#10486ArcturusZhang wants to merge 1 commit intomicrosoft:mainfrom
ArcturusZhang wants to merge 1 commit intomicrosoft:mainfrom
Conversation
Collaborator
|
You can try these changes here
|
9d11c17 to
8c856a0
Compare
commit: |
Contributor
|
No changes needing a change description found. |
28c5da6 to
6f63aa8
Compare
Fixes microsoft#10485 ModelProvider exposed two parallel APIs for the parent type: * BaseType (via BuildBaseType, virtual on TypeProvider) * BaseModelProvider (via BuildBaseModelProvider, private) Their resolution paths were independent: BaseModelProvider read `_inputModel.BaseModel` directly while BuildBaseType was virtual. Subclasses overriding BuildBaseType (e.g. emitters that replace the spec inheritance with a hand-written base class) ended up with the two APIs describing different parents, and visitors that walked BaseModelProvider saw properties from a model that was not in the C# inheritance chain. Make BaseType the single source of truth: * Move CustomCodeView precedence and the empty-namespace name resolution into ModelProvider.BuildBaseType, so BaseType already reflects the customized/spec/overridden parent as a CSharpType. * Reduce BuildBaseModelProvider to a CSharpTypeMap lookup of BaseType. When BaseType is a non-generated type (subclass override, system type, hand-written external base) the lookup naturally misses and BaseModelProvider is null - which is what callers want, since they use it to walk generated parent properties/fields/etc. Adds a regression test that overrides BuildBaseType to point at an external type and asserts BaseModelProvider stays consistent. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
6f63aa8 to
ff57562
Compare
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.
Fixes #10485
Problem
ModelProvider.BuildBaseTypeisprotected virtual, butBuildBaseModelProviderwasprivateand computed the base independently fromInputModelType.BaseModel. A derivedModelProviderthat overrodeBuildBaseType(for example, to replace the spec inheritance chain with a hand-written base class — as the Azure mgmt/provisioning generators do) ended up withBaseTypeandBaseModelProviderdescribing different parents.Visitors that walk
BaseModelProviderthen saw properties from a model that wasn't actually in the C# inheritance chain — for example, the mgmtFlattenPropertyVisitorended up flattening the same set of properties twice.Fix
Make
BaseTypethe single source of truth, and deriveBaseModelProviderfrom it.TypeProvider.BaseTypeis nowvirtual.ModelProvider.BaseTypeoverrides it to handleCustomCodeViewprecedence (custom base wins over spec base) and the namespace-less name resolution for custom bases that Roslyn could not resolve.ModelProvider.BuildBaseTypeis now spec-only and no longer needs to know aboutCustomCodeView.ModelProvider.BuildBaseModelProvidersimply looks upBaseTypein theTypeFactory.CSharpTypeMap.After this change, overriding
BuildBaseType(orBaseType) on a derivedModelProviderautomatically keepsBaseModelProviderin sync — they cannot diverge.Test
Added
OverridingBuildBaseTypeKeepsBaseModelProviderConsistentinModelProviderTests.cs. It defines a derivedModelProviderthat overridesBuildBaseTypeto return an external (hand-written) base type, mirroring what downstream emitters do, and asserts thatBaseModelProviderfollowsBaseType(becomesnullbecause the external type is not a generatedModelProvider). Under the old code,BaseModelProviderwould still have resolved to the spec base, which is exactly the bug being fixed.All 1434
Microsoft.TypeSpec.Generator.Testsand 1312Microsoft.TypeSpec.Generator.ClientModel.Testspass.