`JsonTypeInfo.As.PROPERTY` type id, property with same name, result in duplicate JSON property
See original GitHub issueOrigin: 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 https://github.com/swagger-api/swagger-codegen/issues/8137.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:5
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Duplicate JSON Field with Jackson - java - Stack Overflow
@JsonTypeInfo tells Jackson to serialize the logical type name ( JsonTypeInfo.Id.NAME ) as a property ( JsonTypeInfo.As.PROPERTY ) with name ...
Read more >DeserializationFeature (jackson-databind 2.8.11 API)
Feature that determines what happens when a property annotated with JsonTypeInfo.As.EXTERNAL_PROPERTY is missing, but associated type id is available.
Read more >JsonTypeInfo.As (Jackson-annotations 2.10.0 API) - FasterXML
Inclusion mechanism that wraps typed JSON value (POJO serialized as JSON) in a JSON Object that has a single entry, where field name...
Read more >Duplicate field when serializing: bug or not a bug?
The JsonPatchOperation abstract class is the base class for all operations; it reads: ---- @JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property ...
Read more >Duplicate property names in JSON objects in Power Automate
In our data, unfortunately, some properties have identical names (Id and Name) even though their path is different. As it turns out, object...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@nickshoe Yes, I think you are right:
visible
is meant to “expose” type id metadata as a property. It’s bit of a work-around really, over the original case where type id was never exposed to other processing; and this before addition ofEXISTING_PROPERTY
. So the use case that was not covered originally (where type id maps to/from regular property) is supported by a combination of slightly cumbersome additions, as opposed to having been designed to work from the beginning.@cowtowncoder I was wrong in my last comment, that happens only if one uses
visible = true
in the@JsonTypeInfo
annotation. Since the default value of thevisible
attribute isfalse
, thenPROPERTY
works as expected.