Skip to content

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

@cowtowncoder

Description

@cowtowncoder

(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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions