Skip to content

Commit 0fdcc49

Browse files
committed
Handle NullValue in input field default value code generation
When a GraphQL input field has `= null` as its default value, the code generator called toString() on the NullValue AST node, producing the literal string `NullValue{}` instead of valid Java/Kotlin `null`. This is the same root cause as the EnumValue bug fixed in PR #33. Fixes #770
1 parent 5dfd192 commit 0fdcc49

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/java/DataTypeGenerator.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import graphql.language.InputObjectTypeExtensionDefinition
4141
import graphql.language.IntValue
4242
import graphql.language.InterfaceTypeDefinition
4343
import graphql.language.NonNullType
44+
import graphql.language.NullValue
4445
import graphql.language.ObjectTypeDefinition
4546
import graphql.language.ObjectTypeExtensionDefinition
4647
import graphql.language.ObjectValue
@@ -316,6 +317,7 @@ class InputTypeGenerator(
316317
}
317318
}
318319

320+
is NullValue -> CodeBlock.of("null")
319321
else -> CodeBlock.of("\$L", value)
320322
}
321323

graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/shared/GenerateKotlinCode.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ fun generateKotlinCode(
8787
)
8888
}
8989

90+
is NullValue -> CodeBlock.of("null")
9091
else -> CodeBlock.of("%L", value)
9192
}
9293

graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/CodeGenTest.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,33 @@ class CodeGenTest {
21892189
assertCompilesJava(dataTypes + enumTypes)
21902190
}
21912191

2192+
@Test
2193+
fun generateInputWithDefaultNullValue() {
2194+
val schema =
2195+
"""
2196+
input QuantityRuleInput {
2197+
maximum: Int = null
2198+
minimum: Int!
2199+
}
2200+
""".trimIndent()
2201+
2202+
val (dataTypes) = CodeGen(CodeGenConfig(schemas = setOf(schema), packageName = BASE_PACKAGE_NAME)).generate()
2203+
assertThat(dataTypes).hasSize(1)
2204+
2205+
val data = dataTypes[0]
2206+
assertThat(data.packageName()).isEqualTo(TYPES_PACKAGE_NAME)
2207+
2208+
val type = data.typeSpec()
2209+
assertThat(type.name()).isEqualTo("QuantityRuleInput")
2210+
2211+
val fields = type.fieldSpecs()
2212+
val maximumField = fields.find { it.name() == "maximum" }
2213+
assertThat(maximumField).isNotNull
2214+
assertThat(maximumField!!.initializer().toString()).isEqualTo("null")
2215+
2216+
assertCompilesJava(dataTypes)
2217+
}
2218+
21922219
@Test
21932220
fun generateExtendedInputTypes() {
21942221
val schema =

graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/KotlinCodeGenTest.kt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,6 +1759,48 @@ class KotlinCodeGenTest {
17591759
assertCompilesKotlin(dataTypes.plus(enums))
17601760
}
17611761

1762+
@Test
1763+
fun generateInputWithDefaultNullValue() {
1764+
val schema =
1765+
"""
1766+
input QuantityRuleInput {
1767+
maximum: Int = null
1768+
minimum: Int!
1769+
}
1770+
""".trimIndent()
1771+
1772+
val codeGenResult =
1773+
CodeGen(
1774+
CodeGenConfig(
1775+
schemas = setOf(schema),
1776+
packageName = BASE_PACKAGE_NAME,
1777+
language = Language.KOTLIN,
1778+
),
1779+
).generate()
1780+
val dataTypes = codeGenResult.kotlinDataTypes
1781+
assertThat(dataTypes).hasSize(1)
1782+
1783+
val data = dataTypes[0]
1784+
assertThat(data.packageName).isEqualTo(TYPES_PACKAGE_NAME)
1785+
1786+
val members = data.members
1787+
assertThat(members).hasSize(1)
1788+
1789+
val type = members[0] as TypeSpec
1790+
assertThat(type.name).isEqualTo("QuantityRuleInput")
1791+
1792+
val ctorSpec = type.primaryConstructor
1793+
assertThat(ctorSpec).isNotNull
1794+
assertThat(ctorSpec!!.parameters).hasSize(2)
1795+
1796+
val maximumParam = ctorSpec.parameters.find { it.name == "maximum" }
1797+
assertThat(maximumParam).isNotNull
1798+
assertThat(maximumParam!!.defaultValue).isNotNull
1799+
assertThat(maximumParam.defaultValue.toString()).isEqualTo("null")
1800+
1801+
assertCompilesKotlin(dataTypes)
1802+
}
1803+
17621804
@Test
17631805
fun generateInputWithEmptyDefaultValueForArray() {
17641806
val schema =

0 commit comments

Comments
 (0)