Skip to content

Commit 5939b1a

Browse files
committed
Fix #1853
1 parent 5814b75 commit 5939b1a

File tree

6 files changed

+79
-61
lines changed

6 files changed

+79
-61
lines changed

release-notes/CREDITS

+5
Original file line numberDiff line numberDiff line change
@@ -733,3 +733,8 @@ Pier-Luc Whissell (pwhissell@github):
733733
* Reported #1673: Serialising generic value classes via Reference Types (like Optional) fails
734734
to include type information
735735
(2.9.4)
736+
737+
Alexander Skvortcov (askvortcov@github)
738+
* Reported #1853: Deserialise from Object (using Creator methods) returns field name
739+
instead of value
740+
(2.9.4)

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Project: jackson-databind
1010
(reported by Pier-Luc W)
1111
#1729: Integer bounds verification when calling `TokenBuffer.getIntValue()`
1212
(reported by Kevin G)
13+
#1853: Deserialise from Object (using Creator methods) returns field name instead of value
14+
(reported by Alexander S)
1315
#1854: NPE deserializing collection with `@JsonCreator` and `ACCEPT_CASE_INSENSITIVE_PROPERTIES`
1416
(reported by rue-jw@github)
1517
#1855: Blacklist for more serialization gadgets (dbcp/tomcat, spring)

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,18 @@ protected void _addExplicitDelegatingCreator(DeserializationContext ctxt,
635635
ctxt.reportBadTypeDefinition(beanDesc,
636636
"No argument left as delegating for Creator %s: exactly one required", candidate);
637637
}
638+
// 17-Jan-2018, tatu: as per [databind#1853] need to ensure we will distinguish
639+
// "well-known" single-arg variants (String, int/long, boolean) from "generic" delegating...
640+
if (argCount == 1) {
641+
_handleSingleArgumentCreator(creators, candidate.creator(), true, true);
642+
// one more thing: sever link to creator property, to avoid possible later
643+
// problems with "unresolved" constructor property
644+
BeanPropertyDefinition paramDef = candidate.propertyDef(0);
645+
if (paramDef != null) {
646+
((POJOPropertyBuilder) paramDef).removeConstructors();
647+
}
648+
return;
649+
}
638650
creators.addDelegatingCreator(candidate.creator(), true, properties, ix);
639651
}
640652

@@ -691,7 +703,7 @@ protected void _addExplicitAnyCreator(DeserializationContext ctxt,
691703
// Looks like there's bit of magic regarding 1-parameter creators; others simpler:
692704
if (1 != candidate.paramCount()) {
693705
// Ok: for delegates, we want one and exactly one parameter without
694-
// injection AND without name
706+
// injection AND without name
695707
int oneNotInjected = candidate.findOnlyParamWithoutInjection();
696708
if (oneNotInjected >= 0) {
697709
// getting close; but most not have name
@@ -945,7 +957,7 @@ private void _checkImplicitlyNamedConstructors(DeserializationContext ctxt,
945957
if ((namedCount + injectCount) == argCount) {
946958
creators.addPropertyCreator(factory, false, properties);
947959
} else if ((explicitNameCount == 0) && ((injectCount + 1) == argCount)) {
948-
// [712] secondary: all but one injectable, one un-annotated (un-named)
960+
// secondary: all but one injectable, one un-annotated (un-named)
949961
creators.addDelegatingCreator(factory, false, properties, 0);
950962
} else { // otherwise, epic fail
951963
ctxt.reportBadTypeDefinition(beanDesc,

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

+5-7
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ public void setDefaultCreator(AnnotatedWithParams creator) {
132132
_creators[C_DEFAULT] = _fixAccess(creator);
133133
}
134134

135-
public void addStringCreator(AnnotatedWithParams creator,
136-
boolean explicit) {
135+
public void addStringCreator(AnnotatedWithParams creator, boolean explicit) {
137136
verifyNonDup(creator, C_STRING, explicit);
138137
}
139138

@@ -145,13 +144,11 @@ public void addLongCreator(AnnotatedWithParams creator, boolean explicit) {
145144
verifyNonDup(creator, C_LONG, explicit);
146145
}
147146

148-
public void addDoubleCreator(AnnotatedWithParams creator,
149-
boolean explicit) {
147+
public void addDoubleCreator(AnnotatedWithParams creator, boolean explicit) {
150148
verifyNonDup(creator, C_DOUBLE, explicit);
151149
}
152150

153-
public void addBooleanCreator(AnnotatedWithParams creator,
154-
boolean explicit) {
151+
public void addBooleanCreator(AnnotatedWithParams creator, boolean explicit) {
155152
verifyNonDup(creator, C_BOOLEAN, explicit);
156153
}
157154

@@ -171,7 +168,8 @@ public void addDelegatingCreator(AnnotatedWithParams creator,
171168
}
172169

173170
public void addPropertyCreator(AnnotatedWithParams creator,
174-
boolean explicit, SettableBeanProperty[] properties) {
171+
boolean explicit, SettableBeanProperty[] properties)
172+
{
175173
if (verifyNonDup(creator, C_PROPS, explicit)) {
176174
// Better ensure we have no duplicate names either...
177175
if (properties.length > 1) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fasterxml.jackson.databind.deser.creators;
2+
3+
import com.fasterxml.jackson.annotation.*;
4+
5+
import com.fasterxml.jackson.databind.*;
6+
7+
// Reproduction for [databind#1853], problem with delegating creator,
8+
// but only explicit case
9+
public class TestCreators1853 extends BaseMapTest
10+
{
11+
public static class Product {
12+
String name;
13+
14+
public Object other, errors;
15+
16+
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
17+
public Product(@JsonProperty("name") String name) {
18+
this.name = "PROP:" + name;
19+
}
20+
21+
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
22+
public static Product from(String name){
23+
return new Product(false, "DELEG:"+name);
24+
}
25+
26+
private Product(boolean bogus, String name) {
27+
this.name = name;
28+
}
29+
30+
@JsonValue
31+
public String getName(){
32+
return name;
33+
}
34+
}
35+
36+
private static final String EXAMPLE_DATA = "{\"name\":\"dummy\",\"other\":{},\"errors\":{}}";
37+
38+
private final ObjectMapper MAPPER = newObjectMapper();
39+
40+
public void testSerialization() throws Exception {
41+
assertEquals(quote("testProduct"),
42+
MAPPER.writeValueAsString(new Product(false, "testProduct")));
43+
}
44+
45+
public void testDeserializationFromObject() throws Exception {
46+
assertEquals("PROP:dummy", MAPPER.readValue(EXAMPLE_DATA, Product.class).getName());
47+
}
48+
49+
public void testDeserializationFromString() throws Exception {
50+
assertEquals("DELEG:testProduct",
51+
MAPPER.readValue(quote("testProduct"), Product.class).getName());
52+
}
53+
}

src/test/java/com/fasterxml/jackson/failing/CreatorFail1853Test.java

-52
This file was deleted.

0 commit comments

Comments
 (0)