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

Should fail on trying to serialize unresolvable polymorphic subtype (not just on deserialization) #436

Open
galan opened this issue Apr 9, 2014 · 5 comments

Comments

@galan
Copy link

galan commented Apr 9, 2014

When I annotate a superclass with @JsonTypeInfo and @JsonSubTypes I do not expect that subclasses are serialized in a differnt manner. Therefor if other non-defined types are serialized I expect an exception. Example:

I have a superclass with the following annotations:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.PROPERTY, property = "_name")
@JsonSubTypes({@Type(value = Lion.class, name = "lion"), @Type(value = Elephant.class, name = "elephant")})
public abstract class Animal {...}

And a class using a collection that stores Animals:

public class Zoo {
    List<Animal> animals;
}

This works fine as long no new subclasses are used, example output:

{"animals":[{"_name":"lion"},{"_name":"elephant"}]}

When another type that isn't annotated is used I don't get an exception on serialization but on deserialization - which is to late.

Instead of getting an exception the class name was used (which wasn't configured and therefore expected).
Maybe there is an option to configure Jackson to throw an exception when the type wasn't registered before, but was missed by me.

Serailized example output:

{"animals":[{"_name":"lion"},{"_name":"elephant"},{"_name":"Turtle"}]}

How I serialize:

List<Animal> animals = Lists.newArrayList(lion, elephant);
Zoo zoo = new Zoo(animals);
ObjectMapper mapper = new ObjectMapper();
String zooString = mapper.writeValueAsString(zoo);

How I deserialze:

Zoo zooDeserialized = mapper.readValue(zooString, Zoo.class);

Exception I get (on deserialization):

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Could not resolve type id 'Turtle' into a subtype of [simple type, class mypackage.Animal]
 at [Source: N/A; line: -1, column: -1] (through reference chain: mypackage.Zoo["animals"])
 at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
..
@galan
Copy link
Author

galan commented Apr 9, 2014

Addition: There is a defaultImpl value you can set on the @JsonTypeInfo Annotation, if I set it to JsonTypeInfo.None.class if should throw an error for unmappable types according to the documentation:

None means "there is no default implementation" (in which case an error results from unmappable type)

But the behaviour from above did not changed.

@cowtowncoder
Copy link
Member

There is no annotation currently to force failure on this case, partly because there are use cases where such serialization would work: if user configured mapper with missing types, for example; or if deserialization is handled using some other mechanism than Jackson.

But I agree in that having a feature that would explicitly add checks to prevent such risky cases would be useful. I don't remember whether enough information is available to make such decision, but I hope there is; and if so, adding a check should be straight-forward.

On defaultImpl: comment refers to deserialization, not serialization; it should be clarified.

@galan
Copy link
Author

galan commented Apr 10, 2014

Adding a check would be great. Usually I would try to create a patch, but I have to admit that I don't have a deeper understanding of the jackson libraries, sorry. To be backward compatible a new annotation would help, otherwise different options for defaultImpl might be a good fit, too. (from my user perspective)

@cowtowncoder cowtowncoder changed the title Serialization on annotated polymorph types not as expected Serialization on annotated polymorph types not as expected: should fail on unknown subtype Jun 9, 2015
@cowtowncoder cowtowncoder changed the title Serialization on annotated polymorph types not as expected: should fail on unknown subtype Should fail on trying to serialize unresolvable polymorphic subtype (not just on deserialization) Mar 16, 2017
@FcoJavierSainz
Copy link

Is this on the Roadmap?

@cowtowncoder
Copy link
Member

I haven't hard time to work on this and I am not aware of anyone else working on it.

@cowtowncoder cowtowncoder added 2.11 and removed 2.9 labels Sep 12, 2019
@cowtowncoder cowtowncoder removed the 2.11 label Apr 12, 2020
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

3 participants