Skip to content

Allow customization of base model when spec defines a base model#9181

Merged
live1206 merged 2 commits intomainfrom
copilot/remove-base-model-check
Dec 11, 2025
Merged

Allow customization of base model when spec defines a base model#9181
live1206 merged 2 commits intomainfrom
copilot/remove-base-model-check

Conversation

Copy link
Contributor

Copilot AI commented Dec 11, 2025

The generator prevented customizing a model's base type when the TypeSpec already specified a base model. This blocked scenarios where generated models need to inherit from a different base than what the spec defines.

Changes

  • Modified ModelProvider.BuildBaseModelProvider(): Check for CustomCodeView?.BaseType before falling back to _inputModel.BaseModel, enabling custom base types to override spec-defined base models

  • Added test CanCustomizeBaseModelWithSpecBase: Verifies that a model with a spec-defined base can be customized to inherit from a different base model instead

Example

Given a TypeSpec model with a base:

// Spec defines: MockInputModel extends SpecBaseModel

Users can now customize it to use a different base:

namespace Sample.Models
{
    public partial class MockInputModel : CustomBaseModel  // Overrides spec base
    {
    }
}

The generated code will respect the custom base type instead of the spec-defined one.

Original prompt

This section details on the original issue you should resolve

<issue_title>Unable to customize base model of generated model if spec has a base model</issue_title>
<issue_description>In MTG, we can only customize base model if input model doesn't have a base model already.

if (_inputModel.BaseModel == null)
{
// consider models that have been customized to inherit from a different model
if (CustomCodeView?.BaseType != null)
{
var baseType = CustomCodeView.BaseType;

There are cases, we need to customize the base model even the spec has specified the base model.</issue_description>

<agent_instructions>remove the input model base model check and add a unit test to verify the scenario</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…ec base

Co-authored-by: live1206 <5196139+live1206@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix issue with customizing base model in generated model Allow customization of base model when spec defines a base model Dec 11, 2025
Copilot AI requested a review from live1206 December 11, 2025 01:49
Copy link
Contributor

@JoshLove-msft JoshLove-msft left a comment

Choose a reason for hiding this comment

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

LGTM can you try running the RegenPreview?

@live1206 live1206 marked this pull request as ready for review December 11, 2025 05:43
@live1206
Copy link
Contributor

LGTM can you try running the RegenPreview?

@copilot could you execute this?

Copy link
Contributor Author

Copilot AI commented Dec 11, 2025

LGTM can you try running the RegenPreview?

@copilot could you execute this?

I don't have access to the azure-sdk-for-net repository in this environment to run RegenPreview. However, I've validated the changes by:

  1. Running all 1160 generator unit tests - all pass ✅
  2. The change is minimal and surgical - only reorders the conditional checks in BuildBaseModelProvider() to prioritize custom base types
  3. Added a specific test CanCustomizeBaseModelWithSpecBase that validates the new scenario
  4. All existing tests for base model handling continue to pass, confirming backward compatibility

The change allows customization to override spec-defined base models without affecting existing behavior when no customization is present. Once this PR is merged, the RegenPreview can be run by someone with access to azure-sdk-for-net to validate against actual SDK libraries.

@live1206 live1206 added this pull request to the merge queue Dec 11, 2025
Merged via the queue into main with commit 3317b78 Dec 11, 2025
23 checks passed
@live1206 live1206 deleted the copilot/remove-base-model-check branch December 11, 2025 06:24
@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 Dec 11, 2025
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.

Unable to customize base model of generated model if spec has a base model

3 participants