From b744bf378c9fcc25c69244dcd4133f150813aa3a Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Wed, 10 Jan 2018 21:45:45 -0800 Subject: [PATCH] Add failing test for #1868, related --- .../deser/BasicDeserializerFactory.java | 5 +- .../TestDefaultForUtilCollections1868.java | 100 ++++++++++++++++++ 2 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/fasterxml/jackson/failing/TestDefaultForUtilCollections1868.java diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/BasicDeserializerFactory.java b/src/main/java/com/fasterxml/jackson/databind/deser/BasicDeserializerFactory.java index d5b3639c93..3dd3c4b1f3 100644 --- a/src/main/java/com/fasterxml/jackson/databind/deser/BasicDeserializerFactory.java +++ b/src/main/java/com/fasterxml/jackson/databind/deser/BasicDeserializerFactory.java @@ -267,7 +267,7 @@ public ValueInstantiator findValueInstantiator(DeserializationContext ctxt, } } - // Sanity check: does the chosen instantatior have incomplete creators? + // Sanity check: does the chosen ValueInstantiator have incomplete creators? if (instantiator.getIncompleteParameter() != null) { final AnnotatedParameter nonAnnotatedParam = instantiator.getIncompleteParameter(); final AnnotatedWithParams ctor = nonAnnotatedParam.getOwner(); @@ -281,7 +281,8 @@ private ValueInstantiator _findStdValueInstantiator(DeserializationConfig config BeanDescription beanDesc) throws JsonMappingException { - if (beanDesc.getBeanClass() == JsonLocation.class) { + Class raw = beanDesc.getBeanClass(); + if (raw == JsonLocation.class) { return new JsonLocationInstantiator(); } return null; diff --git a/src/test/java/com/fasterxml/jackson/failing/TestDefaultForUtilCollections1868.java b/src/test/java/com/fasterxml/jackson/failing/TestDefaultForUtilCollections1868.java new file mode 100644 index 0000000000..4dc9cd18fa --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/failing/TestDefaultForUtilCollections1868.java @@ -0,0 +1,100 @@ +package com.fasterxml.jackson.failing; + +import java.util.*; + +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.databind.BaseMapTest; +import com.fasterxml.jackson.databind.ObjectMapper; + +// Unit tests for [databind#1868], related +public class TestDefaultForUtilCollections1868 extends BaseMapTest +{ + private final ObjectMapper DEFAULT_MAPPER = new ObjectMapper(); + { + DEFAULT_MAPPER.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); + } + + /* + /********************************************************** + /* Unit tests, "empty" types + /********************************************************** + */ + + public void testEmptyList() throws Exception { + _verifyCollection(Collections.emptyList()); + } + + public void testEmptySet() throws Exception { + _verifyCollection(Collections.emptySet()); + } + + public void testEmptyMap() throws Exception { + _verifyMap(Collections.emptyMap()); + } + + /* + /********************************************************** + /* Unit tests, "singleton" types + /********************************************************** + */ + + public void testSingletonList() throws Exception { + _verifyCollection(Collections.singletonList(Arrays.asList("TheOne"))); + } + + public void testSingletonSet() throws Exception { + _verifyCollection(Collections.singleton(Arrays.asList("TheOne"))); + } + + public void testSingletonMap() throws Exception { + _verifyMap(Collections.singletonMap("foo", "bar")); + } + + /* + /********************************************************** + /* Unit tests, "unmodifiable" types + /********************************************************** + */ + + public void testUnmodifiableList() throws Exception { + _verifyCollection(Collections.unmodifiableList(Arrays.asList("first", "second"))); + } + + public void testUnmodifiableSet() throws Exception + { + Set input = new LinkedHashSet<>(Arrays.asList("first", "second")); + _verifyCollection(Collections.unmodifiableSet(input)); + } + + public void testUnmodifiableMap() throws Exception + { + Map input = new LinkedHashMap<>(); + input.put("a", "b"); + input.put("c", "d"); + _verifyMap(Collections.unmodifiableMap(input)); + } + + /* + /********************************************************** + /* Helper methods + /********************************************************** + */ + + protected void _verifyCollection(Collection exp) throws Exception + { + String json = DEFAULT_MAPPER.writeValueAsString(exp); + Collection act = DEFAULT_MAPPER.readValue(json, Collection.class); + + assertEquals(exp, act); + assertEquals(exp.getClass(), act.getClass()); + } + + protected void _verifyMap(Map exp) throws Exception + { + String json = DEFAULT_MAPPER.writeValueAsString(exp); + Map act = DEFAULT_MAPPER.readValue(json, Map.class); + + assertEquals(exp, act); + assertEquals(exp.getClass(), act.getClass()); + } +}