Skip to content

fix: find JSON subtypes on the interfaces of a class too - #6144

Merged
Artur- merged 10 commits into
mainfrom
fix/json-subtypes-interface
Sep 6, 2026
Merged

Artur- merged 10 commits into
mainfrom
fix/json-subtypes-interface

Conversation

@totally-not-ai

@totally-not-ai totally-not-ai Bot commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

A sealed interface annotated with @JsonTypeInfo / @JsonSubTypes is a common way to model a closed hierarchy, but its implementations got no discriminator property in the generated TypeScript: SubTypesPlugin only walked the superclasses of a class when looking for the annotations, so an interface supertype was never inspected.

What changed

SubTypesPlugin.findSubTypesInfo now 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.

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "shape")
@JsonSubTypes({ @JsonSubTypes.Type(value = Circle.class, name = "circle"),
        @JsonSubTypes.Type(value = Square.class, name = "square") })
public sealed interface Shape permits Circle, Square {
}

public record Circle(double radius) implements Shape {
}

now generates the discriminator on each implementation:

interface Circle {
    radius: number;
    shape: "circle";
}

type ShapeUnion = Circle | Square;

The implementations do not extend the 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

  • A Shape / Circle / Square hierarchy plus a sendShape() endpoint method added to the parser plugin test case, with snapshots for the generated interfaces, models and union type.
  • The TypeScript plugin test gets the regenerated SubTypes.json input and fixtures for ShapeUnion, Circle and CircleModel, and the type-check usage now includes an exhaustive switch over ShapeUnion narrowing on shape.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.

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.
@totally-not-ai
totally-not-ai Bot changed the base branch from fix/json-subtypes-hierarchy to main September 3, 2026 07:10
@totally-not-ai totally-not-ai Bot closed this Sep 3, 2026
@totally-not-ai totally-not-ai Bot reopened this Sep 3, 2026
@codecov

codecov Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.27%. Comparing base (c036b34) to head (745dcd5).

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           
Flag Coverage Δ
unittests 86.27% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@platosha platosha 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.

#6143 was superceded by #6155 as far as I see. Rebase this one on the latter.

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.
@totally-not-ai

totally-not-ai Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

@platosha This is done — main was merged in as 352b620, after the review was submitted.

Both #6142 and #6155 are in main now, so their commits are gone from this diff and the net change against main is only the interface lookup:

  • findSubTypesInfo walks the interfaces of a class besides its superclasses
  • the sealed interface Shape test hierarchy, its generated TypeScript snapshots, and the exhaustive switch over ShapeUnion

Two things worth knowing about the resolution, since #6146 also landed in between:

  • The subtypes case no longer has an openapi.json, so Shape is verified through the generated TypeScript snapshots instead; those were (re)created with -Dhilla.test.updateSnapshots and only the subtypes case changed.
  • Git auto-merge quietly duplicated five schema blocks in the TypeScript plugin fixture SubTypes.json with their pre-fix: find JSON subtypes anywhere in the class hierarchy #6155 copies. That fixture is rebuilt from the real parser output, so Circle and Square carry the discriminator enum that fix: find JSON subtypes anywhere in the class hierarchy #6155 added, and the rest of the file is identical to main.

mvn test on hilla-typescript-generator is 604/604 and the generator-plugin-subtypes suite is 7/7 locally. The PR only shows as blocked by the changes-requested review — a re-review would clear it.

Artur- and others added 3 commits September 3, 2026 20:25
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.
@Artur-
Artur- enabled auto-merge (squash) September 6, 2026 08:37
@Artur-
Artur- dismissed platosha’s stale review September 6, 2026 08:37

Rebased as requested

@sonarqubecloud

sonarqubecloud Bot commented Sep 6, 2026

Copy link
Copy Markdown

@Artur-
Artur- merged commit 73667bc into main Sep 6, 2026
13 checks passed
@Artur-
Artur- deleted the fix/json-subtypes-interface branch September 6, 2026 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants