Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
JooHyukKim opened this issue Feb 8, 2025 · 3 comments
Closed
1 task done
Labels
2.19 Issues planned at 2.19 or later

Comments

@JooHyukKim
Copy link
Member

JooHyukKim commented Feb 8, 2025

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

@JooHyukKim JooHyukKim added the to-evaluate Issue that has been received but not yet evaluated label Feb 8, 2025
JooHyukKim added a commit to JooHyukKim/jackson-databind that referenced this issue Feb 8, 2025
@JooHyukKim
Copy link
Member Author

@cowtowncoder I think we can consider as fixed also via #4775?

@cowtowncoder
Copy link
Member

There seem to be failing tests in

src/test/java/com/fasterxml/jackson/databind/tofix/ShapeArrayWithAnyGetter4961Test.java 

?

@cowtowncoder
Copy link
Member

Ok I see changed test: yes, will mark as fixed.

@cowtowncoder cowtowncoder added 2.19 Issues planned at 2.19 or later and removed to-evaluate Issue that has been received but not yet evaluated labels Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.19 Issues planned at 2.19 or later
Projects
None yet
Development

No branches or pull requests

2 participants