Skip to content

Commit

Permalink
backport #2158 test, minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Apr 10, 2021
1 parent 40faa9b commit a696a58
Showing 1 changed file with 29 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,24 @@

import static org.junit.Assert.assertThrows;

import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

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));
}

public class JsonCreatorMapKeyDeserialization2158Test extends BaseMapTest
{
private static final class DummyDto {
@JsonValue
private final String value;
Expand Down Expand Up @@ -68,4 +52,24 @@ public String toString() {
return String.format("DummyDto{value=%s}", value);
}
}

private final ObjectMapper MAPPER = newJsonMapper();

private static final TypeReference<Map<DummyDto, Integer>> MAP_TYPE_REFERENCE =
new TypeReference<Map<DummyDto, Integer>>() {};

public void testDeserializeInvalidKey() throws Exception
{
InvalidFormatException exception =
assertThrows(
InvalidFormatException.class,
() -> MAPPER.readValue("{ \"\": 0 }", MAP_TYPE_REFERENCE));
assertTrue(exception.getMessage().contains("Value must be nonempty"));
}

public void testNormalizeKey() throws Exception {
assertEquals(
Collections.singletonMap(DummyDto.fromValue("foo"), 0),
MAPPER.readValue("{ \"FOO\": 0 }", MAP_TYPE_REFERENCE));
}
}

0 comments on commit a696a58

Please sign in to comment.