Skip to content

2.15.0 breaking behaviour change for records and Getter Visibility #3895

@matteobertozzi

Description

@matteobertozzi

In 2.15 trying to encode records with PropertyAccessor.GETTER = Visibility.NONE will result in an empty object {}.

record DataObject (int a, int b) {}

@Test
public void testRecord() throws Exception {
    final ObjectMapper jsonMapper = new JsonMapper();
    final String r = jsonMapper.writeValueAsString(new DataObject(2, 3));
    jsonMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    jsonMapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE);
    Assertions.assertEquals("{\"a\":2,\"b\":3}", r); // we get {}
}

to resolve that we can set PropertyAccessor.GETTER = Visibility.DEFAULT or others, but that will break every single class serialization that does not have annotations on getter methods.

public class Foo {
    private final int x = 2;
    public int getComputedValue() { return 11; }
}

jsonMapper.writeValueAsString(new Foo()) // { x: 2, computedValue: 11 }

The behaviour in 2.14 with PropertyAccessor.GETTER = Visibility.NONE is the expected one, with records able to serialize all fields

Is this change something done on purpose? are there alternatives to set the visibility of getters and annotate every class method?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions