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

Duplicate JSON property when @JsonTypeInfo.property is also regular bean property #392

Closed
vojtechhabarta opened this issue Jul 24, 2019 · 1 comment

Comments

@vojtechhabarta
Copy link
Owner

vojtechhabarta commented Jul 24, 2019

Consider following example which uses Jackson library:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "kind", include = JsonTypeInfo.As.PROPERTY)
@JsonSubTypes({
    @JsonSubTypes.Type(value = DuplicateKind1.class, name = "kind_1"),
    @JsonSubTypes.Type(value = DuplicateKind2.class, name = "kind_2"),
})
abstract class DuplicateKind {
    public String kind;
}
class DuplicateKind1 extends DuplicateKind {
}
class DuplicateKind2 extends DuplicateKind {
}

If instance is created and serialized using Jackson:

DuplicateKind1 object = new DuplicateKind1();
object.kind = "kind_invalid";
System.out.println(new ObjectMapper().writeValueAsString(object));

Jackson generates following invalid JSON:

{
    "kind": "kind_1",
    "kind": "kind_invalid"
}

where first kind property comes from @JsonTypeInfo annotation and second comes from DuplicateKind.kind field. (Tested with Jackson 2.9.7)

Here are relevant issues tracked in jackson-databind repository: FasterXML/jackson-databind#2022, FasterXML/jackson-databind#1410.

To resolve this issus either:

  • remove kind field (preferred)
  • change include = JsonTypeInfo.As.PROPERTY to EXISTING_PROPERTY

Tagged unions

Typescript-generator can generate tagged unions (#81) for Jackson polymorphic classes that uses @JsonTypeInfo annotation with include = JsonTypeInfo.As.PROPERTY.

New

Typescript-generator can now detect mentioned duplicate property issue and does not generate tagged unions for such classes. In addition to this it also logs warning.

@vojtechhabarta
Copy link
Owner Author

Change released in v2.16.538.

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

1 participant