Skip to content

Commit

Permalink
Support mapping to record classes
Browse files Browse the repository at this point in the history
Add support to DefaultInputObjectMapper to handle mapping to Java record classes.
  • Loading branch information
kilink committed Aug 9, 2024
1 parent c8cf3dc commit 0293a8a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.springframework.core.convert.converter.GenericConverter
import org.springframework.core.convert.support.DefaultConversionService
import org.springframework.util.CollectionUtils
import org.springframework.util.ReflectionUtils
import java.lang.reflect.Constructor
import java.lang.reflect.Type
import java.util.Optional
import kotlin.reflect.KClass
Expand Down Expand Up @@ -142,6 +143,10 @@ class DefaultInputObjectMapper(customInputObjectMapper: InputObjectMapper? = nul
return inputMap as T
}

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

val ctor = ReflectionUtils.accessibleConstructor(targetClass)
val instance = ctor.newInstance()
val setterAccessor = setterAccessor(instance)
Expand Down Expand Up @@ -175,6 +180,24 @@ class DefaultInputObjectMapper(customInputObjectMapper: InputObjectMapper? = nul
return instance
}

private fun <T> handleRecordClass(inputMap: Map<String, Any?>, targetClass: Class<T>): T {
val recordComponents = targetClass.recordComponents
val args = arrayOfNulls<Any?>(recordComponents.size)
for ((index, component) in recordComponents.withIndex()) {
if (component.name in inputMap) {
args[index] = maybeConvert(inputMap[component.name], component.genericType)
}
}
@Suppress("UNCHECKED_CAST")
val ctor = targetClass.declaredConstructors.first() as Constructor<T>
ctor.trySetAccessible()
try {
return ctor.newInstance(*args)
} catch (exc: ReflectiveOperationException) {
throw DgsInvalidInputArgumentException("Failed to construct record, class=${targetClass.simpleName}", exc)
}
}

private fun <T> fieldAccessor(instance: T?): ConfigurablePropertyAccessor {
val accessor = PropertyAccessorFactory.forDirectFieldAccess(instance as Any)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2024 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.graphql.dgs.internal.java.test.inputobjects;

public record RecordInput(String foo, boolean bar, Integer baz) { }
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.netflix.graphql.dgs.internal.java.test.inputobjects.JInputObjectWithK
import com.netflix.graphql.dgs.internal.java.test.inputobjects.JInputObjectWithMap
import com.netflix.graphql.dgs.internal.java.test.inputobjects.JInputObjectWithOptional
import com.netflix.graphql.dgs.internal.java.test.inputobjects.JInputObjectWithSet
import com.netflix.graphql.dgs.internal.java.test.inputobjects.RecordInput
import org.assertj.core.api.Assertions.COLLECTION
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
Expand Down Expand Up @@ -274,11 +275,26 @@ internal class InputObjectMapperTest {

@Test
fun `mapping to a Kotlin class with a value class field works`() {
val result = inputObjectMapper.mapToKotlinObject(mapOf("foo" to ValueClass("the-value"), "bar" to 12345), InputWithValueClass::class)
val result = inputObjectMapper.mapToKotlinObject(
mapOf("foo" to ValueClass("the-value"), "bar" to 12345),
InputWithValueClass::class
)
assertThat(result.foo).isEqualTo(ValueClass("the-value"))
assertThat(result.bar).isEqualTo(12345)
}

@Test
fun `The mapper supports mapping to records`() {
val result = inputObjectMapper.mapToJavaObject(mapOf("foo" to "foo-value", "bar" to true, "baz" to 12345), RecordInput::class.java)
assertThat(result).isEqualTo(RecordInput("foo-value", true, 12345))
}

@Test
fun `The mapper supports mapping to classes with record fields`() {
val result = inputObjectMapper.mapToKotlinObject(mapOf("record" to mapOf("foo" to "foo-value", "bar" to true, "baz" to 12345)), KotlinClassWithRecord::class)
assertThat(result).isEqualTo(KotlinClassWithRecord(record = RecordInput("foo-value", true, 12345)))
}

data class KotlinInputObject(val simpleString: String?, val someDate: LocalDateTime, val someObject: KotlinSomeObject)
data class KotlinNestedInputObject(val input: KotlinInputObject)
data class KotlinDoubleNestedInputObject(val inputL1: KotlinNestedInputObject)
Expand All @@ -298,4 +314,6 @@ internal class InputObjectMapperTest {

@JvmInline
value class ValueClass(val value: String)

data class KotlinClassWithRecord(val record: RecordInput)
}

0 comments on commit 0293a8a

Please sign in to comment.