override fun mapToJavaObject()

in graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/internal/DefaultInputObjectMapper.kt [159:206]


    override fun <T> mapToJavaObject(
        inputMap: Map<String, *>,
        targetClass: Class<T>,
    ): T {
        if (targetClass.isAssignableFrom(inputMap::class.java)) {
            @Suppress("UNCHECKED_CAST")
            return inputMap as T
        }

        if (targetClass.isRecord) {
            return handleRecordClass(inputMap, targetClass)
        }

        val ctor = ReflectionUtils.accessibleConstructor(targetClass)
        val instance = ctor.newInstance()
        val setterAccessor = setterAccessor(instance)
        val fieldAccessor = fieldAccessor(instance)
        var nrOfPropertyErrors = 0

        for ((name, value) in inputMap.entries) {
            try {
                if (setterAccessor.isWritableProperty(name)) {
                    setterAccessor.setPropertyValue(name, value)
                } else if (fieldAccessor.isWritableProperty(name)) {
                    fieldAccessor.setPropertyValue(name, value)
                } else {
                    nrOfPropertyErrors++
                    logger.warn("Field or property '{}' was not found on Input object of type '{}'", name, targetClass)
                }
            } catch (ex: Exception) {
                throw DgsInvalidInputArgumentException(
                    "Invalid input argument `$value` for field/property `$name` on type `${targetClass.name}`",
                    ex,
                )
            }
        }

        /**
         We can't error out if only some fields don't match.
         This would happen if new schema fields are added, but the Java type wasn't updated yet.
         If none of the fields match however, it's a pretty good indication that the wrong type was used, hence this check.
         */
        if (inputMap.isNotEmpty() && nrOfPropertyErrors == inputMap.size) {
            throw DgsInvalidInputArgumentException("Input argument type '$targetClass' doesn't match input $inputMap")
        }

        return instance
    }