Skip to content

JsonTypeInfo.As.PROPERTY type id, property with same name, result in duplicate JSON property #2022

@marcelstoer

Description

@marcelstoer

Origin: https://stackoverflow.com/q/50163882/131929

The Swagger Java codegen produces a class like this:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "errorType", visible = true)
@JsonSubTypes({
    @JsonSubTypes.Type(value = SpecificError.class, name = "SpecificError"),
})
public class GeneralError {
    @JsonProperty("errorType")
    private String errorType = null;
    // accessors, even for errorType!, follow here

The Jackson serializer, version 2.9.4, produces JSON that includes the errorType property twice:

{"errorType":"SpecificError","message":"message","errorType":null}

My test bed is as follows:

SpecificError specificError = (SpecificError) new SpecificError().message("message")
ObjectMapper objectMapper = new ObjectMapper();
ObjectWriter writer = objectMapper.writer();
System.out.println(writer.writeValueAsString(clientError));

I'm not 100% sure if Jackson is supposed to behave like that given the annotations you see above. Based on what I read in the JsonTypeInfo Javadoc it seems incorrect. However, I also raised this as swagger-api/swagger-codegen#8137.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions