Repository navigation
fix: find JSON subtypes on the interfaces of a class too - #6144
Conversation
The subtypes plugin assumed that polymorphic types always use the default Jackson `@type` discriminator, so the property name given in `@JsonTypeInfo(property = "...")` was ignored and the generated TypeScript declared a property that the server never sends. The parser now resolves the name from the annotation and publishes it in the OpenAPI document as the `discriminator` of the union schema, so that the TypeScript generator uses it instead of a hardcoded `@type`. The source of the union type is now fully replaced rather than appended to, as the import that the backbone plugin leaves in it refers to a type that the union type does not use. Fixes #3428
`@JsonSubTypes` was only looked for on the direct superclass, so a subtype
deeper in the hierarchy got no discriminator, and neither did a class that
lists itself among its own subtypes.
The lookup now starts at the class itself and walks up its superclasses,
and the discriminator is added to a plain object schema as well, which is
what a class that declares the subtypes has.
Pinning the discriminator to a string literal in a type that is also the
supertype of another subtype would make the generated code invalid: the
subtype narrows it to a different literal, so its interface would not
extend the supertype and its model would not satisfy the type parameter
constraint of the parent model. Such types keep the discriminator out of
their interface, and the union type pins it for them instead, e. g.
type NotificationUnion = (Notification & { kind: "plain" })
| (EmailNotification & { kind: "email" })
| HtmlEmailNotification;
The lookup for such a supertype follows the whole reference chain, so a
class that is not a subtype itself does not hide it, and the import that
the removed discriminator was the only user of is dropped from the model.
The generated sources are now type-checked in the tests, together with an
exhaustive switch over each union type, which covers both the compilation
of the whole hierarchy and the narrowing it is meant to enable.
A sealed interface with `@JsonSubTypes` is a common way to model a closed hierarchy, but its implementations got no discriminator, as only the superclasses of a class were searched for the annotations. The lookup now covers its interfaces as well. The implementations do not extend the interface in the generated TypeScript, as the parser does not model an interface as a supertype, so they are plain object schemas that each carry their own discriminator.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6144 +/- ##
=======================================
Coverage 86.27% 86.27%
=======================================
Files 129 129
Lines 8875 8875
Branches 1369 1369
=======================================
Hits 7657 7657
Misses 1194 1194
Partials 24 24
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The two branches this one was built on are in `main` now, so the merge only keeps the interface lookup on top of them. The subtypes test case moved from the hand-written `openapi.json` to the generated TypeScript snapshots, so the `Shape` hierarchy is verified there instead, and the input of the TypeScript plugin test is regenerated to carry the discriminator enum that `main` added.
|
Both #6142 and #6155 are in
Two things worth knowing about the resolution, since #6146 also landed in between:
|
The snapshots of the `Shape` hierarchy were still under the package of the test case, which #6156 leaves out of the path, so the seven of them were expected where nothing is generated any more. Pure moves, no content changed.
|



A sealed interface annotated with
@JsonTypeInfo/@JsonSubTypesis a common way to model a closed hierarchy, but its implementations got no discriminator property in the generated TypeScript:SubTypesPluginonly walked the superclasses of a class when looking for the annotations, so an interface supertype was never inspected.What changed
SubTypesPlugin.findSubTypesInfonow traverses the whole type hierarchy instead of the superclass chain only. It does a breadth-first walk from the class itself over both its superclass and its interfaces, with a visited set so the diamonds that interfaces introduce are not searched twice.now generates the discriminator on each implementation:
The implementations do not
extendthe interface in the generated TypeScript, because the parser does not model an interface as a supertype — they are plain object schemas that each carry their own discriminator, and the union type ties them together.Tests
Shape/Circle/Squarehierarchy plus asendShape()endpoint method added to the parser plugin test case, with snapshots for the generated interfaces, models and union type.SubTypes.jsoninput and fixtures forShapeUnion,CircleandCircleModel, and the type-check usage now includes an exhaustiveswitchoverShapeUnionnarrowing onshape.shape, so the narrowing this fix enables is verified by the compiler.No public or protected API changes — the fix is confined to a private lookup method.