[http-client-csharp] Model factories use intermediate discriminator for leaf models sharing an inherited discriminator
Maintainer antworten meist innerhalb von 1 Tag
@JoshLove01 arbeitet bereits daran.
Seit 12.10.2026.
- #12157 von @copilot-swe-agent — ohne Merge geschlossen
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 22/100
Rechercherichtung
Start in Microsoft.TypeSpec.Generator/src/Providers/ModelProvider.cs at EnsureDiscriminatorValueExpression, which only searches the direct base's canonical properties, and in ModelFactoryProvider.cs at GetDiscriminatorExpression, which walks ancestors. Run the focused NUnit management-generator tests for the instant and range query factories. Done means the leaf factories pass their own discriminator value, separate-discriminator hierarchies such as root kind and nested nestedKind still pass, and customization mappings are preserved.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Summary
@typespec/http-client-csharp selects an intermediate model's discriminator value in model factories when three inheritance levels share one discriminator property. The concrete leaf models have the correct DiscriminatorValue, but their DiscriminatorValueExpression is null. This needs property-specific discriminator resolution in the upstream generator rather than a generated-SDK patch.
Observed in Azure SDK regeneration PR https://github.com/Azure/azure-sdk-for-net/pull/63730, review comment https://github.com/Azure/azure-sdk-for-net/pull/63730#discussion_r4237098529.
Minimal generator-input reproduction
Using the existing management-generator NUnit infrastructure and upstream InputFactory/ModelFactoryProvider, construct:
var discriminatorEnum = InputFactory.StringEnum(
"AlertsManagementType",
[("PrometheusEnrichmentItem", "PrometheusEnrichmentItem"),
("PrometheusInstantQuery", "PrometheusInstantQuery"),
("PrometheusRangeQuery", "PrometheusRangeQuery")],
access: "internal", isExtensible: true, clientNamespace: "Samples.Models");
var instantQuery = InputFactory.Model(
"PrometheusInstantQuery", properties: [], discriminatedKind: "PrometheusInstantQuery");
var rangeQuery = InputFactory.Model(
"PrometheusRangeQuery", properties: [], discriminatedKind: "PrometheusRangeQuery");
var prometheusItem = InputFactory.Model(
"PrometheusEnrichmentItem", properties: [], discriminatedKind: "PrometheusEnrichmentItem",
derivedModels: [instantQuery, rangeQuery]);
var enrichmentItem = InputFactory.Model(
"AlertEnrichmentItem",
properties: [InputFactory.Property("alertEnrichmentType", discriminatorEnum,
isRequired: true, isDiscriminator: true)],
derivedModels: [prometheusItem]);
The hierarchy has one discriminator property declared on the root; neither the intermediate nor leaf models declares another discriminator.
The unmodified upstream factory body, before management constructor repair, is:
return new PrometheusInstantQuery(
AlertsManagementType.PrometheusEnrichmentItem,
additionalBinaryDataProperties: null);
The range-query factory likewise uses PrometheusEnrichmentItem.
Expected: the respective AlertsManagementType.PrometheusInstantQuery / AlertsManagementType.PrometheusRangeQuery value.
Impact in the SDK
Both generated factory constructors forward the supplied discriminator unchanged. Serialization writes "type":"PrometheusEnrichmentItem", while polymorphic readers dispatch concrete queries only for "PrometheusInstantQuery" and "PrometheusRangeQuery". Reading through the base type falls through to UnknownPrometheusEnrichmentItem, losing the concrete query type.
This impact was validated by inspecting the generated constructor/writer/reader paths; no SDK runtime round-trip test was run for this report.
Root-cause evidence
The focused test reports:
| Model | DiscriminatorValue | DiscriminatorValueExpression | Declared discriminator properties |
|---|---|---|---|
| AlertEnrichmentItem | null | null | AlertEnrichmentType |
| PrometheusEnrichmentItem | PrometheusEnrichmentItem | correct enum expression | none |
| PrometheusInstantQuery | PrometheusInstantQuery | null | none |
| PrometheusRangeQuery | PrometheusRangeQuery | null | none |
In the installed upstream generator:
ModelProvider.EnsureDiscriminatorValueExpressionsearches onlyBaseModelProvider.CanonicalView.Propertiesfor the discriminator. The direct parent of each leaf declares no discriminator, so the leaf expression is null.ModelFactoryProvider.GetDiscriminatorExpressionrecursively walks base models untilmodel.BaseModelProvider.DiscriminatorProperty == property, then returns that level's expression. Here it returns the intermediate model's value rather than the leaf's.
Source locations (the same direct-parent/ancestor-selection logic is also present on current TypeSpec main):
- https://github.com/microsoft/typespec/blob/main/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ModelProvider.cs
- https://github.com/microsoft/typespec/blob/main/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ModelFactoryProvider.cs
Validation and provenance
- Reproduced using
@typespec/http-client-csharp1.0.0-alpha.20260918.2, in Azure SDK for .NET main revisione9bdd3ebf344345a7ae2dd1828d0577d33fe1651. - Added four local NUnit cases: instant/range factories × visitor-time/write-time management repair. All four fail the expected leaf-discriminator assertion. The upstream bodies are already incorrect before either repair path.
- A controlled comparison replacing only the management repair helper with its pre-PR #63719 revision, retaining identical current dependencies and test expectations, also fails all four cases: old repair emits typed
default, current repair emits the intermediate value. This is not a full historical-checkout comparison. - Therefore https://github.com/Azure/azure-sdk-for-net/pull/63719 exposed the intermediate non-null value but did not introduce the underlying concrete-type failure.
Suggested direction and regression coverage
Resolve discriminator values for the specific property, distinguishing a shared discriminator across inheritance levels from separate discriminators declared at different levels. For the shared property, use the most-derived applicable value and the declaring property's enum type/mapping. Preserve customization mappings and unknown-model behavior.
Simply using the leaf's existing DiscriminatorValueExpression is insufficient because it is null here. Conversely, always using the leaf's value would break hierarchies with separate discriminators (e.g. root kind and intermediate nestedKind). Please cover both cases, including upstream factory generation and constructor initialization. Management constructor repair has analogous ancestor-selection logic and will need alignment once upstream resolution is corrected.
- by copilot
- Vorherrschende Sprache
- Java
- Sterne
- 5.9k
- Forks
- 421
- Ø Merge
- 2 T. 3 Std.
- Gemergte PRs (30 T.)
- 176
Entwicklungsumgebung
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus microsoft/typespec
-
bug emitter:client:csharp
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
microsoft/typespec#12572 · 1 Kommentar · 1 Reaktion ·
Maintainer antworten meist innerhalb von 1 Tag
-
Make website build cross-platform compatibleEvtl. vergeben @pcbeingused333 hat das heute übernommen. Offenbug meta:website
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
microsoft/typespec#12550 · 1 Kommentar · 1 Reaktion ·
Maintainer antworten meist innerhalb von 1 Tag
-
Emitter framework: make the language-specific Alloy peer dependencies optionalEvtl. vergeben @timotheeguerin hat das vor 8 Tagen übernommen. Offenemitter-framework feature triaged:core
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 62/100
microsoft/typespec#12476 · 1 Kommentar · 2 Reaktionen · 2 zugewiesene Personen ·
Maintainer antworten meist innerhalb von 1 Tag
-
Upgrade C# generator Roslyn to 5.9.0Evtl. vergeben @ArthurMa1978 hat das vor 7 Tagen übernommen. Offenemitter:client:csharp feature
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
microsoft/typespec#12445 · 2 Kommentare · 1 Reaktion · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug emitter:client:python
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
microsoft/typespec#12360 · 1 Kommentar · 1 Reaktion ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in microsoft/typespec
Ähnliche Issues
-
GCC 15.2: -Wcast-qual warning - cast discards const qualifier from pointer target type (H5trace.c)Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 73/100
Maintainer antworten meist innerhalb von 1 Tag
-
[Bug] RemotingHelper.connect swaps the socket send and receive buffer configurationEvtl. vergeben @amalsaidov hat das heute übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
Maintainer antworten meist innerhalb von 1 Tag
-
needs-area-label
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 2 Tagen
-
area/ai area/backend component/zeebe kind/task
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
spAnser/reacharound#38 ·