Skip to content

Commit

Permalink
Fix escaping of reserved words in kotlin2 generated data types (#623)
Browse files Browse the repository at this point in the history
* Fix escaping of reserved words in kotlin2 generated data types

* Add distribution to CI build
  • Loading branch information
mbossenbroek authored Nov 28, 2023
1 parent de5edfd commit e048106
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/nebula-snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
uses: actions/setup-java@v3
with:
java-version: 8
distribution: zulu
- uses: actions/cache@v3.3.2
id: gradle-cache
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class Person() {
@JsonIgnoreProperties("__typename")
public class Builder {
public fun build(): Person = Person(

)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.netflix.graphql.dgs.codegen.cases.dataClassWithReservedWord.expected

public object DgsClient
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.netflix.graphql.dgs.codegen.cases.dataClassWithReservedWord.expected

import kotlin.String

public object DgsConstants {
public object SAMPLETYPE {
public const val TYPE_NAME: String = "SampleType"

public const val Return: String = "return"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.netflix.graphql.dgs.codegen.cases.dataClassWithReservedWord.expected.client

import com.netflix.graphql.dgs.codegen.GraphQLProjection

public class SampleTypeProjection : GraphQLProjection() {
public val `return`: SampleTypeProjection
get() {
field("return")
return this
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.netflix.graphql.dgs.codegen.cases.dataClassWithReservedWord.expected.types

import com.fasterxml.jackson.`annotation`.JsonIgnoreProperties
import com.fasterxml.jackson.`annotation`.JsonProperty
import com.fasterxml.jackson.`annotation`.JsonTypeInfo
import com.fasterxml.jackson.databind.`annotation`.JsonDeserialize
import com.fasterxml.jackson.databind.`annotation`.JsonPOJOBuilder
import java.lang.IllegalStateException
import kotlin.String
import kotlin.jvm.JvmName

@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
@JsonDeserialize(builder = SampleType.Builder::class)
public class SampleType(
`return`: () -> String = returnDefault,
) {
private val _return: () -> String = `return`

@get:JvmName("getReturn")
public val `return`: String
get() = _return.invoke()

public companion object {
private val returnDefault: () -> String =
{ throw IllegalStateException("Field `return` was not requested") }

}

@JsonPOJOBuilder
@JsonIgnoreProperties("__typename")
public class Builder {
private var `return`: () -> String = returnDefault

@JsonProperty("return")
public fun withReturn(`return`: String): Builder = this.apply {
this.`return` = { `return` }
}

public fun build(): SampleType = SampleType(
`return` = `return`,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type SampleType {
return: String!
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.netflix.graphql.dgs.codegen.generators.shared.SchemaExtensionsUtils.f
import com.netflix.graphql.dgs.codegen.generators.shared.excludeSchemaTypeExtension
import com.netflix.graphql.dgs.codegen.shouldSkip
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
Expand Down Expand Up @@ -143,7 +144,7 @@ fun generateKotlin2DataTypes(
.addAnnotation(jsonPropertyAnnotation(field.name))
.addParameter(field.name, type(field))
.addControlFlow("return this.apply") {
addStatement("this.${field.name} = { ${field.name} }")
addStatement("this.%N = { %N }", field.name, field.name)
}
.returns(builderClassName)
.build()
Expand All @@ -153,7 +154,13 @@ fun generateKotlin2DataTypes(
.addFunction(
FunSpec.builder("build")
.returns(typeDefinition.name.toKtTypeName())
.addStatement("return ${typeDefinition.name}(\n${fields.joinToString("\n") { " ${it.name} = ${it.name}," }}\n)")
.addCode(
fields.let { fs ->
val builder = CodeBlock.builder().add("return %T(\n", ClassName(config.packageNameTypes, typeDefinition.name))
fs.forEach { f -> builder.add(" %N = %N,\n", f.name, f.name) }
builder.add(")").build()
}
)
.build()
)
.build()
Expand Down Expand Up @@ -210,7 +217,7 @@ fun generateKotlin2DataTypes(
type = LambdaTypeName.get(returnType = type(field))
)
.addModifiers(KModifier.PRIVATE)
.initializer(field.name)
.initializer("%N", field.name)
.build()
}
)
Expand Down

0 comments on commit e048106

Please sign in to comment.