Skip to content

Property name on JsonTypeInfo not used and type hierarchies don't generate the type field #3428

@FatalCatharsis

Description

@FatalCatharsis

Describe the bug

For polymoprhic types annotated with @JsonSubTypes, there are a couple issues. First, when you specify a different property name for the discriminator on the root type, it does not utilize that property name on the generated typescript. Second, it does not apply a type guard to the root type if it appears in the subtypes list. Third, if one of the subtypes is deeper in the hierachy, it does not apply a type guard.

Expected-behavior

When it generates typescript interfaces representing the polymorphic type on a return or parameter field of a browser callable, the type discriminator should use the value defined in @JsonTypeInfo(property = "..."). Currently, it is always using @type, which is the default for Jackson.

Also, when you use the base class as one of the sub types, it doesn't place the type guard property field.

Lastly, when there is an inheritance hierarchy, the sub type interfaces do not have the type guard on them.

Reproduction

With this example browser callable:

@Endpoint
public class TestService {
    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "valueType")
    @JsonSubTypes({
        @JsonSubTypes.Type(value = MyTest.class, name = "base"),
        @JsonSubTypes.Type(value = DerivedTest.class, name = "derived"),
        @JsonSubTypes.Type(value = DeeperTest.class, name = "deeper")
    })
    public static class MyTest {
        public Long foo;
    }
    
    public static class DerivedTest extends MyTest {
        public Long bar;
    }

    public static class DeeperTest extends DerivedTest {
        public Long meh;
    }

    public MyTest example() {
        return new MyTest();
    }
}

This generates:

interface MyTest {
    foo?: number
}
export default MyTest

which should've included the property valueType : "base"

import type MyTest_1 from "./MyTest.js";
interface DerivedTest extends MyTest {
    bar?: number;
    "@type": "derived"
}
export default DerivedTest

where @type should've been valueType

import type DerivedTest_1 from "./DerivedTest.js"
interface DeeperTest extends DerivedTest_1 {
    meh?: number;
}
export default DeeperTest

which should've included property valueType: "deeper"

System Info

Windows 10, Vaadin 24.7.2, Chrome 135.0.7049.85

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghillaIssues related to Hilla

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions