Skip to content

Conversation

@radhgupta
Copy link
Member

@radhgupta radhgupta commented Nov 13, 2025

This pull request introduces a new dual constructor pattern for intermediate models in discriminated union hierarchies, ensuring correct instantiation and inheritance when models share discriminator property names with their base models. It adds logic to detect when this pattern is needed, generates the appropriate constructors, and updates constructor initialization to support multi-layer discriminator scenarios. Several new tests are included to validate these changes.

Constructor Pattern Enhancements

  • Added a new ConstructorType enum to define different constructor generation strategies, supporting public, private protected, and internal constructors for various use cases.
  • Implemented logic in ModelProvider to detect when a model should use the dual constructor pattern (i.e., when it shares a discriminator property name with its base and has derived models).
  • Added methods to generate three constructors for intermediate discriminated models: a public constructor (for external use), a private protected constructor (for inheritance), and an internal constructor (for serialization).

Constructor Initialization Logic

  • Updated constructor initialization to correctly call the appropriate base constructor, including passing discriminator values when required for intermediate models using the dual constructor pattern.

Testing Multi-Layer Discriminator Hierarchies

  • Added comprehensive tests to cover multi-layer discriminator scenarios, including cases with and without discriminator properties at intermediate layers, and verifying correct constructor argument initialization.

Minor Cleanup

  • Removed an unused discriminatedKind argument in a test setup for clarity.

Addresses: Azure/azure-sdk-for-net#51958

@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 Nov 13, 2025
@github-actions
Copy link
Contributor

No changes needing a change description found.

@radhgupta radhgupta marked this pull request as ready for review November 14, 2025 18:18
public class ModelProvider : TypeProvider
{
private const string AdditionalBinaryDataPropsFieldDescription = "Keeps track of any properties unknown to the library.";
private const string DiscriminatorParameterName = "discriminatorValue";
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I think we don't need these anymore

Copy link
Member Author

Choose a reason for hiding this comment

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

I am still using it on line 823:
{ var discriminatorParam = new ParameterProvider( DiscriminatorParameterName, $"{DiscriminatorParameterDescription}", new CSharpType(typeof(string))); constructorParameters.Insert(0, discriminatorParam); }

Copy link
Contributor

Choose a reason for hiding this comment

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

Once we adjust BuildConstructorParameters to have a parameter like includeHierarchyDiscriminator and pass that through to GetAllBaseProperties, we shouldn't need it anymore as it will be constructed automatically from the property.

Copy link
Contributor

Choose a reason for hiding this comment

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

Woops we still need to remove these.

@JoshLove-msft
Copy link
Contributor

Looks like the CI is failing

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.

Looks great!

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.

2 participants