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

feat: annotate kotlin input type to enable deserialization

Open dwilkolek opened this issue 1 year ago • 0 comments

Motivation We are migrating from java to kotlin codegen types. We have few quite complex input types. In our test suite we deserialise JSON files and we validate them. We'd like to keep that functionality after migration as it really simplifies our test suite.

Generated input type before changes

public class TestInput(
    public val workspaceKey: String,
    public val name: String,
) : GraphQLInput() {
    override fun fields(): List<Pair<String, Any?>> = listOf("workspaceKey" to workspaceKey, "name" to
        name)
}

Test

@Test
fun `should deserialize`() {
    val json = """
        {
            "workspaceKey": "KEY",
            "name": "Damian"
        }
    """.trimIndent()
    ObjectMapper().readValue(json, TestInput::class.java)
}

Error during deserialization

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.example.TestInput` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (String)"{
    "workspaceKey": "KEY",
    "name": "Damian"
}"; line: 2, column: 5]

Generated input type after PR changes

public class TestInput @JsonCreator constructor(
    @JsonProperty("workspaceKey") 
    public val workspaceKey: String,
    @JsonProperty("name") 
    public val name: String,
 ) : GraphQLInput() {
    override fun fields(): List<Pair<String, Any?>> = listOf("workspaceKey" to workspaceKey, "name" to
        name)
}

dwilkolek avatar Apr 23 '24 20:04 dwilkolek