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

there are still 2 javax.annotation imports in my generated code after using useJakartaValidation #1574

Closed DenWin closed 12 months ago

DenWin commented 1 year ago

Hi, I using jsonschema2pojo as following:

<plugin>
    <groupId>org.jsonschema2pojo</groupId>
    <artifactId>jsonschema2pojo-maven-plugin</artifactId>
    <version>1.1.2</version>
    <configuration>
        <sourceDirectory>${basedir}/src/main/resources/jsonschema</sourceDirectory>
        <targetPackage>de.example.schema</targetPackage>
        <sourceType>jsonschema</sourceType>
        <dateTimeType>java.time.OffsetDateTime</dateTimeType>
        <includeJsr303Annotations>true</includeJsr303Annotations>
        <includeJsr305Annotations>true</includeJsr305Annotations>
        <generateBuilders>true</generateBuilders>
        <useJakartaValidation>true</useJakartaValidation>
        <removeOldOutput>true</removeOldOutput>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

thought in my generated code I still find the following:

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
...
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;

@JsonProperty("_id")
@NotNull
@Nonnull
private String id;

@Nullable
@JsonProperty("_modtime")
private OffsetDateTime modtime;

I would assume:

  1. For NotNull vs NonNull a Rule similar to ValidRule is required.

    @Override
    public JFieldVar apply(String nodeName, JsonNode node, JsonNode parent, JFieldVar field, Schema currentSchema) {
    if (ruleFactory.getGenerationConfig().isIncludeJsr305Annotations()) {
        final Class<? extends Annotation> notClass
                = ruleFactory.getGenerationConfig().isUseJakartaValidation()
                ? NotNull.class
                : javax.annotation.Nonnull.class;
        field.annotate(notClass);
    }
    return field;
    }

    Well one thing I'm still a little unsure, are perhaps both annotations required?

  2. For Nullable it goes a little further as the jaakarta-version of nullable needs to be added in the import

unkish commented 12 months ago

Hi

useJakartaValidation does not mean that there won't be any javax.* import anymore. Configuration provided has both includeJsr303Annotations (Bean Validation) as well as includeJsr305Annotations (Annotations for Software Defect Detection [status: dormant]) the latter is the "cause" for javax.annotation.* imports whilst former is "driven" by useJakartaValidation configuration option.


Well one thing I'm still a little unsure, are perhaps both annotations required?

It depends whether jsr305 (com.google.code.findbugs:jsr305) annotations are being used for static code analysis in project at hand or not.