kobylynskyi / graphql-java-codegen

Make your GraphQL Java application schema-driven.
https://kobylynskyi.github.io/graphql-java-codegen/
MIT License
269 stars 114 forks source link

Generate models with immutable list / map getters #916

Open ghost opened 2 years ago

ghost commented 2 years ago

Is your feature request related to a problem? Please describe. My project is in the stage of code quality checks and associated stuff, and I'm currently using a code scanner that is flagging out the issue that generated model files have getters for list and object types that are not immutable. For example, I'm getting the spotbugs EI_EXPOSE_REP error with lists.

Describe the solution you'd like Is it possible for the generated classes to have list and map attributes be returned in an immutable manner? In my understanding, for lists that would be List.copyOf(listAttribute).

jxnu-liguobin commented 2 years ago

The only problem is that Java does not natively support immutable collections(such as singletonList or Collections.unmodifiableXXX not a good choice). This requires an external library.

ghost commented 2 years ago

The only problem is that Java does not natively support immutable collections(such as singletonList or Collections.unmodifiableXXX not a good choice). This requires an external library.

@jxnu-liguobin would it be possible to do something like List.copyOf()?

jxnu-liguobin commented 2 years ago

But how do I make a list immutable? Immutable collection that avoid modification by throwing exceptions are not a good design. Because this will cause a runtime exception. This is also the default implementation of the Java collection library.