dgs-codegen icon indicating copy to clipboard operation
dgs-codegen copied to clipboard

Issue of code generation when using type field reservation word field

Open jongyunha opened this issue 2 years ago • 2 comments

If the field in the input schema is a reserved word, the Pair value of the fields method of the generated code is missing a backup (`) resulting in a compilation error.

Is there any way to solve the above issue?

used codegen version: 6.0.3

Example Type

type SampleType {
  return: String!
}

Generated Class

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

  @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 } // error
    }

    public fun build() = SampleType(
      return = return, // error
    )
  }
}

gradle

tasks.withType<com.netflix.graphql.dgs.codegen.gradle.GenerateJavaTask> {
    generateClient = true
    generateDataTypes = true
    snakeCaseConstantNames = true
    generateKotlinClosureProjections = true
    generateKotlinNullableClasses = true
    language = "kotlin"
}

Parameters are created with backup, but assignments are not made with backup

jongyunha avatar Nov 27 '23 06:11 jongyunha

@mbossenbroek - could you please take a look when you get a chance?

srinivasankavitha avatar Nov 27 '23 16:11 srinivasankavitha

Fixed here (pending build/merge/release): https://github.com/Netflix/dgs-codegen/pull/623

I'm still unsure how these reserved word issues regressed, but now we have tests to lock in the behavior

mbossenbroek avatar Nov 27 '23 18:11 mbossenbroek