Skip to content

Fix duplicate inherited discriminators in Java models - #11824

Merged
Xiaofei Cao (XiaofeiCao) merged 12 commits into
microsoft:mainfrom
XiaofeiCao:fix-java-duplicate-discriminators
Sep 8, 2026
Merged

Fix duplicate inherited discriminators in Java models#11824
Xiaofei Cao (XiaofeiCao) merged 12 commits into
microsoft:mainfrom
XiaofeiCao:fix-java-duplicate-discriminators

Conversation

@XiaofeiCao

@XiaofeiCao Xiaofei Cao (XiaofeiCao) commented Sep 2, 2026

Copy link
Copy Markdown
Member

Summary

  • merge redundant leaf discriminator declarations with propagated parent discriminators
  • validate that duplicate declarations have matching constant values and Java types
  • emit fixed propagated discriminator fields as final without changing shared serialization behavior
  • regenerate Java clientcore and generator-test golden models

Fixes #11802

Testing

Merge redundant leaf discriminator declarations with propagated parent discriminators and preserve fixed discriminator fields as final.

Fixes microsoft#11802

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 54b93dc2-48ce-4250-a6ea-5bd9b9a7ab2b
@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:client:java Issue for the Java client emitter: @typespec/http-client-java label Sep 2, 2026
@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

All changed packages have been documented.

  • @typespec/http-client-java
Show changes

@typespec/http-client-java - fix ✏️

Prevent duplicate Java discriminator members while preserving inherited discriminators in stream-style XML serialization.

@azure-sdk-automation

azure-sdk-automation Bot commented Sep 2, 2026

Copy link
Copy Markdown

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 54b93dc2-48ce-4250-a6ea-5bd9b9a7ab2b
Keep active discriminators mutable so unknown values can round-trip through fallback deserialization, while fixed ancestor branch discriminators remain final.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 54b93dc2-48ce-4250-a6ea-5bd9b9a7ab2b
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 54b93dc2-48ce-4250-a6ea-5bd9b9a7ab2b

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The changes are well-scoped to discriminator propagation/emission, include validation, and add/regenerate coverage via golden models for the reported edge case.

Pull request overview

Fixes Java model generation for nested polymorphic discriminators by merging redundant leaf discriminator declarations with propagated parent discriminators, preventing duplicate fields/accessors and making fixed inherited discriminators final where safe.

Changes:

  • Merge same-serialized-name discriminator properties during parent-discriminator propagation, with validation that constant value and types match.
  • Emit inherited fixed discriminator fields as final (while keeping the active discriminator mutable to preserve unknown values on fallback deserialization).
  • Add a discriminator edge-case TypeSpec scenario and regenerate Java golden outputs (generator-test and clientcore-test).
File summaries
File Description
packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ModelMapper.java Propagates parent discriminators as constant and merges duplicate child declarations to avoid duplicate emission.
packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/implementation/PolymorphicDiscriminatorHandler.java Makes fixed inherited discriminators final when they aren’t the active discriminator.
packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/implementation/ClientModelPropertiesManager.java Treats merged parent discriminators as masking ancestor properties during property management.
packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/template/ModelTemplate.java Ensures super-constructor argument resolution can see propagated parent discriminators.
packages/http-client-java/generator/http-client-generator-test/tsp/discriminator-edge-cases.tsp Adds a nested discriminator + duplicate leaf discriminator scenario to exercise merging behavior.
packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/tsptest-discriminatoredgecases_metadata.json Regenerated metadata to include new discriminator edge-case models.
packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/discriminatoredgecases/models/GrandChildWithNestedDiscriminator.java New golden model for nested discriminator hierarchy.
packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/discriminatoredgecases/models/GreatGrandChildWithDuplicateDiscriminator.java New golden leaf model that repeats propagated discriminator to validate merging.
packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/discriminatoredgecases/models/ChildWithRequiredPropertyAsDiscriminator.java Regenerated subtype dispatch to include the new nested discriminator branch.
packages/http-client-java/generator/http-client-generator-test/src/main/java/type/model/inheritance/nesteddiscriminator/models/Shark.java Regenerated golden: fixed discriminator field becomes final.
packages/http-client-java/generator/http-client-generator-test/src/main/java/type/model/inheritance/nesteddiscriminator/models/SawShark.java Regenerated golden: fixed discriminator field becomes final.
packages/http-client-java/generator/http-client-generator-test/src/main/java/type/model/inheritance/nesteddiscriminator/models/GoblinShark.java Regenerated golden: fixed discriminator field becomes final.
packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/patch/models/Shark.java Regenerated golden: fixed discriminator field becomes final.
packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/patch/models/SawShark.java Regenerated golden: fixed discriminator field becomes final.
packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/enumnesteddiscriminator/models/Shark.java Regenerated golden: fixed discriminator enum field becomes final.
packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/enumnesteddiscriminator/models/SawShark.java Regenerated golden: fixed discriminator enum field becomes final.
packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/enumnesteddiscriminator/models/GoblinShark.java Regenerated golden: fixed discriminator enum field becomes final.
packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/armstreamstyleserialization/models/Shark.java Regenerated golden: fixed discriminator field becomes final.
packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/armstreamstyleserialization/models/SawShark.java Regenerated golden: fixed discriminator field becomes final.
packages/http-client-java/generator/http-client-generator-test/src/main/java/tsptest/armstreamstyleserialization/models/GoblinShark.java Regenerated golden: fixed discriminator field becomes final.
packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/nesteddiscriminator/Shark.java Regenerated clientcore golden: fixed discriminator field becomes final.
packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/nesteddiscriminator/SawShark.java Regenerated clientcore golden: fixed discriminator field becomes final.
packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/type/model/inheritance/nesteddiscriminator/GoblinShark.java Regenerated clientcore golden: fixed discriminator field becomes final.
.chronus/changes/fix-java-duplicate-discriminators-2026-09-02.md Changelog entry for the generator fix.
Review details
  • Files reviewed: 24/24 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Xiaofei Cao (XiaofeiCao) added a commit to XiaofeiCao/typespec-azure that referenced this pull request Sep 4, 2026
Advance the pinned core commit to the latest microsoft/typespec#11824 head and resync its discriminator regression fixtures.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: e11a1a40-39fd-4e62-a567-584fde94178c
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 00e7fd30-d342-4b5a-ad7e-f16198c7dbde
Xiaofei Cao (XiaofeiCao) added a commit to XiaofeiCao/typespec-azure that referenced this pull request Sep 4, 2026
Advance to the latest microsoft/typespec#11824 commit and sync its XML discriminator regression fixture.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: e11a1a40-39fd-4e62-a567-584fde94178c

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Core generator logic for polymorphic discriminator propagation and XML stream serialization is subtly behavior-affecting and should get a final human review despite tests/regenerations passing.

Review details
  • Files reviewed: 23/23 changed files
  • Comments generated: 1
  • Review effort level: Lite

Keep merged fixed discriminators in parentPolymorphicDiscriminators so JSON, constructors, and XML share one canonical representation.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 00e7fd30-d342-4b5a-ad7e-f16198c7dbde
Xiaofei Cao (XiaofeiCao) added a commit to XiaofeiCao/typespec-azure that referenced this pull request Sep 4, 2026
Advance to the latest microsoft/typespec#11824 commit and sync the restored propagated discriminator regression fixture.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: e11a1a40-39fd-4e62-a567-584fde94178c

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

It changes core polymorphic discriminator mapping and stream-style XML (de)serialization behavior, which can have broad downstream impact beyond the regenerated golden baselines.

Review details
  • Files reviewed: 28/28 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Xiaofei Cao (XiaofeiCao) added a commit to XiaofeiCao/typespec-azure that referenced this pull request Sep 4, 2026
Pin the Java emitter to microsoft/typespec#11824 and sync its inherited discriminator regression case.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: e11a1a40-39fd-4e62-a567-584fde94178c
Xiaofei Cao (XiaofeiCao) added a commit to XiaofeiCao/typespec-azure that referenced this pull request Sep 4, 2026
Advance the pinned core commit to the latest microsoft/typespec#11824 head and resync its discriminator regression fixtures.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: e11a1a40-39fd-4e62-a567-584fde94178c
Xiaofei Cao (XiaofeiCao) added a commit to XiaofeiCao/typespec-azure that referenced this pull request Sep 4, 2026
Advance to the latest microsoft/typespec#11824 commit and sync its XML discriminator regression fixture.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: e11a1a40-39fd-4e62-a567-584fde94178c
Xiaofei Cao (XiaofeiCao) added a commit to XiaofeiCao/typespec-azure that referenced this pull request Sep 4, 2026
Advance to the latest microsoft/typespec#11824 commit and sync the restored propagated discriminator regression fixture.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: e11a1a40-39fd-4e62-a567-584fde94178c
@XiaofeiCao
Xiaofei Cao (XiaofeiCao) marked this pull request as ready for review September 7, 2026 03:39
Explain nested discriminator propagation with a concrete type and role example, including why matching child properties are removed from normal model properties.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: e6933b7d-0cee-4855-a585-2e15b24c70c5
Copilot AI review requested due to automatic review settings September 7, 2026 06:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

It changes core Java model-mapping and stream-style XML polymorphism logic, so a final human review is warranted to validate broader compatibility beyond the updated golden outputs and described test runs.

Review details
  • Files reviewed: 28/28 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d4e8259f-d938-4448-9321-8eaf17f8b53d
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d4e8259f-d938-4448-9321-8eaf17f8b53d
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d4e8259f-d938-4448-9321-8eaf17f8b53d
Copilot AI review requested due to automatic review settings September 8, 2026 06:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

It changes core Java codegen behavior for polymorphic discriminator propagation and stream-style XML/JSON serialization across regenerated golden outputs, warranting a final human validation pass.

Review details
  • Files reviewed: 38/38 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@XiaofeiCao

Copy link
Copy Markdown
Member Author

Updated comments. Added dedicated tests, and detailed red/green scenario-fix can be found https://gist.github.com/XiaofeiCao/7ff7531da56345eedf61d01eff18c1f3

AFAIK, current fix touches little of "parent class in different package". model.getParentPolymorphicDiscriminators() stores parent fixed discriminators in outer->inner order for hierarchical serialization/deserialization, and will be written to child classes field for this exact purpose.

There's a dedicated simplified serialization/deserialization path for share-jsonserializable-code settings, where discriminator can remain in parent with package-private access. But this setting is never enabled, and doesn't affect model.getParentPolymorphicDiscriminators(), or vice-versa.

Xiaofei Cao (XiaofeiCao) added a commit to XiaofeiCao/typespec-azure that referenced this pull request Sep 8, 2026
Advance to the latest microsoft/typespec#11824 head and sync its focused inherited discriminator tests.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: e11a1a40-39fd-4e62-a567-584fde94178c
Xiaofei Cao (XiaofeiCao) added a commit to XiaofeiCao/typespec-azure that referenced this pull request Sep 8, 2026
Pin the Java emitter to microsoft/typespec#11824 and sync its inherited discriminator regression case.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: e11a1a40-39fd-4e62-a567-584fde94178c
Xiaofei Cao (XiaofeiCao) added a commit to XiaofeiCao/typespec-azure that referenced this pull request Sep 8, 2026
Advance the pinned core commit to the latest microsoft/typespec#11824 head and resync its discriminator regression fixtures.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: e11a1a40-39fd-4e62-a567-584fde94178c
Xiaofei Cao (XiaofeiCao) added a commit to XiaofeiCao/typespec-azure that referenced this pull request Sep 8, 2026
Advance to the latest microsoft/typespec#11824 commit and sync its XML discriminator regression fixture.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: e11a1a40-39fd-4e62-a567-584fde94178c
Xiaofei Cao (XiaofeiCao) added a commit to XiaofeiCao/typespec-azure that referenced this pull request Sep 8, 2026
Advance to the latest microsoft/typespec#11824 commit and sync the restored propagated discriminator regression fixture.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: e11a1a40-39fd-4e62-a567-584fde94178c
Xiaofei Cao (XiaofeiCao) added a commit to XiaofeiCao/typespec-azure that referenced this pull request Sep 8, 2026
Advance to the latest microsoft/typespec#11824 head and sync its focused inherited discriminator tests.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: e11a1a40-39fd-4e62-a567-584fde94178c
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: d4e8259f-d938-4448-9321-8eaf-ba5f78405f73
Copilot AI review requested due to automatic review settings September 8, 2026 08:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The change alters core polymorphism mapping plus stream-style XML/JSON serialization behavior and triggers broad golden regenerations, which warrants final human verification.

Review details
  • Files reviewed: 39/44 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@XiaofeiCao
Xiaofei Cao (XiaofeiCao) added this pull request to the merge queue Sep 8, 2026
Merged via the queue into microsoft:main with commit a61e908 Sep 8, 2026
35 of 36 checks passed
@XiaofeiCao
Xiaofei Cao (XiaofeiCao) deleted the fix-java-duplicate-discriminators branch September 8, 2026 09:44
Xiaofei Cao (XiaofeiCao) added a commit to XiaofeiCao/typespec-azure that referenced this pull request Sep 8, 2026
Advance the Java emitter pin to the merged microsoft/typespec#11824 main commit and sync the latest core tests.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: e11a1a40-39fd-4e62-a567-584fde94178c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:java Issue for the Java client emitter: @typespec/http-client-java

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Java emitter generates duplicate fixed discriminator members in Realtime message leaves

3 participants