Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: annotate kotlin input type to enable deserialization #684

Conversation

dwilkolek
Copy link
Contributor

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 dwilkolek changed the title feat: add annotations to allow json to input type deserialization feat: annotate kotlin input type to enable deserialization Apr 23, 2024
@dwilkolek dwilkolek force-pushed the feat/kotlin2-input-type-json-deserialization branch from 347baa3 to db1bf6c Compare May 12, 2024 22:45
@srinivasankavitha
Copy link
Contributor

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

@mbossenbroek
Copy link
Contributor

LGTM - thanks for the addition @dwilkolek! Also feel free to tag me directly as a reviewer for any of the kotlin2 stuff; otherwise I don't watch this repo.

@mbossenbroek mbossenbroek merged commit 467ba8a into Netflix:master May 14, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants