Skip to content

Commit cb069f3

Browse files
committed
Minor improvement to validity checking, wrt #2977
1 parent 6c2bebe commit cb069f3

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

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

+25-5
Original file line numberDiff line numberDiff line change
@@ -880,11 +880,8 @@ protected void _addExplicitPropertyCreator(DeserializationContext ctxt,
880880
*/
881881
}
882882
name = candidate.findImplicitParamName(i);
883-
// Must be injectable or have name; without either won't work
884-
if ((name == null) && (injectId == null)) {
885-
ctxt.reportBadTypeDefinition(beanDesc,
886-
"Argument #%d has no property name, is not Injectable: can not use as Creator %s", i, candidate);
887-
}
883+
_validateNamedPropertyParameter(ctxt, beanDesc, candidate, i,
884+
name, injectId);
888885
}
889886
properties[i] = constructCreatorProperty(ctxt, beanDesc, name, i, param, injectId);
890887
}
@@ -949,6 +946,11 @@ protected void _addExplicitAnyCreator(DeserializationContext ctxt,
949946
// 13-Sep-2020, tatu: since we are configured to prefer Properties-style,
950947
// any name (explicit OR implicit does):
951948
paramName = candidate.paramName(0);
949+
// [databind#2977]: Need better exception if name missing
950+
if (paramName == null) {
951+
_validateNamedPropertyParameter(ctxt, beanDesc, candidate, 0,
952+
paramName, injectId);
953+
}
952954
break;
953955

954956
case REQUIRE_MODE:
@@ -1126,6 +1128,24 @@ protected boolean _handleSingleArgumentCreator(CreatorCollector creators,
11261128
return false;
11271129
}
11281130

1131+
// Helper method to check that parameter of Property-based creator either
1132+
// has name or is marked as Injectable
1133+
//
1134+
// @since 2.12.1
1135+
protected void _validateNamedPropertyParameter(DeserializationContext ctxt,
1136+
BeanDescription beanDesc,
1137+
CreatorCandidate candidate, int paramIndex,
1138+
PropertyName name, JacksonInject.Value injectId)
1139+
throws JsonMappingException
1140+
{
1141+
// Must be injectable or have name; without either won't work
1142+
if ((name == null) && (injectId == null)) {
1143+
ctxt.reportBadTypeDefinition(beanDesc,
1144+
"Argument #%d of constructor %s has no property name (and is not Injectable): can not use as property-based Creator",
1145+
paramIndex, candidate);
1146+
}
1147+
}
1148+
11291149
// 01-Dec-2016, tatu: As per [databind#265] we cannot yet support passing
11301150
// of unwrapped values through creator properties, so fail fast
11311151
protected void _reportUnwrappedCreatorProperty(DeserializationContext ctxt,

src/test/java/com/fasterxml/jackson/databind/deser/creators/TestCreatorNullPrimitives.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.annotation.JsonCreator;
44
import com.fasterxml.jackson.annotation.JsonProperty;
5+
56
import com.fasterxml.jackson.databind.BaseMapTest;
67
import com.fasterxml.jackson.databind.DeserializationFeature;
78
import com.fasterxml.jackson.databind.JsonMappingException;
@@ -39,7 +40,7 @@ private NestedJsonEntity(@JsonProperty("entity") JsonEntity entity) {
3940
/**********************************************************
4041
*/
4142

42-
private final ObjectMapper MAPPER = sharedMapper();
43+
private final ObjectMapper MAPPER = newJsonMapper();
4344

4445
// [databind#2101]: ensure that the property is included in the path
4546
public void testCreatorNullPrimitive() throws IOException {

0 commit comments

Comments
 (0)