Skip to content

Serialization for JsonFormat.Shape.ARRAY does not work when there is @JsonAnyGetter #4961

Closed
@JooHyukKim

Description

@JooHyukKim

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

While working on #4388 , found some unexpected behavior.
Behavior as title.
Will file a reproductino PR

Version Information

Tested and does not work on...

  • 2.18.2
  • 2.19

Reproduction

public class ShapeArrayWithAnyGetterTest
    extends DatabindTestUtil
{

    static class WrapperForAnyGetter {
        public BeanWithAnyGetter value;
    }

    @JsonPropertyOrder({ "firstProperty", "secondProperties", "anyProperty", "forthProperty" })
    @JsonFormat(shape = JsonFormat.Shape.ARRAY)
    static class BeanWithAnyGetter {
        public String firstProperty = "first";
        public String secondProperties = "second";
        public String forthProperty = "forth";
        @JsonAnyGetter
        public Map<String, String> getAnyProperty() {
            Map<String, String> map = new TreeMap<>();
            map.put("third_A", "third_A");
            map.put("third_B", "third_B");
            return map;
        }
    }

    final ObjectMapper MAPPER = newJsonMapper();

    @Test
    public void testSerializeArrayWithAnyGetterWithWrapper() throws Exception {
        WrapperForAnyGetter wrapper = new WrapperForAnyGetter();
        wrapper.value = new BeanWithAnyGetter();

        String json = MAPPER.writeValueAsString(wrapper);

        // Fails Actual   :{"value":{"firstProperty":"first","secondProperties":"second","forthProperty":"forth","third_A":"third_A","third_B":"third_B"}}
        assertEquals(a2q("{'value':" +
                "[" +
                "'first'," +
                "'second'," +
                "'forth'," +
                "'third_A'," +
                "'third_B'" +
                "]" +
                "}"), json);
    }

    @Test
    public void testSerializeArrayWithAnyGetterAsRoot() throws Exception {
        BeanWithAnyGetter bean = new BeanWithAnyGetter();

        String json = MAPPER.writeValueAsString(bean);

        // Fails Actual : {"firstProperty":"first","secondProperties":"second","forthProperty":"forth","third_A":"third_A","third_B":"third_B"}
        assertEquals(a2q("[" +
                "'first'," +
                "'second'," +
                "'forth'," +
                "'third_A'," +
                "'third_B'" +
                "]"), json);
    }
}

Expected behavior

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.19Issues planned at 2.19 or later

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions