Skip to content

Commit

Permalink
fix: in an input object a nested scalar is not mapped (#1660)
Browse files Browse the repository at this point in the history
Co-authored-by: gteplitsky <gteplitsky@chegg.com>
  • Loading branch information
gilteplitsky and gteplitsky authored Oct 23, 2023
1 parent 3c4bf47 commit d8eb2e9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ class DefaultInputObjectMapper(private val customInputObjectMapper: InputObjectM
trySetField(declaredField, instance, newList)
}
} else if (fieldClass.isEnum) {
val enumValue =
(fieldClass.enumConstants as Array<Enum<*>>).find { enumValue -> enumValue.name == it.value }
val enumValue = if (it.value == null) null else (fieldClass.enumConstants as Array<Enum<*>>).find { enumValue -> enumValue.name == it.value.toString() }
trySetField(declaredField, instance, enumValue)
} else {
trySetField(declaredField, instance, it.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@

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

import graphql.scalars.country.code.CountryCode;

import java.util.List;

public class JInputMessage {
private JGreetingType type;
private List<JGreetingType> typeList;

private CountryCode countryCode;

public JGreetingType getType() {
return type;
}
Expand All @@ -37,4 +41,12 @@ public List<JGreetingType> getTypeList() {
public void setTypeList(List<JGreetingType> typeList) {
this.typeList = typeList;
}

public CountryCode getCountryCode() {
return countryCode;
}

public void setCountryCode(CountryCode countryCode) {
this.countryCode = countryCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import graphql.ExceptionWhileDataFetching
import graphql.ExecutionInput
import graphql.GraphQL
import graphql.scalars.ExtendedScalars
import graphql.scalars.country.code.CountryCode
import graphql.schema.DataFetchingEnvironment
import graphql.schema.idl.RuntimeWiring
import org.assertj.core.api.Assertions.assertThat
Expand Down Expand Up @@ -2217,6 +2218,47 @@ internal class InputArgumentTest {
}
}

@Test
fun `The enum scalar in a nested input should be mapped`() {
val schema = """
type Query {
hello(input:InputMessage): String
}
input InputMessage {
countryCode: CountryCode
}
scalar CountryCode
""".trimIndent()

@DgsComponent
class Fetcher {
@DgsData(parentType = "Query", field = "hello")
fun hello(@InputArgument input: JInputMessage): String {
assertThat(input.countryCode).isInstanceOf(CountryCode::class.java)
assertThat(input.countryCode).isEqualTo(CountryCode.US)
return "Hello, this is a ${input.countryCode} greeting"
}

@DgsRuntimeWiring
fun addScalar(builder: RuntimeWiring.Builder): RuntimeWiring.Builder {
return builder.scalar(ExtendedScalars.CountryCode)
}
}

contextRunner.withBeans(Fetcher::class).run { context ->
val provider = schemaProvider(context)
val build = GraphQL.newGraphQL(provider.schema(schema)).build()
val executionResult = build.execute("""{hello(input: {countryCode: "US"})}""")
assertThat(executionResult).isNotNull
assertThat(executionResult.errors).isEmpty()
assertThat(executionResult.isDataPresent).isTrue
val data = executionResult.getData<Map<String, *>>()
assertThat(data).extracting("hello").isEqualTo("Hello, this is a US greeting")
}
}

private fun ApplicationContextRunner.withBeans(vararg beanClasses: KClass<*>): ApplicationContextRunner {
var context = this
for (klazz in beanClasses) {
Expand Down

0 comments on commit d8eb2e9

Please sign in to comment.