Skip to content

Commit 80e1b7e

Browse files
committed
Add a (failing) test for #383
1 parent d352f0c commit 80e1b7e

File tree

5 files changed

+63
-46
lines changed

5 files changed

+63
-46
lines changed

src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java

+13-25
Original file line numberDiff line numberDiff line change
@@ -685,11 +685,8 @@ public JsonMappingException mappingException(Class<?> targetClass) {
685685
return mappingException(targetClass, _parser.getCurrentToken());
686686
}
687687

688-
public JsonMappingException mappingException(Class<?> targetClass, JsonToken token)
689-
{
690-
String clsName = _calcName(targetClass);
691-
return JsonMappingException.from(_parser,
692-
"Can not deserialize instance of "+clsName+" out of "+token+" token");
688+
public JsonMappingException mappingException(Class<?> targetClass, JsonToken token) {
689+
return JsonMappingException.from(_parser, "Can not deserialize instance of "+_calcName(targetClass)+" out of "+token+" token");
693690
}
694691

695692
/**
@@ -705,11 +702,9 @@ public JsonMappingException mappingException(String message) {
705702
* to indicate problem with physically constructing instance of
706703
* specified class (missing constructor, exception from constructor)
707704
*/
708-
public JsonMappingException instantiationException(Class<?> instClass, Throwable t)
709-
{
705+
public JsonMappingException instantiationException(Class<?> instClass, Throwable t) {
710706
return JsonMappingException.from(_parser,
711-
"Can not construct instance of "+instClass.getName()+", problem: "+t.getMessage(),
712-
t);
707+
"Can not construct instance of "+instClass.getName()+", problem: "+t.getMessage(), t);
713708
}
714709

715710
public JsonMappingException instantiationException(Class<?> instClass, String msg) {
@@ -768,8 +763,7 @@ public JsonMappingException weirdNumberException(Number value, Class<?> instClas
768763
* Object field name was not in format to be able to deserialize specified
769764
* key type.
770765
*/
771-
public JsonMappingException weirdKeyException(Class<?> keyClass, String keyValue, String msg)
772-
{
766+
public JsonMappingException weirdKeyException(Class<?> keyClass, String keyValue, String msg) {
773767
return InvalidFormatException.from(_parser,
774768
"Can not construct Map key of type "+keyClass.getName()+" from String \""+_desc(keyValue)+"\": "+msg,
775769
keyValue, keyClass);
@@ -779,22 +773,19 @@ public JsonMappingException weirdKeyException(Class<?> keyClass, String keyValue
779773
* Helper method for indicating that the current token was expected to be another
780774
* token.
781775
*/
782-
public JsonMappingException wrongTokenException(JsonParser jp, JsonToken expToken, String msg)
783-
{
776+
public JsonMappingException wrongTokenException(JsonParser jp, JsonToken expToken, String msg) {
784777
return JsonMappingException.from(jp, "Unexpected token ("+jp.getCurrentToken()+"), expected "+expToken+": "+msg);
785778
}
786779

787780
/**
788781
* Helper method for constructing exception to indicate that given
789782
* type id (parsed from JSON) could not be converted to a Java type.
790783
*/
791-
public JsonMappingException unknownTypeException(JavaType type, String id)
792-
{
784+
public JsonMappingException unknownTypeException(JavaType type, String id) {
793785
return JsonMappingException.from(_parser, "Could not resolve type id '"+id+"' into a subtype of "+type);
794786
}
795787

796-
public JsonMappingException endOfInputException(Class<?> instClass)
797-
{
788+
public JsonMappingException endOfInputException(Class<?> instClass) {
798789
return JsonMappingException.from(_parser, "Unexpected end-of-input when trying to deserialize a "
799790
+instClass.getName());
800791
}
@@ -820,8 +811,7 @@ protected DateFormat getDateFormat()
820811
return df;
821812
}
822813

823-
protected String determineClassName(Object instance)
824-
{
814+
protected String determineClassName(Object instance) {
825815
return ClassUtil.getClassDescription(instance);
826816
}
827817

@@ -831,24 +821,22 @@ protected String determineClassName(Object instance)
831821
/**********************************************************
832822
*/
833823

834-
protected String _calcName(Class<?> cls)
835-
{
824+
protected String _calcName(Class<?> cls) {
836825
if (cls.isArray()) {
837826
return _calcName(cls.getComponentType())+"[]";
838827
}
839828
return cls.getName();
840829
}
841830

842-
protected String _valueDesc()
843-
{
831+
protected String _valueDesc() {
844832
try {
845833
return _desc(_parser.getText());
846834
} catch (Exception e) {
847835
return "[N/A]";
848836
}
849837
}
850-
protected String _desc(String desc)
851-
{
838+
839+
protected String _desc(String desc) {
852840
// !!! should we quote it? (in case there are control chars, linefeeds)
853841
if (desc.length() > MAX_ERROR_STR_LEN) {
854842
desc = desc.substring(0, MAX_ERROR_STR_LEN) + "]...[" + desc.substring(desc.length() - MAX_ERROR_STR_LEN);

src/main/java/com/fasterxml/jackson/databind/deser/SettableBeanProperty.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@
2121
* setter-backed properties, as well as a few more esoteric variations,
2222
* can be handled.
2323
*/
24+
@SuppressWarnings("serial")
2425
public abstract class SettableBeanProperty
2526
implements BeanProperty,
2627
java.io.Serializable
2728
{
28-
private static final long serialVersionUID = -1026580169193933453L;
29-
3029
/**
3130
* To avoid nasty NPEs, let's use a placeholder for _valueDeserializer,
3231
* if real deserializer is not (yet) available.

src/main/java/com/fasterxml/jackson/databind/deser/impl/FailingDeserializer.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ public FailingDeserializer(String m) {
2323
}
2424

2525
@Override
26-
public Object deserialize(JsonParser jp, DeserializationContext ctxt)
27-
throws JsonMappingException
28-
{
26+
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws JsonMappingException{
2927
throw ctxt.mappingException(_message);
3028
}
3129
}

src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrapped.java

+15-16
Original file line numberDiff line numberDiff line change
@@ -75,39 +75,38 @@ static class Name {
7575
/**********************************************************
7676
*/
7777

78-
private final ObjectMapper mapper = new ObjectMapper();
79-
80-
public void testSimpleUnwrappingSerialize() throws Exception
81-
{
78+
private final ObjectMapper MAPPER = new ObjectMapper();
79+
80+
public void testSimpleUnwrappingSerialize() throws Exception {
8281
assertEquals("{\"name\":\"Tatu\",\"x\":1,\"y\":2}",
83-
mapper.writeValueAsString(new Unwrapping("Tatu", 1, 2)));
82+
MAPPER.writeValueAsString(new Unwrapping("Tatu", 1, 2)));
8483
}
85-
public void testDeepUnwrappingSerialize() throws Exception
86-
{
84+
85+
public void testDeepUnwrappingSerialize() throws Exception {
8786
assertEquals("{\"name\":\"Tatu\",\"x\":1,\"y\":2}",
88-
mapper.writeValueAsString(new DeepUnwrapping("Tatu", 1, 2)));
87+
MAPPER.writeValueAsString(new DeepUnwrapping("Tatu", 1, 2)));
8988
}
9089

9190
/*
9291
/**********************************************************
9392
/* Tests, deserialization
9493
/**********************************************************
9594
*/
96-
95+
9796
public void testSimpleUnwrappedDeserialize() throws Exception
9897
{
99-
Unwrapping bean = mapper.readValue("{\"name\":\"Tatu\",\"y\":7,\"x\":-13}",
98+
Unwrapping bean = MAPPER.readValue("{\"name\":\"Tatu\",\"y\":7,\"x\":-13}",
10099
Unwrapping.class);
101100
assertEquals("Tatu", bean.name);
102101
Location loc = bean.location;
103102
assertNotNull(loc);
104103
assertEquals(-13, loc.x);
105104
assertEquals(7, loc.y);
106105
}
107-
106+
108107
public void testDoubleUnwrapping() throws Exception
109108
{
110-
TwoUnwrappedProperties bean = mapper.readValue("{\"first\":\"Joe\",\"y\":7,\"last\":\"Smith\",\"x\":-13}",
109+
TwoUnwrappedProperties bean = MAPPER.readValue("{\"first\":\"Joe\",\"y\":7,\"last\":\"Smith\",\"x\":-13}",
111110
TwoUnwrappedProperties.class);
112111
Location loc = bean.location;
113112
assertNotNull(loc);
@@ -118,10 +117,10 @@ public void testDoubleUnwrapping() throws Exception
118117
assertEquals("Joe", name.first);
119118
assertEquals("Smith", name.last);
120119
}
121-
120+
122121
public void testDeepUnwrapping() throws Exception
123122
{
124-
DeepUnwrapping bean = mapper.readValue("{\"x\":3,\"name\":\"Bob\",\"y\":27}",
123+
DeepUnwrapping bean = MAPPER.readValue("{\"x\":3,\"name\":\"Bob\",\"y\":27}",
125124
DeepUnwrapping.class);
126125
Unwrapping uw = bean.unwrapped;
127126
assertNotNull(uw);
@@ -131,10 +130,10 @@ public void testDeepUnwrapping() throws Exception
131130
assertEquals(3, loc.x);
132131
assertEquals(27, loc.y);
133132
}
134-
133+
135134
public void testUnwrappedDeserializeWithCreator() throws Exception
136135
{
137-
UnwrappingWithCreator bean = mapper.readValue("{\"x\":1,\"y\":2,\"name\":\"Tatu\"}",
136+
UnwrappingWithCreator bean = MAPPER.readValue("{\"x\":1,\"y\":2,\"name\":\"Tatu\"}",
138137
UnwrappingWithCreator.class);
139138
assertEquals("Tatu", bean.name);
140139
Location loc = bean.location;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fasterxml.jackson.failing;
2+
3+
import com.fasterxml.jackson.annotation.*;
4+
5+
import com.fasterxml.jackson.databind.*;
6+
7+
public class TestUnwrappedIssue383 extends BaseMapTest
8+
{
9+
// [Issue#383]
10+
static class RecursivePerson {
11+
public String name;
12+
public int age;
13+
@JsonUnwrapped(prefix="child.") public RecursivePerson child;
14+
}
15+
16+
/*
17+
/**********************************************************
18+
/* Tests, serialization
19+
/**********************************************************
20+
*/
21+
22+
private final ObjectMapper MAPPER = new ObjectMapper();
23+
24+
public void testRecursiveUsage() throws Exception
25+
{
26+
final String JSON = "{ 'name': 'Bob', 'age': 45, 'gender': 0, 'child.name': 'Bob jr', 'child.age': 15 }}";
27+
RecursivePerson p = MAPPER.readValue(aposToQuotes(JSON), RecursivePerson.class);
28+
assertNotNull(p);
29+
assertEquals("Bob", p.name);
30+
assertNotNull(p.child);
31+
assertEquals("Bob jr", p.child.name);
32+
}
33+
}

0 commit comments

Comments
 (0)