Skip to content

Commit 04c33a5

Browse files
committed
Fixed #1345
1 parent 5f1039f commit 04c33a5

File tree

4 files changed

+43
-37
lines changed

4 files changed

+43
-37
lines changed

release-notes/VERSION

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Project: jackson-databind
66
2.8.8 (not yet released)
77

88
(partial) #994: `DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS` only works for POJOs, Maps
9+
#1345: `@JsonProperty(access = READ_ONLY)` together with generated constructor (Lombok) causes
10+
exception: "Could not find creator property with name ..."
11+
(reported by Raniz85@github)
912
#1533: `AsPropertyTypeDeserializer` ignores `DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT`
1013
#1543: JsonFormat.Shape.NUMBER_INT does not work when defined on enum type in 2.8
1114
(reported by Alex P)

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

+14-7
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,11 @@ protected void addBeanProps(DeserializationContext ctxt,
441441
BeanDescription beanDesc, BeanDeserializerBuilder builder)
442442
throws JsonMappingException
443443
{
444-
final SettableBeanProperty[] creatorProps =
445-
builder.getValueInstantiator().getFromObjectArguments(ctxt.getConfig());
446444
final boolean isConcrete = !beanDesc.getType().isAbstract();
445+
final SettableBeanProperty[] creatorProps = isConcrete
446+
? builder.getValueInstantiator().getFromObjectArguments(ctxt.getConfig())
447+
: null;
448+
final boolean hasCreatorProps = (creatorProps != null);
447449

448450
// 01-May-2016, tatu: Which base type to use here gets tricky, since
449451
// it may often make most sense to use general type for overrides,
@@ -490,8 +492,8 @@ protected void addBeanProps(DeserializationContext ctxt,
490492
}
491493
}
492494
}
493-
final boolean useGettersAsSetters = (ctxt.isEnabled(MapperFeature.USE_GETTERS_AS_SETTERS)
494-
&& ctxt.isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
495+
final boolean useGettersAsSetters = ctxt.isEnabled(MapperFeature.USE_GETTERS_AS_SETTERS)
496+
&& ctxt.isEnabled(MapperFeature.AUTO_DETECT_GETTERS);
495497

496498
// Ok: let's then filter out property definitions
497499
List<BeanPropertyDefinition> propDefs = filterBeanProps(ctxt,
@@ -531,7 +533,7 @@ protected void addBeanProps(DeserializationContext ctxt,
531533
}
532534
// 25-Sep-2014, tatu: No point in finding constructor parameters for abstract types
533535
// (since they are never used anyway)
534-
if (isConcrete && propDef.hasConstructorParameter()) {
536+
if (hasCreatorProps && propDef.hasConstructorParameter()) {
535537
/* If property is passed via constructor parameter, we must
536538
* handle things in special way. Not sure what is the most optimal way...
537539
* for now, let's just call a (new) method in builder, which does nothing.
@@ -548,8 +550,13 @@ protected void addBeanProps(DeserializationContext ctxt,
548550
}
549551
}
550552
if (cprop == null) {
551-
ctxt.reportMappingException("Could not find creator property with name '%s' (in class %s)",
552-
name, beanDesc.getBeanClass().getName());
553+
List<String> n = new ArrayList<>();
554+
for (SettableBeanProperty cp : creatorProps) {
555+
n.add(cp.getName());
556+
}
557+
ctxt.reportBadPropertyDefinition(beanDesc, propDef,
558+
"Could not find creator property with name '%s' (known Creator properties: %s)",
559+
name, n);
553560
continue;
554561
}
555562
if (prop != null) {

src/test/java/com/fasterxml/jackson/databind/deser/ReadOrWriteOnlyTest.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.fasterxml.jackson.databind.deser;
22

3+
import java.beans.ConstructorProperties;
4+
35
import com.fasterxml.jackson.annotation.JsonProperty;
46
import com.fasterxml.jackson.databind.*;
57

@@ -47,7 +49,22 @@ public String getLastName() {
4749
public void setLastName(String n) {
4850
lastName = n;
4951
}
50-
}
52+
}
53+
54+
// for [databind#1345], emulate way Lombok embellishes classes
55+
static class Foo1345 {
56+
@JsonProperty(access=JsonProperty.Access.READ_ONLY)
57+
public String id;
58+
public String name;
59+
60+
@ConstructorProperties({ "id", "name" })
61+
public Foo1345(String id, String name) {
62+
this.id = id;
63+
this.name = name;
64+
}
65+
66+
protected Foo1345() { }
67+
}
5168

5269
/*
5370
/**********************************************************
@@ -75,4 +92,12 @@ public void testReadOnly935() throws Exception
7592
Pojo935 result = MAPPER.readValue(json, Pojo935.class);
7693
assertNotNull(result);
7794
}
95+
96+
public void testReadOnly1345() throws Exception
97+
{
98+
Foo1345 result = MAPPER.readValue("{\"name\":\"test\"}", Foo1345.class);
99+
assertNotNull(result);
100+
assertEquals("test", result.name);
101+
assertNull(result.id);
102+
}
78103
}

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

-29
This file was deleted.

0 commit comments

Comments
 (0)