Fix nested subtypes tagged union generation (#1005)#1127
Open
almarzn wants to merge 1 commit intovojtechhabarta:mainfrom
Open
Fix nested subtypes tagged union generation (#1005)#1127almarzn wants to merge 1 commit intovojtechhabarta:mainfrom
almarzn wants to merge 1 commit intovojtechhabarta:mainfrom
Conversation
When using Jackson's @JsonTypeInfo and @JsonSubTypes with nested subtypes
(intermediate abstract classes that also have @JsonSubTypes), the generated
tagged union types now correctly reference the intermediate subtypes'
union aliases instead of the intermediate interfaces directly.
For example, with hierarchy:
Animal -> Mammal -> Cat/Dog
-> Bird -> Parrot/Duck
Before:
type AnimalUnion = Mammal | Bird;
type MammalUnion = Cat | Dog;
type BirdUnion = Parrot | Duck;
After:
type AnimalUnion = MammalUnion | BirdUnion;
type MammalUnion = Cat | Dog;
type BirdUnion = Parrot | Duck;
The fix builds a map of class -> union symbol before constructing the
unions, allowing nested subtypes to be replaced by their union aliases
when they appear as named entries in their parent's @JsonSubTypes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1005 - Incorrect tagged unions when using Jackson and nested subtypes.
Problem
When using Jackson's
@JsonTypeInfoand@JsonSubTypeswith nested subtypes (intermediate abstract classes that also have@JsonSubTypes), the generated tagged union types referenced the intermediate interfaces directly instead of their own tagged union types.For example, with this hierarchy:
Before (buggy):
This meant
const animal: AnimalUnion = { type: "Cat", ... }would fail with a TypeScript error becauseCatis not assignable toMammal.After (fixed):
Solution
Modified
ModelCompiler.createAndUseTaggedUnionsto:Class<?> -> Symbolfor all beans that have tagged unions before constructing the unions@JsonSubTypesannotation (ensuring backward compatibility with existing behavior for intermediate types without names)Testing
TaggedUnionsTest.testNestedSubtypesTaggedUnionthat reproduces the issuetestIntermediateUnionstest