apollo-kotlin icon indicating copy to clipboard operation
apollo-kotlin copied to clipboard

Java codegen properties generated as public

Open stengvac opened this issue 3 years ago • 4 comments

Summary In gradle project with kotlin set codegen option to Java.

apollo {
    generateKotlinModels.set(false)
}

all generated Mutations/Queries/Data are generated with public properties public final String entityId; expected is private and generated getter.

Version 3.3.2 of gradle codegen plugin

Description Tried to use Kotlin code gen, but lib is used in Java project where work with generated objects was hardcore (generic out of bound when generated object were passed into java code - if passed directly into Kotlin code then it worked) so I decided to go for java target code gen. Seems work fine, but fields are public not private with getter.

stengvac avatar Jun 20 '22 13:06 stengvac

Hi 👋 Can you elaborate a bit more? What is the issue with public final fields?

martinbonnin avatar Jun 20 '22 13:06 martinbonnin

Hello,

I would say they are not used in Java that much. Fields are usually private and accessed via getter. It is kind of standard I would say? Also various libs work with getters instead of with directly with fields.

Same goes for Kotlin, which use val/var, but for java those fields are not public but accessed also via generated getters.

stengvac avatar Jun 20 '22 13:06 stengvac

I think the main argument for getters is that you can introduce some logic in the getter if required without breaking the API?

I don't really see this happening in the GraphQL models. These models are very simple and using fields keeps them simple. We could always introduce an option to generate getters but I'm reluctant to add complexity to the codegen unless there is a strong reason to.

Let's keep this issue open see if it receives some interest and revisit in a few weeks/months.

martinbonnin avatar Jun 20 '22 13:06 martinbonnin

Ok.

Thx, for quick reaction.

stengvac avatar Jun 20 '22 13:06 stengvac

Closing this one as this is achievable with the compiler hooks.

BoD avatar Feb 24 '23 09:02 BoD