Skip to content

Commit

Permalink
Fix #4810: heuristics should detect correct mode for creator
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 27, 2024
1 parent 4c11d74 commit ffb2a7b
Showing 1 changed file with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;

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

public class CreatorWithRenamedParamTest
Expand Down Expand Up @@ -42,23 +44,58 @@ public String getKey2() {
}
}

private final ObjectMapper MAPPER = jsonMapperBuilder()
.disable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
.annotationIntrospector(new ImplicitNameIntrospector())
.build();
// [databind#4810]
static class DataClass4810 {
private String x;

private DataClass4810(String x) {
this.x = x;
}

@JsonProperty("bar")
public String getFoo() {
return x;
}

//@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
// @JsonCreator
public static DataClass4810 create(@ImplicitName("bar") String bar) {
return new DataClass4810(bar);
}
}

// [databind#4545]
@Test
public void testCreatorWithRename4545() throws Exception
{
final ObjectMapper mapper4545 = jsonMapperBuilder()
.disable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS)
.annotationIntrospector(new ImplicitNameIntrospector())
.build();
String jsonPayload = a2q("{ 'key1': 'val1', 'key2': 'val2'}");

try {
MAPPER.readValue(jsonPayload, Payload4545.class);
mapper4545.readValue(jsonPayload, Payload4545.class);
fail("Should not pass");
} catch (UnrecognizedPropertyException e) {
verifyException(e, "Unrecognized");
verifyException(e, "key1");
}
}

// [databind#4810]
@Test
void shouldSupportPropertyRenaming() throws Exception {
ObjectMapper mapper = JsonMapper.builder()
.annotationIntrospector(new ImplicitNameIntrospector())
.build();

JsonNode serializationResult = mapper.valueToTree(DataClass4810.create("42"));

assertEquals(a2q("{'bar':'42'}"), serializationResult.toString());

DataClass4810 deserializationResult = mapper.treeToValue(serializationResult, DataClass4810.class);

assertEquals("42", deserializationResult.getFoo());
}
}

0 comments on commit ffb2a7b

Please sign in to comment.