Skip to content

JsonProperty.Access.READ_ONLY fails with collections when a property name is specified #2283

@Yona-Appletree

Description

@Yona-Appletree

Very similar issue to #1805, but when a property name is specified and MapperFeature.USE_GETTERS_AS_SETTERS is enabled:

static class Foo {
    @JsonProperty(value = "list", access = JsonProperty.Access.READ_ONLY)
    List<Long> getList() {
        return Collections.unmodifiableList(Arrays.asList());
    }
}

Fails with com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.UnsupportedOperationException) (through reference chain: com.fasterxml.jackson.databind.deser.ReadOnlyDeserWithNameTest$Foo["list"]->java.util.Collections$UnmodifiableRandomAccessList[0])

Here's the test:

private final ObjectMapper MAPPER = jsonMapperBuilder().configure(MapperFeature.USE_GETTERS_AS_SETTERS, true).build();

public void testReadOnly() throws Exception
{
    String payload = "{\"list\":[1,2,3,4]}";
    Foo foo = MAPPER.readValue(payload, Foo.class);
    assertTrue("List should be empty", foo.getList().isEmpty());
}

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