Skip to content

Commit 5814b75

Browse files
committed
Add failing test for #1853; improve error checking for StringDeserializer (wrt JsonToken.FIELD_NAME)
1 parent cd14544 commit 5814b75

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public Object deserializeFromObject(JsonParser p, DeserializationContext ctxt) t
310310
* Once again, if we must, we can do more complex handling with buffering,
311311
* but let's only do that if and when that becomes necessary.
312312
*/
313-
if (_objectIdReader != null && _objectIdReader.maySerializeAsObject()) {
313+
if ((_objectIdReader != null) && _objectIdReader.maySerializeAsObject()) {
314314
if (p.hasTokenId(JsonTokenId.ID_FIELD_NAME)
315315
&& _objectIdReader.isValidReferencePropertyName(p.getCurrentName(), p)) {
316316
return deserializeFromObjectId(p, ctxt);

src/main/java/com/fasterxml/jackson/databind/deser/std/StringDeserializer.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx
5252
return ob.toString();
5353
}
5454
// allow coercions for other scalar types
55-
String text = p.getValueAsString();
56-
if (text != null) {
57-
return text;
55+
// 17-Jan-2018, tatu: Related to [databind#1853] avoid FIELD_NAME by ensuring it's
56+
// "real" scalar
57+
if (t.isScalarValue()) {
58+
String text = p.getValueAsString();
59+
if (text != null) {
60+
return text;
61+
}
5862
}
5963
return (String) ctxt.handleUnexpectedToken(_valueClass, p);
6064
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fasterxml.jackson.failing;
2+
3+
import com.fasterxml.jackson.annotation.*;
4+
5+
import com.fasterxml.jackson.databind.*;
6+
7+
public class CreatorFail1853Test extends BaseMapTest
8+
{
9+
@JsonIgnoreProperties(ignoreUnknown = true)
10+
public static class Product {
11+
String name;
12+
13+
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
14+
public Product(@JsonProperty("name") String name)
15+
{
16+
this.name = name;
17+
}
18+
19+
@JsonValue
20+
public String getName(){
21+
return name;
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return "|~" + name + "~|";
27+
}
28+
29+
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
30+
public static Product from(String name){
31+
return new Product(name);
32+
}
33+
}
34+
35+
private static final String EXAMPLE_DATA = "{\"name\":\"dummy\",\"other\":{},\"errors\":{}}";
36+
private static final String TEST_PRODUCT_JSON = "\"testProduct\"";
37+
38+
private ObjectMapper objectMapper = new ObjectMapper();
39+
40+
public void testSerialization() throws Exception {
41+
assertEquals(TEST_PRODUCT_JSON,
42+
objectMapper.writeValueAsString(new Product("testProduct")));
43+
}
44+
45+
public void testDeserializationFromObject() throws Exception {
46+
assertEquals("dummy", objectMapper.readValue(EXAMPLE_DATA, Product.class).getName());
47+
}
48+
49+
public void testDeserializationFromString() throws Exception {
50+
assertEquals("testProduct", objectMapper.readValue(TEST_PRODUCT_JSON, Product.class).getName());
51+
}
52+
}

0 commit comments

Comments
 (0)