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

Problem with @JsonTypeInfo.As.EXTERNAL_PROPERTY, defaultImpl, missing type id, NPE #1198

Closed
cowtowncoder opened this issue Apr 14, 2016 · 4 comments
Milestone

Comments

@cowtowncoder
Copy link
Member

(as reported by Joe O on mailing list)


Looks like one of following test methods fails:

public class JsonTypeInfoTest extends BaseMapTest
{
    public enum Attacks { KICK, PUNCH }

    static class Character {
        public String name;
        public Attacks preferredAttack;

        @JsonTypeInfo(use=JsonTypeInfo.Id.NAME, defaultImpl=Kick.class,
                include=JsonTypeInfo.As.EXTERNAL_PROPERTY, property="preferredAttack")
        @JsonSubTypes({
            @JsonSubTypes.Type(value=Kick.class, name="KICK"),
            @JsonSubTypes.Type(value=Punch.class, name="PUNCH")
        })
        public Attack attack;
    }

    public static abstract class Attack {
        public String side;

        @JsonCreator
        public Attack(String side) {
            this.side = side;
        }
    }

    public static class Kick extends Attack {
      @JsonCreator
      public Kick(String side) {
        super(side);
      }
    }

    public static class Punch extends Attack {
      @JsonCreator
      public Punch(String side) {
        super(side);
      }
    }

    final ObjectMapper MAPPER = new ObjectMapper();

    public void testFails() throws Exception {
      String json = "{ \"name\": \"foo\", \"attack\":\"right\" } }";

      Character character = MAPPER.readValue(json, Character.class);

      assertNotNull(character);
      assertNotNull(character.attack);
      assertEquals("foo", character.name);
    }

    public void testWorks() throws Exception {
      String json = "{ \"name\": \"foo\", \"preferredAttack\": \"KICK\", \"attack\":\"right\" } }";

      Character character = MAPPER.readValue(json, Character.class);

      assertNotNull(character);
      assertNotNull(character.attack);
      assertEquals("foo", character.name);
    }
  }

with a NullPointerException (with 2.7.3, current master)

@cowtowncoder
Copy link
Member Author

Ok, so the underlying issue is related to couple of things; and combination of missing type id (needing to use defaultImpl), along with special handling of "natural" types (of which String, used here as JSON value to bind is one) results in trouble. null specifically just refers to missing Map for resolving Class to "type id" (since it's not usually needed for deserialization, only serialization).
But it probably goes deeper than that. Will keep digging.

@cowtowncoder cowtowncoder changed the title Problem with @JsonTypeInfo.As.EXTERNAL_PROPERTY, NPE Problem with @JsonTypeInfo.As.EXTERNAL_PROPERTY, defaultImpl, missing type id, NPE Apr 14, 2016
@cowtowncoder cowtowncoder added this to the 2.7.0 milestone Apr 14, 2016
@cowtowncoder
Copy link
Member Author

Managed to fix this so that tests pass. Fix will be in 2.7.4.

@joeo73
Copy link

joeo73 commented Apr 14, 2016

Thanks. In the short term, can you recommend a work around?

@cowtowncoder
Copy link
Member Author

@joeo73 unfortunately the only thing I can think of would be ensuring that the type id is not missing.

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

2 participants