Skip to content

JsonTypeInfo with defaultImpl is generated as required property #480

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

Closed
danielkaneider opened this issue May 12, 2020 · 2 comments
Closed

Comments

@danielkaneider
Copy link
Contributor

Jackson allows to specify a JsonTypeInfo.defaultImpl on an interface. If such annotation is present, the client doesn't need to specify @class/@type in the JSON object when pushing data.

Therefore, it would also be expected that the TS generates such property as optional, in the case a defaultImpl was specified.

@danielkaneider danielkaneider changed the title JsonTypeInfo.defaultImpl is generated as required property JsonTypeInfo with defaultImpl is generated as required property May 12, 2020
@vojtechhabarta
Copy link
Owner

I tried it, here is my example:

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "type",
    defaultImpl = MyClass1.class)
@JsonSubTypes({
    @JsonSubTypes.Type(value = MyClass1.class, name = "class1"),
    @JsonSubTypes.Type(value = MyClass2.class, name = "class2"),
})
interface MyInterface {
    public String getText();
}

class MyClass1 implements MyInterface {
    public String getText() {
        return "text1";
    }
}

class MyClass2 implements MyInterface {
    public String getText() {
        return "text2";
    }
}

When I deserialize JSON the discriminant property is really optional:
new ObjectMapper().readValue("{}", MyInterface.class).getText() returns text1.

But when instance of MyInterface implementation is serialized type property is always there:
new ObjectMapper().writeValueAsString(new MyClass1())
produces: {"type":"class1","text":"text1"}.

So in one direction type property can be undefined but in other direction it cannot be undefined. BTW this is not the first time when contract defined by one class is slightly different depending on the direction. I was thinking about some mechanism how to distinguish the direction and generate proper contract but it is complicated especially when single class is used in both directions.

I agree that it would be slightly better to generate this property as optional because it is easier in TypeScript to handle property marked as optional when it isn't than to handle required property when it isn't. But since none of these cases are 100% correct I would leave it as it is now because of backwards compatibility.

@olegshtch
Copy link
Contributor

I suppose properties those type depends on direction could work with microsoft/TypeScript#43662

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

No branches or pull requests

3 participants