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

Generated serialization for field named with hot keywords #848

Closed
dbarwacz opened this issue May 9, 2019 · 7 comments · Fixed by #915
Closed

Generated serialization for field named with hot keywords #848

dbarwacz opened this issue May 9, 2019 · 7 comments · Fixed by #915
Labels

Comments

@dbarwacz
Copy link

dbarwacz commented May 9, 2019

I use class as response field. In kotlin:

@Keep
@JsonClass(generateAdapter = true)
data class StaticLabelField(
        override val key: String,
        override val label: String,
        override val order: Int,
        override val condition: Condition? = null,
        val `class`: List<String>?
) : Field(), Validatable {
    override fun validator() = null
}

When using moshi to generate adapter code, it's not 'blocking/escaping' that field in toJson method:

override fun toJson(writer: JsonWriter, value: Field.StaticLabelField?) {
    if (value == null) {
        throw NullPointerException("value was null! Wrap in .nullSafe() to write nullable values.")
    }
    writer.beginObject()
    writer.name("key")
    stringAdapter.toJson(writer, value.key)
    writer.name("label")
    stringAdapter.toJson(writer, value.label)
    writer.name("order")
    intAdapter.toJson(writer, value.order)
    writer.name("condition")
    nullableConditionAdapter.toJson(writer, value.condition)
    writer.name("class")
    nullableListOfStringAdapter.toJson(writer, value.class) // <==== field with name 'class' has different result than intended.
    writer.endObject()
}

I worked it around by using Json annotation and different name for the field, but I think this is a bug that might be worth fixing.

@NightlyNexus
Copy link
Contributor

good find. thanks for the report.

@ZacSweers ZacSweers changed the title Generated serialization for field named class Generated serialization for field named with hot keywords May 10, 2019
@ZacSweers
Copy link
Collaborator

ZacSweers commented May 10, 2019

We'd need to either copy KotlinPoet's escapeKeywords or ask for it to be a public API. @Egorand thoughts?

@Egorand
Copy link
Collaborator

Egorand commented May 11, 2019

Latest snapshot has some improvements on that point, make sure you use %N when emitting names that might require escaping and KP will escape them for you. If that works I can cut a release.

@ZacSweers
Copy link
Collaborator

Will try it out!

@ZacSweers
Copy link
Collaborator

Confirmed this works on the latest version of kotlinpoet. We'll update to that before the release

@cortinico
Copy link

Confirmed this works on the latest version of kotlinpoet. We'll update to that before the release

Do we have an ETA for the next stable release? I'm just asking as this is blocking us on Yelp/swagger-gradle-codegen#62

@ZacSweers
Copy link
Collaborator

ZacSweers commented Sep 18, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants