square / moshi

A modern JSON library for Kotlin and Java.
https://square.github.io/moshi/1.x/
Apache License 2.0
9.69k stars 758 forks source link

Mirror @Experimental in generated code #1135

Open Wasabi375 opened 4 years ago

Wasabi375 commented 4 years ago

I want to use moshi to generate adapters for properties that use a custom @Experimental annotation. Currently this doesn't work because the toJson function generated by moshi won't compile as it's missing the OptIn(Experimental::class) annotation.

My suggestion is that moshi could add a CodegenOptIn annotation that adds the required OptIn to the toJson and fromJson functions.

Also if you like the idea I can look into implementing this if you want. Let me know.

JakeWharton commented 4 years ago

I don't really understand why we want a new annotation for this. Seems more straightforward to just mirror the experimental annotation out onto the generated code.

Wasabi375 commented 4 years ago

Yeah, you are totally right. Don't know why thought a separate annotation was necessary.

tomridder commented 1 year ago

I want to use moshi to generate adapters for properties that use a custom @Experimental annotation. Currently this doesn't work because the toJson function generated by moshi won't compile as it's missing the OptIn(Experimental::class) annotation.

My suggestion is that moshi could add a CodegenOptIn annotation that adds the required OptIn to the toJson and fromJson functions.

Also if you like the idea I can look into implementing this if you want. Let me know.

What are the situations where @Experimental annotation needs to be added in the code generated by using @JsonClass? since moshi doesn't support inline class yet

saiimons commented 1 year ago

@tomridder some projects use @RequiresOptIn for some of their code [0], and when annotated classes are part of an API, then one may need to have the JsonAdapter annotated too.

[0] https://github.com/stripe/stripe-android/blob/3c96cf719ca562f36660e2c7489a15d1a3444df0/payments-core/src/main/java/com/stripe/android/ExperimentalPaymentSheetDecouplingApi.kt

ZacSweers commented 1 year ago

PR's welcome on this one 👍