Skip to content
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

Jackson2: Super class did not get type information #497

Merged

Conversation

michaelzangl
Copy link
Contributor

@michaelzangl michaelzangl commented Jun 29, 2020

Jackson can (de)serialize instances of the hierarchy root class when using @JsonSubTypes. When generating interface for this java class:

    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
    @JsonSubTypes(@JsonSubTypes.Type(NonAbstractRootSub.class))
    @JsonTypeName("rootType")
    public static class NonAbstractRoot {
    }

    @JsonTypeName("subType")
    public static class NonAbstractRootSub extends NonAbstractRoot {
    }

Previously wrong generated code ("rootType" missing):

interface NonAbstractRoot {
    type: "subType";
}

interface NonAbstractRootSub extends NonAbstractRoot {
    type: "subType";
}

type NonAbstractRootUnion = NonAbstractRootSub;

Correct:

interface NonAbstractRoot {
    type: "rootType" | "subType";
}

interface NonAbstractRootSub extends NonAbstractRoot {
    type: "subType";
}

type NonAbstractRootUnion = NonAbstractRootSub;

@michaelzangl michaelzangl force-pushed the fix-polymorphic-root-type-name branch from 688010a to dcba663 Compare June 29, 2020 13:45
@vojtechhabarta
Copy link
Owner

Thanks for your pull request with good example.

While this fixes typescript-generator inconsistency with Jackson it can break some users code. I am not sure how frequent this problem could be but I think we need to change this sooner the better.

For users:

make root class abstract if you don't want discriminant value for this class to be included in union

@vojtechhabarta vojtechhabarta merged commit 8e91cd7 into vojtechhabarta:master Jul 7, 2020
@vojtechhabarta vojtechhabarta added this to the 2.25 milestone Aug 5, 2020
@vojtechhabarta
Copy link
Owner

Released in v2.25.695.

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