From 074d87d7d52a11f24acb6cc0f1b60af98a319872 Mon Sep 17 00:00:00 2001 From: Drew Stephens Date: Sat, 8 May 2021 15:45:48 -0400 Subject: [PATCH] Extract missing parameter check --- .../module/kotlin/KotlinValueInstantiator.kt | 115 +++++++----------- 1 file changed, 41 insertions(+), 74 deletions(-) diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt index c25a5204..b3987aa3 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt @@ -66,43 +66,7 @@ internal class KotlinValueInstantiator( paramVal = NullsAsEmptyProvider(jsonProp.valueDeserializer).getNullValue(ctxt) } - val isGenericTypeVar = paramDef.type.javaType is TypeVariable<*> - val isMissingAndRequired = paramVal == null && isMissing && jsonProp.isRequired - if (isMissingAndRequired || - (!isGenericTypeVar && paramVal == null && !paramDef.type.isMarkedNullable)) { - throw MissingKotlinParameterException( - parameter = paramDef, - processor = ctxt.parser, - 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" - ).wrapWithPath(this.valueClass, jsonProp.name) - } - - if (strictNullChecks && paramVal != null) { - var paramType: String? = null - var itemType: KType? = null - if (jsonProp.type.isCollectionLikeType && paramDef.type.arguments.getOrNull(0)?.type?.isMarkedNullable == false && (paramVal as Collection<*>).any { it == null }) { - paramType = "collection" - itemType = paramDef.type.arguments[0].type - } - - if (jsonProp.type.isMapLikeType && paramDef.type.arguments.getOrNull(1)?.type?.isMarkedNullable == false && (paramVal as Map<*, *>).any { it.value == null }) { - paramType = "map" - itemType = paramDef.type.arguments[1].type - } - - if (jsonProp.type.isArrayType && paramDef.type.arguments.getOrNull(0)?.type?.isMarkedNullable == false && (paramVal as Array<*>).any { it == null }) { - paramType = "array" - itemType = paramDef.type.arguments[0].type - } - - if (paramType != null && itemType != null) { - throw MissingKotlinParameterException( - parameter = paramDef, - processor = ctxt.parser, - msg = "Instantiation of $itemType $paramType failed for JSON property ${jsonProp.name} due to null value in a $paramType that does not allow null values" - ).wrapWithPath(this.valueClass, jsonProp.name) - } - } + throwIfMissingParameter(paramDef, paramVal, isMissing, jsonProp, ctxt) bucket[idx] = paramVal } @@ -195,43 +159,7 @@ internal class KotlinValueInstantiator( paramVal = NullsAsEmptyProvider(jsonProp.valueDeserializer).getNullValue(ctxt) } - val isGenericTypeVar = paramDef.type.javaType is TypeVariable<*> - val isMissingAndRequired = paramVal == null && isMissing && jsonProp.isRequired - if (isMissingAndRequired || - (!isGenericTypeVar && paramVal == null && !paramDef.type.isMarkedNullable)) { - throw MissingKotlinParameterException( - parameter = paramDef, - processor = ctxt.parser, - 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" - ).wrapWithPath(this.valueClass, jsonProp.name) - } - - if (strictNullChecks && paramVal != null) { - var paramType: String? = null - var itemType: KType? = null - if (jsonProp.type.isCollectionLikeType && paramDef.type.arguments.getOrNull(0)?.type?.isMarkedNullable == false && (paramVal as Collection<*>).any { it == null }) { - paramType = "collection" - itemType = paramDef.type.arguments[0].type - } - - if (jsonProp.type.isMapLikeType && paramDef.type.arguments.getOrNull(1)?.type?.isMarkedNullable == false && (paramVal as Map<*, *>).any { it.value == null }) { - paramType = "map" - itemType = paramDef.type.arguments[1].type - } - - if (jsonProp.type.isArrayType && paramDef.type.arguments.getOrNull(0)?.type?.isMarkedNullable == false && (paramVal as Array<*>).any { it == null }) { - paramType = "array" - itemType = paramDef.type.arguments[0].type - } - - if (paramType != null && itemType != null) { - throw MissingKotlinParameterException( - parameter = paramDef, - processor = ctxt.parser, - msg = "Instantiation of $itemType $paramType failed for JSON property ${jsonProp.name} due to null value in a $paramType that does not allow null values" - ).wrapWithPath(this.valueClass, jsonProp.name) - } - } + throwIfMissingParameter(paramDef, paramVal, isMissing, jsonProp, ctxt) jsonParamValueList[numCallableParameters] = paramVal callableParameters[numCallableParameters] = paramDef @@ -258,6 +186,45 @@ internal class KotlinValueInstantiator( } } + private fun throwIfMissingParameter(paramDef: KParameter, paramVal: Any?, isMissing: Boolean, jsonProp: SettableBeanProperty, ctxt: DeserializationContext) { + val isGenericTypeVar = paramDef.type.javaType is TypeVariable<*> + val isMissingAndRequired = paramVal == null && isMissing && jsonProp.isRequired + if (isMissingAndRequired || (!isGenericTypeVar && paramVal == null && !paramDef.type.isMarkedNullable)) { + throw MissingKotlinParameterException( + parameter = paramDef, + processor = ctxt.parser, + 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" + ).wrapWithPath(this.valueClass, jsonProp.name) + } + + if (strictNullChecks && paramVal != null) { + var paramType: String? = null + var itemType: KType? = null + if (jsonProp.type.isCollectionLikeType && paramDef.type.arguments.getOrNull(0)?.type?.isMarkedNullable == false && (paramVal as Collection<*>).any { it == null }) { + paramType = "collection" + itemType = paramDef.type.arguments[0].type + } + + if (jsonProp.type.isMapLikeType && paramDef.type.arguments.getOrNull(1)?.type?.isMarkedNullable == false && (paramVal as Map<*, *>).any { it.value == null }) { + paramType = "map" + itemType = paramDef.type.arguments[1].type + } + + if (jsonProp.type.isArrayType && paramDef.type.arguments.getOrNull(0)?.type?.isMarkedNullable == false && (paramVal as Array<*>).any { it == null }) { + paramType = "array" + itemType = paramDef.type.arguments[0].type + } + + if (paramType != null && itemType != null) { + throw MissingKotlinParameterException( + parameter = paramDef, + processor = ctxt.parser, + msg = "Instantiation of $itemType $paramType failed for JSON property ${jsonProp.name} due to null value in a $paramType that does not allow null values" + ).wrapWithPath(this.valueClass, jsonProp.name) + } + } + } + override fun createFromObjectWith( ctxt: DeserializationContext, props: Array,