question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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 https://github.com/swagger-api/swagger-codegen/issues/8137.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Jul 31, 2022

@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 of EXISTING_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.

0reactions
nickshoecommented, Jul 29, 2022

@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 the visible attribute is false, then PROPERTY works as expected.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found