Skip to content

Commit 074d87d

Browse files
committed
Extract missing parameter check
1 parent d0e6544 commit 074d87d

File tree

1 file changed

+41
-74
lines changed

1 file changed

+41
-74
lines changed

src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt

Lines changed: 41 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -66,43 +66,7 @@ internal class KotlinValueInstantiator(
6666
paramVal = NullsAsEmptyProvider(jsonProp.valueDeserializer).getNullValue(ctxt)
6767
}
6868

69-
val isGenericTypeVar = paramDef.type.javaType is TypeVariable<*>
70-
val isMissingAndRequired = paramVal == null && isMissing && jsonProp.isRequired
71-
if (isMissingAndRequired ||
72-
(!isGenericTypeVar && paramVal == null && !paramDef.type.isMarkedNullable)) {
73-
throw MissingKotlinParameterException(
74-
parameter = paramDef,
75-
processor = ctxt.parser,
76-
msg = "Instantiation of ${this.valueTypeDesc} value failed for JSON property ${jsonProp.name} due to missing (therefore NULL) value for creator parameter ${paramDef.name} which is a non-nullable type"
77-
).wrapWithPath(this.valueClass, jsonProp.name)
78-
}
79-
80-
if (strictNullChecks && paramVal != null) {
81-
var paramType: String? = null
82-
var itemType: KType? = null
83-
if (jsonProp.type.isCollectionLikeType && paramDef.type.arguments.getOrNull(0)?.type?.isMarkedNullable == false && (paramVal as Collection<*>).any { it == null }) {
84-
paramType = "collection"
85-
itemType = paramDef.type.arguments[0].type
86-
}
87-
88-
if (jsonProp.type.isMapLikeType && paramDef.type.arguments.getOrNull(1)?.type?.isMarkedNullable == false && (paramVal as Map<*, *>).any { it.value == null }) {
89-
paramType = "map"
90-
itemType = paramDef.type.arguments[1].type
91-
}
92-
93-
if (jsonProp.type.isArrayType && paramDef.type.arguments.getOrNull(0)?.type?.isMarkedNullable == false && (paramVal as Array<*>).any { it == null }) {
94-
paramType = "array"
95-
itemType = paramDef.type.arguments[0].type
96-
}
97-
98-
if (paramType != null && itemType != null) {
99-
throw MissingKotlinParameterException(
100-
parameter = paramDef,
101-
processor = ctxt.parser,
102-
msg = "Instantiation of $itemType $paramType failed for JSON property ${jsonProp.name} due to null value in a $paramType that does not allow null values"
103-
).wrapWithPath(this.valueClass, jsonProp.name)
104-
}
105-
}
69+
throwIfMissingParameter(paramDef, paramVal, isMissing, jsonProp, ctxt)
10670

10771
bucket[idx] = paramVal
10872
}
@@ -195,43 +159,7 @@ internal class KotlinValueInstantiator(
195159
paramVal = NullsAsEmptyProvider(jsonProp.valueDeserializer).getNullValue(ctxt)
196160
}
197161

198-
val isGenericTypeVar = paramDef.type.javaType is TypeVariable<*>
199-
val isMissingAndRequired = paramVal == null && isMissing && jsonProp.isRequired
200-
if (isMissingAndRequired ||
201-
(!isGenericTypeVar && paramVal == null && !paramDef.type.isMarkedNullable)) {
202-
throw MissingKotlinParameterException(
203-
parameter = paramDef,
204-
processor = ctxt.parser,
205-
msg = "Instantiation of ${this.valueTypeDesc} value failed for JSON property ${jsonProp.name} due to missing (therefore NULL) value for creator parameter ${paramDef.name} which is a non-nullable type"
206-
).wrapWithPath(this.valueClass, jsonProp.name)
207-
}
208-
209-
if (strictNullChecks && paramVal != null) {
210-
var paramType: String? = null
211-
var itemType: KType? = null
212-
if (jsonProp.type.isCollectionLikeType && paramDef.type.arguments.getOrNull(0)?.type?.isMarkedNullable == false && (paramVal as Collection<*>).any { it == null }) {
213-
paramType = "collection"
214-
itemType = paramDef.type.arguments[0].type
215-
}
216-
217-
if (jsonProp.type.isMapLikeType && paramDef.type.arguments.getOrNull(1)?.type?.isMarkedNullable == false && (paramVal as Map<*, *>).any { it.value == null }) {
218-
paramType = "map"
219-
itemType = paramDef.type.arguments[1].type
220-
}
221-
222-
if (jsonProp.type.isArrayType && paramDef.type.arguments.getOrNull(0)?.type?.isMarkedNullable == false && (paramVal as Array<*>).any { it == null }) {
223-
paramType = "array"
224-
itemType = paramDef.type.arguments[0].type
225-
}
226-
227-
if (paramType != null && itemType != null) {
228-
throw MissingKotlinParameterException(
229-
parameter = paramDef,
230-
processor = ctxt.parser,
231-
msg = "Instantiation of $itemType $paramType failed for JSON property ${jsonProp.name} due to null value in a $paramType that does not allow null values"
232-
).wrapWithPath(this.valueClass, jsonProp.name)
233-
}
234-
}
162+
throwIfMissingParameter(paramDef, paramVal, isMissing, jsonProp, ctxt)
235163

236164
jsonParamValueList[numCallableParameters] = paramVal
237165
callableParameters[numCallableParameters] = paramDef
@@ -258,6 +186,45 @@ internal class KotlinValueInstantiator(
258186
}
259187
}
260188

189+
private fun throwIfMissingParameter(paramDef: KParameter, paramVal: Any?, isMissing: Boolean, jsonProp: SettableBeanProperty, ctxt: DeserializationContext) {
190+
val isGenericTypeVar = paramDef.type.javaType is TypeVariable<*>
191+
val isMissingAndRequired = paramVal == null && isMissing && jsonProp.isRequired
192+
if (isMissingAndRequired || (!isGenericTypeVar && paramVal == null && !paramDef.type.isMarkedNullable)) {
193+
throw MissingKotlinParameterException(
194+
parameter = paramDef,
195+
processor = ctxt.parser,
196+
msg = "Instantiation of ${this.valueTypeDesc} value failed for JSON property ${jsonProp.name} due to missing (therefore NULL) value for creator parameter ${paramDef.name} which is a non-nullable type"
197+
).wrapWithPath(this.valueClass, jsonProp.name)
198+
}
199+
200+
if (strictNullChecks && paramVal != null) {
201+
var paramType: String? = null
202+
var itemType: KType? = null
203+
if (jsonProp.type.isCollectionLikeType && paramDef.type.arguments.getOrNull(0)?.type?.isMarkedNullable == false && (paramVal as Collection<*>).any { it == null }) {
204+
paramType = "collection"
205+
itemType = paramDef.type.arguments[0].type
206+
}
207+
208+
if (jsonProp.type.isMapLikeType && paramDef.type.arguments.getOrNull(1)?.type?.isMarkedNullable == false && (paramVal as Map<*, *>).any { it.value == null }) {
209+
paramType = "map"
210+
itemType = paramDef.type.arguments[1].type
211+
}
212+
213+
if (jsonProp.type.isArrayType && paramDef.type.arguments.getOrNull(0)?.type?.isMarkedNullable == false && (paramVal as Array<*>).any { it == null }) {
214+
paramType = "array"
215+
itemType = paramDef.type.arguments[0].type
216+
}
217+
218+
if (paramType != null && itemType != null) {
219+
throw MissingKotlinParameterException(
220+
parameter = paramDef,
221+
processor = ctxt.parser,
222+
msg = "Instantiation of $itemType $paramType failed for JSON property ${jsonProp.name} due to null value in a $paramType that does not allow null values"
223+
).wrapWithPath(this.valueClass, jsonProp.name)
224+
}
225+
}
226+
}
227+
261228
override fun createFromObjectWith(
262229
ctxt: DeserializationContext,
263230
props: Array<out SettableBeanProperty>,

0 commit comments

Comments
 (0)