Skip to content

Commit 58a7f7b

Browse files
committed
Add failing tests for #1502, #1503
1 parent 63afa52 commit 58a7f7b

File tree

3 files changed

+83
-10
lines changed

3 files changed

+83
-10
lines changed

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ public ValueInstantiator _valueInstantiatorInstance(DeserializationConfig config
396396
Map<AnnotatedWithParams,BeanPropertyDefinition[]> creatorParams)
397397
throws JsonMappingException
398398
{
399+
final boolean isNonStatic = ClassUtil.isNonStaticInnerClass(beanDesc.getBeanClass());
400+
399401
// First things first: the "default constructor" (zero-arg
400402
// constructor; whether implicit or explicit) is NOT included
401403
// in list of constructors, so needs to be handled separately.
@@ -411,10 +413,18 @@ public ValueInstantiator _valueInstantiatorInstance(DeserializationConfig config
411413
for (AnnotatedConstructor ctor : beanDesc.getConstructors()) {
412414
final boolean isCreator = intr.hasCreatorAnnotation(ctor);
413415
BeanPropertyDefinition[] propDefs = creatorParams.get(ctor);
414-
final int argCount = ctor.getParameterCount();
416+
int argCount = ctor.getParameterCount();
417+
418+
// 24-Jan-2017, tatu: Handling of constructors for non-static inner classes
419+
// cause nothing but grief (see [databind#1503] for example)... ugh.
420+
/*
421+
if (isNonStatic) {
422+
--argCount;
423+
}
424+
*/
415425

416426
// some single-arg factory methods (String, number) are auto-detected
417-
if (argCount == 1) {
427+
if ((argCount == 1) && !isNonStatic) {
418428
BeanPropertyDefinition argDef = (propDefs == null) ? null : propDefs[0];
419429
boolean useProps = _checkIfCreatorPropertyBased(intr, ctor, argDef);
420430

@@ -841,7 +851,12 @@ protected SettableBeanProperty constructCreatorProperty(DeserializationContext c
841851
}
842852
// 15-Oct-2015, tatu: Not 100% if context needed; removing it does not make any
843853
// existing unit tests fail. Still seems like the right thing to do.
844-
JavaType t0 = beanDesc.resolveType(param.getParameterType());
854+
java.lang.reflect.Type paramType = param.getParameterType();
855+
JavaType t0 = beanDesc.resolveType(paramType);
856+
if (t0 == null) {
857+
System.err.println("type of: "+param.getClass());
858+
// throw new Error("FOOBAR: param #"+index+" type? "+paramType);
859+
}
845860
BeanProperty.Std property = new BeanProperty.Std(name, t0,
846861
intr.findWrapperName(param),
847862
beanDesc.getClassAnnotations(), param, metadata);

src/test/java/com/fasterxml/jackson/databind/creators/InnerClassCreatorTest.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,30 @@
77
// for [databind#1501]
88
public class InnerClassCreatorTest extends BaseMapTest
99
{
10-
static class Something {
10+
static class Something1501 {
1111
public InnerSomething a;
1212

1313
// important: must name the parameter (param names module, or explicit)
1414
@JsonCreator
15-
public Something(@JsonProperty("a") InnerSomething a) { this.a = a; }
15+
public Something1501(@JsonProperty("a") InnerSomething a) { this.a = a; }
1616

17-
public Something() { a = new InnerSomething(); }
17+
public Something1501(boolean bogus) { a = new InnerSomething(); }
1818

1919
class InnerSomething {
2020
@JsonCreator
2121
public InnerSomething() { }
2222
}
2323
}
2424

25+
private final ObjectMapper MAPPER = new ObjectMapper();
26+
{
27+
MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
28+
}
29+
30+
// Used to trigger `ArrayIndexOutOfBoundsException` for missing creator property index
2531
public void testIssue1501() throws Exception
2632
{
27-
ObjectMapper mapper = new ObjectMapper();
28-
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
29-
String ser = mapper.writeValueAsString(new Something());
30-
mapper.readValue(ser, Something.class);
33+
String ser = MAPPER.writeValueAsString(new Something1501(false));
34+
MAPPER.readValue(ser, Something1501.class);
3135
}
3236
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.fasterxml.jackson.failing;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.fasterxml.jackson.databind.*;
6+
7+
// for [databind#1502], [databind#1503]
8+
public class InnerClassCreator1502Test extends BaseMapTest
9+
{
10+
static class Something1502 {
11+
@JsonProperty
12+
public InnerSomething1502 a;
13+
14+
@JsonCreator
15+
public Something1502(InnerSomething1502 a) {}
16+
17+
class InnerSomething1502 {
18+
@JsonCreator
19+
public InnerSomething1502() {}
20+
}
21+
22+
}
23+
24+
static class Broken1503 {
25+
public InnerClass1503 innerClass;
26+
27+
class InnerClass1503 {
28+
public Generic<?> generic;
29+
public InnerClass1503(@JsonProperty("generic") Generic<?> generic) {}
30+
}
31+
32+
static class Generic<T> {
33+
public int ignored;
34+
}
35+
}
36+
37+
private final ObjectMapper MAPPER = new ObjectMapper();
38+
{
39+
MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
40+
}
41+
42+
public void testIssue1502() throws Exception
43+
{
44+
String ser = MAPPER.writeValueAsString(new Something1502(null));
45+
//System.err.println("DEBUG: ser == "+ser);
46+
MAPPER.readValue(ser, Something1502.class);
47+
}
48+
49+
public void testIssue1503() throws Exception
50+
{
51+
String ser = MAPPER.writeValueAsString(new Broken1503());
52+
MAPPER.readValue(ser, Broken1503.class);
53+
}
54+
}

0 commit comments

Comments
 (0)