Skip to content

Create Issue4752Test.java #4765

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
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions src/test/java/com/fasterxml/jackson/databind/ser/Issue4752Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.fasterxml.jackson.databind.ser;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
import org.junit.jupiter.api.Test;

import java.util.LinkedHashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue4752Test extends DatabindTestUtil {

private final ObjectMapper objectMapper = newJsonMapper();

@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.ANY,
getterVisibility = JsonAutoDetect.Visibility.NONE
)
static class FirstObject {

@JsonAnySetter
@JsonAnyGetter
private Map<String, Object> data = new LinkedHashMap<>();

public String getTransactionId() {
return (String) data.get("transactionId");
}
}

@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.ANY,
getterVisibility = JsonAutoDetect.Visibility.NONE
)
static class SecondObject {

private final String transactionId;
@JsonAnySetter
@JsonAnyGetter
private final Map<String, Object> data;


@JsonCreator
public SecondObject(@JsonProperty("transactionId") String transactionId) {
this.transactionId = transactionId;
this.data = new LinkedHashMap<>();
}

public String getTransactionId() {
return this.transactionId;
}

public Map<String, Object> getData() {
return this.data;
}
}

@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.ANY,
getterVisibility = JsonAutoDetect.Visibility.NONE
)
static class ThirdObject {

private final String transactionId;
@JsonAnySetter
@JsonAnyGetter
private final Map<String, Object> data;


@JsonCreator
public ThirdObject(
@JsonProperty("transactionId") String transactionId,
@JsonProperty("data") Map<String, Object> data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would adding @JsonAnySetter in here make any difference?

In fact, @JsonAnySetter SHOULD NOT be added to field in this case as the intent is to propagate any properties via constructor arguments.

And @JsonAnyGetter should be added on getData() similarly, not in field.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would adding @JsonAnySetter in here make any difference?

Do you mean put @JsonAnySetter near to @JsonProperty("data")?

In fact, @JsonAnySetter SHOULD NOT be added to field in this case as the intent is to propagate any properties via constructor arguments.
And @JsonAnyGetter should be added on getData() similarly, not in field.

Sounds reasonable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly. FWTW ideally usage as shown should also work, but trying to think of possible work arounds.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created class:

@JsonAutoDetect(
        fieldVisibility = JsonAutoDetect.Visibility.ANY,
        getterVisibility = JsonAutoDetect.Visibility.NONE
)
public class FourthObject {

    private final String transactionId;
    private final Map<String, Object> data;

    @JsonCreator
    public FourthObject(
            @JsonProperty("transactionId") String transactionId,
            @JsonAnySetter @JsonProperty("data") Map<String, Object> data
    ) {
        this.transactionId = transactionId;
        this.data = data;
    }

    public String getTransactionId() {
        return this.transactionId;
    }

    @JsonAnyGetter
    public Map<String, Object> getData() {
        return this.data;
    }
}

When running the test for jackson-databind 2.18.1-20241024.043825-21 I got:

org.opentest4j.AssertionFailedError: 

Expected:

{
  "b" : 2,
  "a" : 1,
  "transactionId" : "test",
  "c" : [ {
    "id" : "3",
    "value" : "c"
  }, {
    "id" : "1",
    "value" : "a"
  }, {
    "id" : "2",
    "value" : "b"
  } ]
}

Actual:

{
  "transactionId" : "test",
  "data" : {
    "a" : 1,
    "b" : 2,
    "c" : [ {
      "id" : "3",
      "value" : "c"
    }, {
      "id" : "1",
      "value" : "a"
    }, {
      "id" : "2",
      "value" : "b"
    } ]
  },
  "a" : 1,
  "b" : 2,
  "c" : [ {
    "id" : "3",
    "value" : "c"
  }, {
    "id" : "1",
    "value" : "a"
  }, {
    "id" : "2",
    "value" : "b"
  } ]
}

For 2.17.2 I got:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "b" (class com.arvgord.FourthObject), not marked as ignorable (2 known properties: "transactionId", "data"])
 at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 19, column: 6] (through reference chain: com.arvgord.FourthObject["b"])

) {
this.transactionId = transactionId;
this.data = data;
}

public String getTransactionId() {
return this.transactionId;
}

public Map<String, Object> getData() {
return this.data;
}
}

private final String INPUT_JSON = a2q("{'b': 2,'a': 1,'transactionId': 'test'," +
"'c': [{'id': '3','value': 'c'},{'id': '1','value': 'a'},{'id': '2','value': 'b'}]}");

private final String SECOND_UNEXPECTED_JSON_OUTPUT = a2q("{'transactionId': 'test'," +
"'c': [{'id': '3','value': 'c'},{'id': '1','value': 'a'},{'id': '2','value': 'b'}]}");

private final String THIRD_UNEXPECTED_JSON_OUTPUT = a2q("{'transactionId': 'test'}");

private <T> void testSerializationDeserialization(String outputResult, Class<T> clazz) throws Exception {
T deserializedObject = objectMapper.readValue(INPUT_JSON, clazz);
String serializedJson = objectMapper.writeValueAsString(deserializedObject);

String expectedJson = objectMapper.readTree(outputResult).toPrettyString();
String actualJson = objectMapper.readTree(serializedJson).toPrettyString();

assertEquals(expectedJson, actualJson);
}

@Test
public void testSerializationAndDeserializationForFirstObject() throws Exception {
testSerializationDeserialization(INPUT_JSON, FirstObject.class);
}

@Test
public void testSerializationAndDeserializationForSecondObject() throws Exception {
testSerializationDeserialization(SECOND_UNEXPECTED_JSON_OUTPUT, SecondObject.class);
}

@Test
public void testSerializationAndDeserializationForThirdObject() throws Exception {
testSerializationDeserialization(THIRD_UNEXPECTED_JSON_OUTPUT, ThirdObject.class);
}
}
Loading