Skip to content

Commit

Permalink
Handled reserved keywords for enum constants. (#629)
Browse files Browse the repository at this point in the history
  • Loading branch information
srinivasankavitha authored Dec 11, 2023
1 parent 5a5443b commit 3432596
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class EnumTypeGenerator(private val config: CodeGenConfig) {
}
}
}
javaType.addEnumConstant(it.name, typeSpec.build())
javaType.addEnumConstant(ReservedKeywordSanitizer.sanitize(it.name), typeSpec.build())
}

val javaFile = JavaFile.builder(getPackageName(), javaType.build()).build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package com.netflix.graphql.dgs.codegen.generators.kotlin
import com.netflix.graphql.dgs.codegen.CodeGenConfig
import com.netflix.graphql.dgs.codegen.CodeGenResult
import com.netflix.graphql.dgs.codegen.generators.java.EnumTypeGenerator
import com.netflix.graphql.dgs.codegen.generators.java.ReservedKeywordSanitizer
import com.netflix.graphql.dgs.codegen.generators.shared.applyDirectivesKotlin
import com.netflix.graphql.dgs.codegen.shouldSkip
import com.squareup.kotlinpoet.FileSpec
Expand Down Expand Up @@ -59,7 +60,7 @@ class KotlinEnumTypeGenerator(private val config: CodeGenConfig) {
applyDirectivesKotlin(it.directives, config)
)
}
kotlinType.addEnumConstant(it.name, typeSpec.build())
kotlinType.addEnumConstant(ReservedKeywordSanitizer.sanitize(it.name), typeSpec.build())
}

kotlinType.addType(TypeSpec.companionObjectBuilder().addOptionalGeneratedAnnotation(config).build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,36 @@ class CodeGenTest {
assertCompilesJava(codeGenResult.javaEnumTypes)
}

@Test
fun generateEnumWithReservedKeywords() {
val schema = """
type Query {
people: [Person]
}
enum EmployeeTypes {
default
root
new
}
""".trimIndent()

val codeGenResult = CodeGen(
CodeGenConfig(
schemas = setOf(schema),
packageName = basePackageName
)
).generate()

// Check generated enum type
assertThat(codeGenResult.javaEnumTypes.size).isEqualTo(1)
assertThat(codeGenResult.javaEnumTypes[0].typeSpec.name).isEqualTo("EmployeeTypes")
assertThat(codeGenResult.javaEnumTypes[0].typeSpec.enumConstants.size).isEqualTo(3)
assertThat(codeGenResult.javaEnumTypes[0].typeSpec.enumConstants).containsKeys("_default", "_root", "_new")

assertCompilesJava(codeGenResult.javaEnumTypes)
}

@Nested
inner class EnumAnnotationTest {
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,38 @@ class KotlinCodeGenTest {
assertCompilesKotlin(result.kotlinDataTypes + result.kotlinEnumTypes)
}

@Test
fun generateEnumWithReservedKeywords() {
val schema = """
type Query {
people: [Person]
}
enum EmployeeTypes {
default
root
new
}
""".trimIndent()

val result = CodeGen(
CodeGenConfig(
schemas = setOf(schema),
packageName = basePackageName,
language = Language.KOTLIN
)
).generate()
val type = result.kotlinEnumTypes[0].members[0] as TypeSpec

// Check generated enum type
assertThat(type.name).isEqualTo("EmployeeTypes")
assertThat(type.enumConstants.size).isEqualTo(3)
assertThat(type.enumConstants).containsKeys("_default", "_root", "_new")
assertThat(type.typeSpecs[0].isCompanion).isTrue

assertCompilesKotlin(result.kotlinDataTypes + result.kotlinEnumTypes)
}

@Nested
inner class EnumAnnotationTests {
@Test
Expand Down

0 comments on commit 3432596

Please sign in to comment.