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.24k stars 1.66k forks source link

Maven plugin's `includeGeneratedAnnotation` does not support `jakarta.annotation.Generated` #1461

Open anno1985 opened 1 year ago

anno1985 commented 1 year ago

When using both includeJsr303Annotations and includeGeneratedAnnotation, it seems one still gets javax.annotation.(processing.)Generated instead of jakarta.annotaiton.Generated.

unkish commented 1 year ago

Hi

I'm not sure whether there was intent/consideration to use/offer @Generated from javax/jakarta annotations package. Current solution is attempting to add javax.annotation.processing.Generated if it's resolvable (java version used to run plugin is 9+) or fallback to javax.annotation.Generated

See: https://github.com/joelittlejohn/jsonschema2pojo/blob/78ec2954c02ec18cfc886aa97c648153dfdca3be/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/AnnotationHelper.java#L25-L26 https://github.com/joelittlejohn/jsonschema2pojo/blob/78ec2954c02ec18cfc886aa97c648153dfdca3be/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/util/AnnotationHelper.java#L42-L46

Please also note that both jsr303 and jsr349 are about bean validation, which would be javax.validation/jakarta.validation

joelittlejohn commented 1 year ago

So I guess javax has become jakarta, and now we have another Generated annotation to throw into the mix :)

unkish commented 1 year ago

Current annotations are part of the JRE, thus shouldn't require additional dependencies. Would throwing in jakarta.annotation in the mix mean yet another flag? As both: includeJsr303Annotations and useJakartaValidation are about validation api: https://github.com/joelittlejohn/jsonschema2pojo/blob/8e772064776c51376d63c35674e6dd5a0d5e123c/pom.xml#L373-L382

Whereas jakarta.annotaiton.Generated would be about annotations:

<dependency>
    <groupId>jakarta.annotation</groupId>
    <artifactId>jakarta.annotation-api</artifactId>
    <version>2.1.1</version>
</dependency>
joelittlejohn commented 1 year ago

:weary:

joelittlejohn commented 1 year ago

It's funny how an annotation is such a trivial thing, just a tag with no behaviour at all, but it's bounced around so many different locations over the years. Why can't they just include this trivial annotation in Java's core API!

eirnym commented 1 year ago

I think it's mostly political reasons between Java Core and J2EE.

darkmastermindz commented 11 months ago

Hi any updates on this? @joelittlejohn This is blocking JDK17+ compatibility which is required for CVE fixes.

GregDThomas commented 9 months ago

FWIW, https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-gradle-plugin/README.adoc has the same problem (i.e. javax vs jakarta) - and overcomes it with a single flag "useJakartaEe= true/false" to determine if it uses javax. or jakarta. style annotations.

GregDThomas commented 9 months ago

In the meantime, I've overcome this issue with the following bit of Gradle;

    // Until https://github.com/joelittlejohn/jsonschema2pojo/issues/1461 is fixed, correct the package for the "@Generated" annotation
    tasks.named('generateJsonSchema2Pojo') {
        finalizedBy('makeJakartaAnnotationsCompatible')
    }

    tasks.register('makeJakartaAnnotationsCompatible') {
        doLast {
            ant.replaceregexp(match:'javax.annotation.processing.Generated', replace:'jakarta.annotation.Generated', flags:'g', byline:true) {
                fileset(dir: 'build/generated-sources/js2p', includes: '**/*.java')
            }
        }
    }
darkmastermindz commented 9 months ago

@joelittlejohn after reading #1586 as well, I'm thinking a PR for an useJakartaAnnotation= true/false or useJakartaEe= true/false to just cover this scenario would be a cleaner solution (similar to #1280) - thoughts on this proposal?

magicDGS commented 6 months ago

Is someboy taking over this issue? It would be great to have a way to fully use jakarta instead of require javax just for the generated annotation. Thanks in advance!