Skip to content

Commit 81b7cc7

Browse files
authored
Ensure reserved words are escaped in generated inputs (#604)
* Ensure reserved words are escaped in generated inputs * Address linting
1 parent f967747 commit 81b7cc7

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.netflix.graphql.dgs.codegen.cases.inputWithReservedWord.expected
2+
3+
public object DgsClient
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.netflix.graphql.dgs.codegen.cases.inputWithReservedWord.expected
2+
3+
import kotlin.String
4+
5+
public object DgsConstants {
6+
public object SAMPLEINPUT {
7+
public const val TYPE_NAME: String = "SampleInput"
8+
9+
public const val Return: String = "return"
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.netflix.graphql.dgs.codegen.cases.inputWithReservedWord.expected.types
2+
3+
import com.netflix.graphql.dgs.codegen.GraphQLInput
4+
import kotlin.Any
5+
import kotlin.Pair
6+
import kotlin.String
7+
import kotlin.collections.List
8+
9+
public class SampleInput(
10+
public val `return`: String,
11+
) : GraphQLInput() {
12+
public override fun fields(): List<Pair<String, Any?>> = listOf("return" to `return`)
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
input SampleInput {
2+
return: String!
3+
}

graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/kotlin2/GenerateKotlin2InputTypes.kt

+7-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.netflix.graphql.dgs.codegen.generators.shared.SchemaExtensionsUtils.f
2727
import com.netflix.graphql.dgs.codegen.generators.shared.excludeSchemaTypeExtension
2828
import com.netflix.graphql.dgs.codegen.shouldSkip
2929
import com.squareup.kotlinpoet.ClassName
30+
import com.squareup.kotlinpoet.CodeBlock
3031
import com.squareup.kotlinpoet.FileSpec
3132
import com.squareup.kotlinpoet.FunSpec
3233
import com.squareup.kotlinpoet.KModifier
@@ -120,10 +121,12 @@ fun generateKotlin2InputTypes(
120121
)
121122
)
122123
)
123-
.addStatement(
124-
"return listOf(${
125-
fields.joinToString(", ") { """"${it.name}" to ${it.name}""" }
126-
})"
124+
.addCode(
125+
fields.let { fs ->
126+
val builder = CodeBlock.builder().add("return listOf(")
127+
fs.forEachIndexed { i, f -> builder.add("%S to %N%L", f.name, f.name, if (i < fs.size.dec()) ", " else "") }
128+
builder.add(")").build()
129+
}
127130
)
128131
.build()
129132
)

0 commit comments

Comments
 (0)