JsonSubTypes doesn't work with mixin

23 views
Skip to first unread message

Konstantin Klimov

unread,
Jul 6, 2019, 4:20:14 AM7/6/19
to jackson-user
Dear jackson community,

I'm trying to use @JsonTypeInfo  and @JsonSubTypes I in a mixin to create polymorphic type mapping for third-party classes. But it looks like the annotations do not work with mixin. Could anybody please drop a hint how to deal with it or find a workaround? 


class Animal { }

class Cat extends Animal { }

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({@JsonSubTypes.Type(value = Cat.class, name = "cat")})
abstract class SubTypesMixin { }


@Test
public void mixinsShouldWorkWithSubtypesAnnotations() throws IOException {
// given
String json = "{\"type\":\"cat\"}";
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.addMixIn(Animal.class, SubTypesMixin.class);

// when
Animal animal = new ObjectMapper()
.readerFor(Animal.class)
.readValue(json);

// then
assertEquals(animal.getClass(), Cat.class);
}

Thank you in advance,
Regards

Tatu Saloranta

unread,
Jul 18, 2019, 1:27:30 AM7/18/19
to jackson-user
I see this was files as

https://github.com/FasterXML/jackson-databind/issues/2379

and the comment there is that I think your code has flaw -- you are
configuring an `ObjectMapper` correctly, but then for some reason
using a fresh new unconfigured one for operation. That would explain
the problem?

-+ Tatu +-
Reply all
Reply to author
Forward
0 new messages