Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
FatalCatharsis opened this issue Apr 17, 2025 · 0 comments · May be fixed by #3429
Open
Labels
bug Something isn't working hilla Issues related to Hilla

Comments

@FatalCatharsis
Copy link

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

@FatalCatharsis FatalCatharsis added bug Something isn't working hilla Issues related to Hilla labels Apr 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working hilla Issues related to Hilla
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant