forked from FasterXML/jackson-databind
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix FasterXML#2543: Skip delegating creator arguments when collecting…
… properties
- Loading branch information
1 parent
6493f5f
commit 05ab176
Showing
2 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...com/fasterxml/jackson/databind/deser/creators/DelegatingCreatorImplicitNames2543Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.fasterxml.jackson.databind.deser.creators; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.introspect.*; | ||
import org.junit.Test; | ||
|
||
import static com.fasterxml.jackson.annotation.JsonCreator.Mode.DELEGATING; | ||
import static com.fasterxml.jackson.annotation.JsonCreator.Mode.PROPERTIES; | ||
import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.a2q; | ||
import static java.util.Objects.requireNonNull; | ||
import static org.assertj.core.api.Assertions.assertThatNoException; | ||
|
||
public class DelegatingCreatorImplicitNames2543Test { | ||
static class Data { | ||
|
||
final String part1; | ||
final String part2; | ||
|
||
// this creator is considered a source of settable bean properties, | ||
// used during deserialization | ||
@JsonCreator(mode = PROPERTIES) | ||
public Data(@JsonProperty("part1") String part1, | ||
@JsonProperty("part2") String part2) { | ||
this.part1 = part1; | ||
this.part2 = part2; | ||
} | ||
|
||
// no properties should be collected from this creator, | ||
// even though it has an argument with an implicit name | ||
@JsonCreator(mode = DELEGATING) | ||
public static Data fromFullData(String fullData) { | ||
String[] parts = fullData.split("\\s+", 2); | ||
return new Data(parts[0], parts[1]); | ||
} | ||
} | ||
|
||
private static class DelegatingCreatorNamedArgumentIntrospector | ||
extends JacksonAnnotationIntrospector { | ||
public String findImplicitPropertyName(AnnotatedMember member) { | ||
if (member instanceof AnnotatedParameter p) { | ||
if (p.getOwner() instanceof AnnotatedMethod m) { | ||
if (requireNonNull(m.getAnnotation(JsonCreator.class)).mode() == DELEGATING) | ||
return "fullData"; | ||
} | ||
} | ||
return super.findImplicitPropertyName(member); | ||
} | ||
} | ||
|
||
private static final ObjectMapper MAPPER = new ObjectMapper() | ||
.setAnnotationIntrospector(new DelegatingCreatorNamedArgumentIntrospector()); | ||
|
||
@Test | ||
public void testDeserialization() { | ||
assertThatNoException() | ||
.isThrownBy(() -> MAPPER.readValue(a2q("{'part1':'a','part2':'b'}"), Data.class)); | ||
} | ||
|
||
@Test | ||
public void testDelegatingDeserialization() { | ||
assertThatNoException() | ||
.isThrownBy(() -> MAPPER.readValue(a2q("'a b'"), Data.class)); | ||
} | ||
} |