When using type id with `As.EXTERNAL_PROPERTY` together with @JsonValue inside type the serialiser omits external type id field from result when @JsonValue value is null
See original GitHub issueDescribe the bug
When using type id with As.EXTERNAL_PROPERTY
together with @JsonValue inside type the serialiser omits external type id field from result when @JsonValue value is null.
Version information 2.13.3
To Reproduce
package org.example;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static interface GenericType {
String getValue();
void setValue(String value);
}
public static abstract class AbstractGenericType implements GenericType
{
protected String value;
public AbstractGenericType() {}
public AbstractGenericType(String value) {this.value = value;}
@Override @JsonValue public String getValue() {return value;}
@Override @JsonValue public void setValue(String value) {this.value = value;}
}
public static class FooType extends AbstractGenericType {
public FooType() {super();}
public FooType(String value) {super(value);}
}
public static class Container {
@JsonTypeInfo(use = Id.MINIMAL_CLASS, include = As.EXTERNAL_PROPERTY, property = "type", visible = true)
@JsonSubTypes(value = {@Type(value = FooType.class)})
protected GenericType value;
public GenericType getValue() { return value; }
public void setValue(GenericType value) { this.value = value; }
}
public static void main(String[] args) throws JsonProcessingException {
var om = new ObjectMapper();
var container = new Container();
System.out.println(om.writeValueAsString(container)); // {"value":null}
// Its fine as container::value == null so external type id field is missing
container.setValue(new FooType());
System.out.println(om.writeValueAsString(container)); // {"value":null}
// Its not fine as container::value != null, external type id field should be set ".Main$FooType" value
container.setValue(new FooType("foobar"));
System.out.println(om.writeValueAsString(container)); // {"value":"foobar","type":".Main$FooType"}
// Now external type id field is present
}
}
Expected behavior
Expected that external type id field should be present with type id value when type is not null.
P.S. It would be also great if it was possible to still include external type id field with for e.g. null
value when type is null.
Issue Analytics
- State:
- Created a year ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Jackson JsonTypeInfo.As.EXTERNAL_PROPERTY doesn't ...
So, you have to move all annotation from Info class to property info or setInfo method in Response class. @JsonTypeInfo(use = JsonTypeInfo.Id.
Read more >Issues · FasterXML/jackson-databind · GitHub
When using type id with As.EXTERNAL_PROPERTY together with @JsonValue inside type the serialiser omits external type id field from result when @JsonValue ......
Read more >JsonTypeInfo.java - Android Code Search
Means that logical type name is used as type information; name will then need. * to be separately resolved to actual concrete type...
Read more >Index (jackson-databind 2.9.0 API) - FasterXML
Method called after all external properties have been assigned, to further link property with polymorphic value with possible property for type id itself....
Read more >Using @JsonTypeInfo annotation to handle polymorphic types
In cases where polymorphic types are persisted to JSON, there's no way for Jackson to figure out the right type during deserialization. Let's ......
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 FreeTop 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
Top GitHub Comments
Oh maybe important tip is that
@JsonValue
is not on generic type, but a property inside the type. In my screenshot it’sTextValueType
has get/set value with@JsonValue
attribute.@aurimasniekis np at all. I know how limited time can be. Glad that you were able to figure out a work-around at least; that sounds like a good approach to solve your immediate needs fwtw.