Skip to content

Getter that returns an abstract collection breaks a delegating @JsonCreator #2251

@2is10

Description

@2is10

Using version 2.9.2 or 2.9.8 (and probably many other versions), the following test throws:

JsonMappingException: Cannot find a deserializer for non-concrete Collection type [collection type; class java.util.AbstractSet, contains [simple type, class java.lang.Integer]]

    @Test
    public void testDeserializeValSet() throws IOException {
        new ObjectMapper().readValue("[1,2,3]", ValSet.class);
    }

    static class ValSet {
        @JsonValue
        private final AbstractSet<Integer> vals;

        @JsonCreator(mode = Mode.DELEGATING)
        ValSet(Collection<Integer> vals) {
            this.vals = new HashSet<>(vals);
        }

        public AbstractSet<Integer> getVals() {
            return vals;
        }
    }

This is a spurious error since the @JsonCreator constructor only requires Collection<Integer>, not AbstractSet<Integer>.

Some workarounds:

  • Delete getVals().
  • Make getVals() non-public.
  • Move @JsonValue to getVals().
  • Add @JsonIgnore to getVals().
  • Add @JsonAutoDetect(getterVisibility = Visibility.NONE) to ValSet.

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