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

Kotlin inheritance with non-settable properties #835

Closed
3mph4515 opened this issue Apr 11, 2019 · 3 comments
Closed

Kotlin inheritance with non-settable properties #835

3mph4515 opened this issue Apr 11, 2019 · 3 comments

Comments

@3mph4515
Copy link

3mph4515 commented Apr 11, 2019

If I have an open class and inherited data class from it, Kotlin-moshi codegen skip default values. Is this intended behaviour or bug?

@JsonClass(generateAdapter = true)
data class B constructor(val test: String) : A(foo = "foo")
open class A constructor(val foo: String)
val b = B("test")
val json = jsonAdapter.toJson(b)

produces only
{"test":"test"} , w/out foo default value.

@ZacSweers
Copy link
Collaborator

This is happening because we only handle supertype properties if they're mutable. The logic to fix this would be to include "not settable" properties and filter those out during fromJson.

I can see why it'd be expected to appear in toJson, but at the same time worry about the possible asymmetry of fromJson ignoring them and toJson not ignoring them.

@swankjesse @JakeWharton @rharter penny for your thoughts?

@ZacSweers
Copy link
Collaborator

FWIW - KotlinJsonAdapterFactory has the same behavior

@ZacSweers ZacSweers changed the title Kotlin-moshi codegen inheritance with default values Kotlin inheritance with default values Sep 12, 2019
@ZacSweers ZacSweers changed the title Kotlin inheritance with default values Kotlin inheritance with non-settable properties Sep 12, 2019
@swankjesse
Copy link
Member

Our rules are pretty simple: Moshi wants to both read and write JSON properties. If you have a property whose API permits writes but not reads, that’s a problem because it prevents you from testing your JSON.

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

No branches or pull requests

3 participants