Fix duplicate inherited discriminators in Java models - #11824
Fix duplicate inherited discriminators in Java models#11824Xiaofei Cao (XiaofeiCao) merged 12 commits into
Conversation
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
|
All changed packages have been documented.
Show changes
|
|
You can try these changes here
|
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
There was a problem hiding this comment.
🟢 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.
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
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
There was a problem hiding this comment.
🔵 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
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
There was a problem hiding this comment.
🔵 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
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
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
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
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
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
There was a problem hiding this comment.
🔵 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
There was a problem hiding this comment.
🔵 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
|
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". There's a dedicated simplified serialization/deserialization path for |
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
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
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
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
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
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
There was a problem hiding this comment.
🔵 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
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
Summary
finalwithout changing shared serialization behaviorFixes #11802
Testing
http-client-generator-clientcore-test/Generate.ps1regenerationhttp-client-generator-test/Generate.ps1regeneration