On Mon, Sep 30, 2019 at 1:11 AM Marc Dzaebel <mdzae...@gmail.com> wrote:

> Tatu, thanks for your persistent help! If my program is not correct, could
> you post a sample (like in your new blog post) that simultaneously
> configures an ObjectMapper to:
>
>    - use JsonTypeInfo (without annotations) for certain types and their
>    subclasses to disable polymorphic serialisation (if I use default typing)
>    - only use certain types and their subclasses to be serialized and
>    deserialized polymorphically (with class attribute) even to Object
>    target/generic types
>    - don't serialize Collections/Maps polymophically (only certain types)
>    - All instances of the resulting graph should be of same class as in
>    the original graph (according to config)
>
> Thanks, Marc
>
>
If I had unlimited time, I'd definitely do that, but to be honest I am not
sure this is very common use case... there are so many things to document.

But I can help with specific issue here.


> Following is an other approach, where Test0 should not be
> serialized/deserialized polymorphically, but it serializes Test0 which
> leads to error (if @JsonTypeInfo is not set):
>
> import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
> import com.fasterxml.jackson.databind.ObjectMapper;
> import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
> import com.fasterxml.jackson.databind.SerializationFeature;
> import com.fasterxml.jackson.databind.json.JsonMapper;
> import
> com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
> import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
>
> public class Main4 {
> //    @JsonTypeInfo(use = Id.NONE)     // with this, it works
>

Yes, `@JsonTypeInfo` override works: Default Typing is more general rule,
annotation can override it.


>     static class Test0  { public Object test1; }
>     static class Test1  { public Object exception; }
>     public static void main(String[] args) throws java.io.IOException,
> ClassNotFoundException {
>         Test0 test0 = new Test0();
>         Test1 test = new Test1();
>         test.exception=new Exception("Test");
>         test0.test1=test;
>         PolymorphicTypeValidator ptv =
> BasicPolymorphicTypeValidator.builder().allowIfSubType(Test1.class).allowIfSubType(Exception.class).build();
>

Note that PTV is concerned with security aspect: is given type ever allowed
to be handled via polymorphic deserialization or not. It is not used or
relevant for question of whether Default Typing is enabled. While from user
perspective these are related, from Jackson perspective these are
orthogonal concepts.

So inclusion (and thereby expectation) of Polymorphic handling is simply
driven by this:


>         ObjectMapper mapper =
> JsonMapper.builder().activateDefaultTyping(ptv,
>                 DefaultTyping.NON_FINAL, As.PROPERTY).disable(
>

  ^^^ DefaultTyping.NON_FINAL

and `Test0` not being `final`, type id is written and is expected. You
could add `final` in declaration to prevent that, or `@JsonTypeInfo`. Or,
possible, change to use `DefaultTyping.JAVA_LANG_OBJECT`.

Or perhaps change `test1` and `exception` fields to have `@JsonTypeInfo`
instead of DefaultTyping.

-+ 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 view this discussion on the web visit 
https://groups.google.com/d/msgid/jackson-user/CAGrxA26HTybg-FhidxCQfFZda_Jx-i-08vgFrpWqbx%2B8nJhMew%40mail.gmail.com.

Reply via email to