graphql-java-codegen icon indicating copy to clipboard operation
graphql-java-codegen copied to clipboard

Generate models with immutable list / map getters

Open ghost opened this issue 3 years ago • 3 comments

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).

ghost avatar Dec 13 '21 03:12 ghost

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 avatar Dec 15 '21 07:12 jxnu-liguobin

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()?

ghost avatar Dec 15 '21 08:12 ghost

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.

jxnu-liguobin avatar Dec 15 '21 10:12 jxnu-liguobin