On Fri, Jul 20, 2018 at 5:06 AM, Michal Aron <aron...@gmail.com> wrote: > Hey, > > May somebody please have a look at the following problem? > > It is about nested polymorphism into another object and it fails on > nullPointerException when it meets attribute using abstract class > declaration. > > In other words it fails to determine which concrete entity to use when > parent entity for that attribute is using abstract entity.Or it never does - > since it never enter the deserializer. > > Problem: > 1) I am acting as a proxy, getting requests from UI and forwarding them to > the other system. > 2) Before sending the response back to the UI I am mapping it to my local > DTO schema of that remote system – so we can have power over what we send to > the UI.. > 3) That system sends completely ambiguous data structure depending on their > count. Imagine entity object with attribute member. If that member is only > one, that attribute is object. If more than one, then that attribute is > array. > > So you see my point now.. I have 2 DTOs for this case – 1 is when member is > object, 2 is when member is array and they both inherit from abstract > parent. > > But this isnt all.. That schema I just described is wrapped into another > Wrapper object where I am using their abstract parent for the attribute > type. That wrapper is needed because that remote system returns 0 or 1 or >1 > objects in the very same attribute of that wrapper but of different type > also. So it looks like this: > > public class Wrapper { > @JsonProperty(“wrapper”) > private AbstractWrapper wrapper; > } > > public abstract class AbstractWrapper { > } > > public class WrapperString extends AbstractWrapper { > @JsonProperty(“dto”) > private String dto; > } > > public class WrapperObject extends AbstractWrapper{ > @JsonProperty(“dto”) > private AbstractDTO dto; > } > > public class WrapperList extends AbstractWrapper{ > @JsonProperty(“dto”) > private List<AbstractDTO> dto; > } > > public abstract class AbstractDTO { > } > > public class Impl1DTO extends AbstractDTO { > @JsonProperty(“member”) > private String member; > } > > public class Impl2DTO extends AbstractDTO { > @JsonProperty(“member”) > private List<String> member; > } > > > I have both deserializers for this registered. It will check if member is > type of Object or Array or String and determine the concrete class. This > works for the Wrapper Polymorphism. However it doesnt for nested > polymorphism. It never enters that deserializer no matter what I try.. And > when deserializing it fails on null pointer exception on > @JsonProperty(“dto”) > > Here is also more detailed stackoverflow thread I created for this. > > I will appreciate any inputs for this. Thanks, Michal
Although code is missing some of the relevant code (how is the deserializer registered f.ex), as well as actual stack trace (what is null?), I'd be guessing that this: return mapper.readValue(root.traverse(), variantClass); might be it. Specifically, `root.traverse()` creates `JsonParser`, but not one with codec assigned (where would it get one?). But I think there is another variant that takes `codec`, or, if not, simply call `setCodec()` on parser. And that would make sure you at least are not missing codec (ObjectMapper). But wait! There's more. Instead of relying on codec, much/most of the functionality is actually available via DeserializationContext! For example, it's possible to call `readValue()` there to read `JsonNode`, something like JsonNode tree = ctxt.readValue(p, JsonNode.class); and similarly for other types. This is more efficient way (it avoids setting up of context), and retains some more of contextual information (any attributes you may have set). -+ Tatu +- -- You received this message because you are subscribed to the Google Groups "jackson-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user+unsubscr...@googlegroups.com. To post to this group, send email to jackson-user@googlegroups.com. For more options, visit https://groups.google.com/d/optout.