Skip to content

Commit

Permalink
Merge pull request #1130 from swagger-api/Fyro-Ing-issues/1054
Browse files Browse the repository at this point in the history
Fyro ing issues/1054
  • Loading branch information
HugoMario authored Apr 6, 2023
2 parents b185e5f + 31a85f3 commit 85541d1
Show file tree
Hide file tree
Showing 128 changed files with 983 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,26 @@ public CharSequence getModelImports(Map<String, Object> templateData, Options op
boolean withXml = Boolean.valueOf(String.valueOf(templateData.get("withXml")));
boolean parcelableModel = Boolean.valueOf(String.valueOf(templateData.get("parcelableModel")));
boolean useBeanValidation = Boolean.valueOf(String.valueOf(templateData.get("useBeanValidation")));
boolean jakarta = Boolean.valueOf(String.valueOf(templateData.get("jakarta")));
if (serializableMode) {
builder.append("import java.io.Serializable;\n");
}
if (jackson && withXml) {
builder.append("import com.fasterxml.jackson.dataformat.xml.annotation.*;\n");
}
if (withXml) {
builder.append("import javax.xml.bind.annotation.*;\n");
if (withXml && jakarta) {
builder.append("import jakarta.xml.bind.annotation.*;\n");
} else if (withXml) {
builder.append("import jakarta.xml.bind.annotation.*;\n");
}
if (parcelableModel) {
builder.append("import android.os.Parcelable;\n");
builder.append("import android.os.Parcel;\n");
}
if (useBeanValidation) {
if (useBeanValidation && jakarta) {
builder.append("import jakarta.validation.constraints.*;\n");
builder.append("import jakarta.validation.Valid;\n");
} else if (useBeanValidation) {
builder.append("import javax.validation.constraints.*;\n");
builder.append("import javax.validation.Valid;\n");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package io.swagger.codegen.v3.generators.java;

import static io.swagger.codegen.v3.CodegenConstants.HAS_ENUMS_EXT_NAME;
import static io.swagger.codegen.v3.CodegenConstants.IS_ENUM_EXT_NAME;
import static io.swagger.codegen.v3.generators.features.NotNullAnnotationFeatures.NOT_NULL_JACKSON_ANNOTATION;
import static io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.getBooleanValue;

import com.github.jknack.handlebars.Handlebars;
import io.swagger.codegen.v3.CliOption;
import io.swagger.codegen.v3.CodegenArgument;
Expand All @@ -26,11 +31,6 @@
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -42,11 +42,10 @@
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;

import static io.swagger.codegen.v3.CodegenConstants.HAS_ENUMS_EXT_NAME;
import static io.swagger.codegen.v3.CodegenConstants.IS_ENUM_EXT_NAME;
import static io.swagger.codegen.v3.generators.features.NotNullAnnotationFeatures.NOT_NULL_JACKSON_ANNOTATION;
import static io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.getBooleanValue;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractJavaCodegen extends DefaultCodegenConfig {
private static Logger LOGGER = LoggerFactory.getLogger(AbstractJavaCodegen.class);
Expand All @@ -62,6 +61,8 @@ public abstract class AbstractJavaCodegen extends DefaultCodegenConfig {

public static final String WIREMOCK_OPTION = "wiremock";

public static final String JAKARTA = "jakarta";

protected String dateLibrary = "threetenbp";
protected boolean java8Mode = false;
protected boolean java11Mode = false;
Expand Down Expand Up @@ -93,6 +94,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegenConfig {
protected String apiDocPath = "docs/";
protected String modelDocPath = "docs/";
protected boolean supportJava6= false;
protected boolean jakarta = false;
private NotNullAnnotationFeatures notNullOption;

public AbstractJavaCodegen() {
Expand Down Expand Up @@ -194,6 +196,15 @@ public AbstractJavaCodegen() {
cliOptions.add(CliOption.newBoolean(CHECK_DUPLICATED_MODEL_NAME, "Check if there are duplicated model names (ignoring case)"));

cliOptions.add(CliOption.newBoolean(WIREMOCK_OPTION, "Use wiremock to generate endpoint calls to mock on generated tests."));

cliOptions.add(CliOption.newBoolean(JAKARTA, "Use Jakarta EE (package jakarta.*) instead of Java EE (javax.*)"));

CliOption jeeSpec = CliOption.newBoolean(JAKARTA, "Use Jakarta EE (package jakarta.*) instead of Java EE (javax.*)");
Map<String, String> jeeSpecModeOptions = new HashMap<String, String>();
jeeSpecModeOptions.put("true", "Use Jakarta EE (package jakarta.*)");
jeeSpecModeOptions.put("false", "Use Java EE (javax.*)");
jeeSpec.setEnum(jeeSpecModeOptions);
cliOptions.add(jeeSpec);
}

@Override
Expand Down Expand Up @@ -490,6 +501,11 @@ public void processOpts() {
} else if (dateLibrary.equals("legacy")) {
additionalProperties.put("legacyDates", true);
}

if (additionalProperties.containsKey(JAKARTA)) {
setJakarta(Boolean.parseBoolean(String.valueOf(additionalProperties.get(JAKARTA))));
additionalProperties.put(JAKARTA, jakarta);
}
}

private void sanitizeConfig() {
Expand Down Expand Up @@ -1564,6 +1580,10 @@ public void setJava11Mode(boolean java11Mode) {
this.java11Mode = java11Mode;
}

public void setJakarta(boolean jakarta) {
this.jakarta = jakarta;
}

@Override
public String escapeQuotationMark(String input) {
// remove " to avoid code injection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import io.swagger.codegen.v3.CodegenProperty;
import io.swagger.codegen.v3.SupportingFile;
import io.swagger.codegen.v3.generators.features.BeanValidationFeatures;
import org.apache.commons.lang3.StringUtils;

import java.io.File;

/**
Expand Down Expand Up @@ -48,7 +46,14 @@ public void processOpts() {

super.processOpts();

importMapping.put("Valid", "javax.validation.Valid");
if (additionalProperties.containsKey(JAKARTA)) {
setJakarta(convertPropertyToBoolean(JAKARTA));
}
if (jakarta) {
importMapping.put("Valid", "jakarta.validation.Valid");
} else {
importMapping.put("Valid", "javax.validation.Valid");
}

// Three API templates to support CDI injection
apiTemplateFiles.put("apiService.mustache", ".java");
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/handlebars/Java/ApiClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ import com.sun.jersey.api.client.WebResource.Builder;
import com.sun.jersey.multipart.FormDataMultiPart;
import com.sun.jersey.multipart.file.FileDataBodyPart;

{{#jakarta}}
import jakarta.ws.rs.core.Response.Status.Family;
import jakarta.ws.rs.core.MediaType;
{{/jakarta}}
{{^jakarta}}
import javax.ws.rs.core.Response.Status.Family;
import javax.ws.rs.core.MediaType;
{{/jakarta}}

import java.util.Collection;
import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package {{invokerPackage}};

import java.util.Set;

{{#jakarta}}
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ValidationException;
{{/jakarta}}
{{^jakarta}}
import javax.validation.ConstraintViolation;
import javax.validation.ValidationException;
{{/jakarta}}

public class BeanValidationException extends ValidationException {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repositories {
ext {
swagger_annotations_version = "{{#useOas2}}1.5.24{{/useOas2}}{{^useOas2}}2.0.0{{/useOas2}}"
jackson_version = "{{^threetenbp}}2.11.4{{/threetenbp}}{{#threetenbp}}2.6.4{{/threetenbp}}"
jersey_version = "1.19.4"
jersey_version = "{{#jakarta}}3.1.0{{/jakarta}}{{^jakarta}}1.19.4{{/jakarta}}"
jodatime_version = "2.10.5"
junit_version = "4.13.1"
}
Expand All @@ -38,7 +38,7 @@ dependencies {
{{#threetenbp}}
implementation "com.github.joschi.jackson:jackson-datatype-threetenbp:$jackson_version"
{{/threetenbp}}
implementation 'com.sun.xml.ws:jaxws-rt:2.3.3'
implementation 'com.sun.xml.ws:jaxws-rt:{{#jakarta}}4.0.0{{/jakarta}}{{^jakarta}}2.3.3{{/jakarta}}'
testImplementation "junit:junit:$junit_version"
{{#wiremock}}
testImplementation "com.github.tomakehurst:wiremock:2.27.2"
Expand Down Expand Up @@ -73,4 +73,4 @@ publishing {

tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
}
4 changes: 2 additions & 2 deletions src/main/resources/handlebars/Java/build.gradle.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ if(hasProperty('target') && target == 'android') {
}

dependencies {
provided 'javax.annotation:jsr250-api:1.0'
provided 'jakarta.annotation:jakarta.annotation-api:{{#jakarta}}2.1.1{{/jakarta}}{{^jakarta}}1.3.5{{/jakarta}}'
}
}

Expand Down Expand Up @@ -113,7 +113,7 @@ ext {
swagger_annotations_version = "2.0.0"
{{/useOas2}}
jackson_version = "2.10.1"
jersey_version = "1.19.4"
jersey_version = "{{#jakarta}}3.1.0{{/jakarta}}{{^jakarta}}1.19.4{{/jakarta}}"
jodatime_version = "2.9.9"
junit_version = "4.12"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{^hideGenerationTimestamp}}@javax.annotation.Generated(value = "{{generatorClass}}", date = "{{generatedDate}}"){{/hideGenerationTimestamp}}
{{^hideGenerationTimestamp}}@{{#jakarta}}jakarta{{/jakarta}}{{^jakarta}}javax{{/jakarta}}.annotation.Generated(value = "{{generatorClass}}", date = "{{generatedDate}}"){{/hideGenerationTimestamp}}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ dependencies {
}
implementation "org.json:json:20180130"
implementation "com.brsanthu:migbase64:2.2"
implementation "com.sun.xml.ws:jaxws-rt:2.3.3"
implementation "com.sun.xml.ws:jaxws-rt:{{#jakarta}}4.0.0{{/jakarta}}{{^jakarta}}2.3.3{{/jakarta}}"
testImplementation "junit:junit:$junit_version"
testImplementation "com.squareup.okhttp3:mockwebserver:3.6.0"
testImplementation "org.assertj:assertj-core:1.7.1"
Expand Down Expand Up @@ -87,4 +87,4 @@ publishing {

tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ if(hasProperty('target') && target == 'android') {
}

dependencies {
provided 'javax.annotation:jsr250-api:1.0'
provided 'jakarta.annotation:jakarta.annotation-api:{{#jakarta}}2.1.1{{/jakarta}}{{^jakarta}}1.3.5{{/jakarta}}'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.3.3</version>
<version>{{#jakarta}}4.0.0{{/jakarta}}{{^jakarta}}2.3.3{{/jakarta}}</version>
<type>pom</type>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
package {{invokerPackage}};

{{#jakarta}}
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.Invocation;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.Form;
import jakarta.ws.rs.core.GenericType;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
{{/jakarta}}
{{^jakarta}}
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
Expand All @@ -10,6 +23,7 @@ import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
{{/jakarta}}

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import com.fasterxml.jackson.datatype.threetenbp.ThreeTenModule;

import java.text.DateFormat;

{{#jakarta}}
import jakarta.ws.rs.ext.ContextResolver;
{{/jakarta}}
{{^jakarta}}
import javax.ws.rs.ext.ContextResolver;
{{/jakarta}}

{{>generatedAnnotation}}
public class JSON implements ContextResolver<ObjectMapper> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import {{invokerPackage}}.ApiClient;
import {{invokerPackage}}.Configuration;
import {{invokerPackage}}.Pair;

{{#jakarta}}
import jakarta.ws.rs.core.GenericType;
{{/jakarta}}
{{^jakarta}}
import javax.ws.rs.core.GenericType;
{{/jakarta}}

{{#imports}}import {{import}};
{{/imports}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repositories {
ext {
swagger_annotations_version = "{{#useOas2}}1.5.24{{/useOas2}}{{^useOas2}}2.0.0{{/useOas2}}"
jackson_version = "2.6.4"
jersey_version = "2.29.1"
jersey_version = "{{#jakarta}}3.1.0{{/jakarta}}{{^jakarta}}2.29.1{{/jakarta}}"
junit_version = "4.13.1"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ if(hasProperty('target') && target == 'android') {
}

dependencies {
provided 'javax.annotation:jsr250-api:1.0'
provided 'jakarta.annotation:jakarta.annotation-api:{{#jakarta}}2.1.1{{/jakarta}}{{^jakarta}}1.3.5{{/jakarta}}'
}
}

Expand Down Expand Up @@ -112,7 +112,7 @@ ext {
swagger_annotations_version = "2.0.0"
{{/useOas2}}
jackson_version = "2.10.1"
jersey_version = "2.26"
jersey_version = "{{#jakarta}}3.1.0{{/jakarta}}{{^jakarta}}2.26{{/jakarta}}"
{{#supportJava6}}
commons_io_version=2.5
commons_lang3_version=3.6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,9 @@
{{#useBeanValidation}}
<!-- Bean Validation API support -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>{{#jakarta}}3.0.2{{/jakarta}}{{^jakarta}}2.0.2{{/jakarta}}</version>
<scope>provided</scope>
</dependency>
{{/useBeanValidation}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,26 @@ import com.google.gson.reflect.TypeToken;
import java.io.IOException;

{{#useBeanValidation}}
{{#jakarta}}
import jakarta.validation.constraints.*;
{{/jakarta}}
{{^jakarta}}
import javax.validation.constraints.*;
{{/jakarta}}
{{/useBeanValidation}}
{{#performBeanValidation}}
{{#jakarta}}
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.ValidatorFactory;
import jakarta.validation.executable.ExecutableValidator;
{{/jakarta}}
{{^jakarta}}
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import javax.validation.executable.ExecutableValidator;
{{/jakarta}}
import java.util.Set;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
Expand Down Expand Up @@ -248,4 +261,4 @@ public class {{classname}} {
{{/contents}}
{{/operation}}
}
{{/operations}}
{{/operations}}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies {
{{#threetenbp}}
implementation 'org.threeten:threetenbp:1.3.5'
{{/threetenbp}}
implementation 'com.sun.xml.ws:jaxws-rt:2.3.3'
implementation 'com.sun.xml.ws:jaxws-rt:{{#jakarta}}4.0.0{{/jakarta}}{{^jakarta}}2.3.3{{/jakarta}}'
testImplementation 'junit:junit:4.13.1'
{{#wiremock}}
testImplementation "com.github.tomakehurst:wiremock:2.27.2"
Expand Down
Loading

0 comments on commit 85541d1

Please sign in to comment.