jsonschema2pojo icon indicating copy to clipboard operation
jsonschema2pojo copied to clipboard

Add config "extraClassAnnotations" (enabler for Lombok usage)

Open goekay opened this issue 2 years ago • 7 comments

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

goekay avatar Apr 12 '22 19:04 goekay

@joelittlejohn please let me know if there is anything i missed or should be improved.

goekay avatar May 17 '22 14:05 goekay

@joelittlejohn Why doesn't this getting merged already?

migroskub avatar May 27 '22 15:05 migroskub

@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 avatar Jun 16 '22 16:06 princefisher

@princefisher done, thanks for the heads up.

goekay avatar Jun 16 '22 19:06 goekay

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)

octgsoftware avatar Aug 14 '22 23:08 octgsoftware

@goekay Will this PR handle annotations with properties like @Accessors(fluent = true, chain = true) or @FieldDefaults(level=AccessLevel.PRIVATE)?

octgsoftware avatar Aug 14 '22 23:08 octgsoftware

@octgsoftware no. the properties of the annotations are not handled.

goekay avatar Aug 15 '22 14:08 goekay

this is where i give up.

goekay avatar Oct 13 '23 08:10 goekay

@joelittlejohn @goekay Is it possible if we take another go at this?

darkmastermindz avatar Dec 22 '23 22:12 darkmastermindz