-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
937d632
commit f348318
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/test/java/com/fasterxml/jackson/failing/JsonCreatorMapKeyDeserialization2158Test.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,71 @@ | ||
package com.fasterxml.jackson.failing; | ||
|
||
import static org.junit.Assert.assertThrows; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonValue; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.exc.InvalidFormatException; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
// For [databind#2158]: The default map key deserializer bypasses `@JsonCreator` methods in favour | ||
// of direct constructor invocation. | ||
public class JsonCreatorMapKeyDeserialization2158Test extends BaseMapTest { | ||
private static final TypeReference<Map<DummyDto, Integer>> MAP_TYPE_REFERENCE = | ||
new TypeReference<Map<DummyDto, Integer>>() {}; | ||
|
||
public void testDeserializeInvalidKey() { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
InvalidFormatException exception = | ||
assertThrows( | ||
InvalidFormatException.class, | ||
() -> mapper.readValue("{ \"\": 0 }", MAP_TYPE_REFERENCE)); | ||
assertTrue(exception.getMessage().contains("Value must be nonempty")); | ||
} | ||
|
||
public void testNormalizeKey() { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
assertEquals( | ||
Collections.singletonMap(DummyDto.fromValue("foo"), 0), | ||
mapper.readValue("{ \"FOO\": 0 }", MAP_TYPE_REFERENCE)); | ||
} | ||
|
||
private static final class DummyDto { | ||
@JsonValue | ||
private final String value; | ||
|
||
private DummyDto(String value) { | ||
this.value = value; | ||
} | ||
|
||
@JsonCreator | ||
static DummyDto fromValue(String value) { | ||
if (value.isEmpty()) { | ||
throw new IllegalArgumentException("Value must be nonempty"); | ||
} | ||
|
||
return new DummyDto(value.toLowerCase(Locale.ROOT)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
return o instanceof DummyDto && ((DummyDto) o).value.equals(value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(value); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("DummyDto{value=%s}", value); | ||
} | ||
} | ||
} |