[http-client-csharp] Model factories use intermediate discriminator for leaf models sharing an inherited discriminator

未关闭
#12,151 1 条评论 2 个 reaction 已指派 2 人 在 GitHub 查看

维护者通常 1 天内回复

@JoshLove01 已经在做这个了。

开始于 2026年10月12日。

  • #12157 来自 @copilot-swe-agent —— 已关闭,未合并

评估

难度
4/5
预计耗时
3-5 天
新手友好度
22/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
csharp
领域
devtools

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

bug emitter:client:csharp

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.EnsureDiscriminatorValueExpression searches only BaseModelProvider.CanonicalView.Properties for the discriminator. The direct parent of each leaf declares no discriminator, so the leaf expression is null.
  • ModelFactoryProvider.GetDiscriminatorExpression recursively walks base models until model.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):

Validation and provenance

  • Reproduced using @typespec/http-client-csharp 1.0.0-alpha.20260918.2, in Azure SDK for .NET main revision e9bdd3ebf344345a7ae2dd1828d0577d33fe1651.
  • 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

主要语言
Java
星标
5.9k
派生
421
平均合并
2 天 3 小时
30 天内合并 PR
176

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/typespec 的其他 Issue

查看 microsoft/typespec 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。