joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
http://www.jsonschema2pojo.org
Apache License 2.0
6.22k stars 1.66k forks source link

Add rule for "const": generate Java constants #1627

Open volomas opened 3 weeks ago

volomas commented 3 weeks ago

As of now, generator doesn't take into account "const" keyword. The value of "const" is simply lost during generation.

In this PR, I've added ConstRule that generates java constant: public static final <Type> <snake_case_var_name> = <value>.

This allows to use this value in code like this:

var myClass = new GeneratedClass();
myClass.setMyProperty(GeneratedClass.MY_PROPERTY);

... or just when I need to refer to that constant somewhere in the code.

It doesn't set the generated field value to "const" value, because it would mimic "default", so I wasn't sure if that's a good idea.