jsonschema2pojo
jsonschema2pojo copied to clipboard
Add config "extraClassAnnotations" (enabler for Lombok usage)
this PR introduces a new config extraClassAnnotations
that allows the user to specify fully qualified class names of annotations that should be put on all classes during code generation.
its primary motivation is to enable the usage of lombok without this library depending on lombok. and hence the generic solution approach. if wanted, the user can extend the use case to other annotations.
the main work is in core module. maven, cli, ant and gradle modules are adjusted accordingly to enable this config with all entry points. the following examples are for the maven plugin.
as an example, in order to use it with lombok, the following configurations can be included:
<configuration>
....
<!-- either getters or setters have to be enabled, because jsonschema2pojo makes the fields public otherwise -->
<includeGetters>true</includeGetters>
<includeSetters>false</includeSetters>
<includeToString>false</includeToString>
<includeHashcodeAndEquals>false</includeHashcodeAndEquals>
<extraClassAnnotations>
<annotation>lombok.NoArgsConstructor</annotation>
<annotation>lombok.Setter</annotation>
<annotation>lombok.ToString</annotation>
<annotation>lombok.EqualsAndHashCode</annotation>
</extraClassAnnotations>
....
</configuration>
if having public fields is not an issue, another example would be:
<configuration>
....
<includeGetters>false</includeGetters>
<includeSetters>false</includeSetters>
<includeToString>false</includeToString>
<includeHashcodeAndEquals>false</includeHashcodeAndEquals>
<extraClassAnnotations>
<annotation>lombok.Data</annotation>
</extraClassAnnotations>
....
</configuration>
keep in mind that lombok (or the annotation the user is trying to add) should be included in the project executing this plugin.
References:
- https://github.com/joelittlejohn/jsonschema2pojo/issues/166
- https://github.com/joelittlejohn/jsonschema2pojo/issues/166#issuecomment-1079443937
@joelittlejohn please let me know if there is anything i missed or should be improved.
@joelittlejohn Why doesn't this getting merged already?
@goekay the PR shows as out of sync with base branch, any chance you can update the PR? @joelittlejohn is there anything else required to get this feature merged?
I really like the concept of Lombok integration and our team would greatly benefit from it as well.
@princefisher done, thanks for the heads up.
Any update on this feature? I would also like to add a property that will override the field access regardless if includeGetters and includeSetters is false.
Currently I had to use an RuleFactory and PropertyRule just to have private fields if I disabled the getters and setters. I also had to implement an AbstractAnnator.
In my case I would want JMod.PRIVATE since Lombok would be generating the accessors.
Jmod.NONE could also be used with @FieldDefaults(level=AccessLevel.PRIVATE)
@goekay Will this PR handle annotations with properties like @Accessors(fluent = true, chain = true) or @FieldDefaults(level=AccessLevel.PRIVATE)?
@octgsoftware no. the properties of the annotations are not handled.
this is where i give up.
@joelittlejohn @goekay Is it possible if we take another go at this?