Skip to content

Commit

Permalink
Merge pull request swagger-api#941 from swagger-api/java-11-support
Browse files Browse the repository at this point in the history
added java 11 support to java generators
  • Loading branch information
HugoMario authored Aug 15, 2021
2 parents a48fc03 + 6e7f175 commit ee42987
Show file tree
Hide file tree
Showing 24 changed files with 234 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ public abstract class AbstractJavaCodegen extends DefaultCodegenConfig {
public static final String DEFAULT_LIBRARY = "<default>";
public static final String DATE_LIBRARY = "dateLibrary";
public static final String JAVA8_MODE = "java8";
public static final String JAVA11_MODE = "java11";
public static final String WITH_XML = "withXml";
public static final String SUPPORT_JAVA6 = "supportJava6";
public static final String ERROR_ON_UNKNOWN_ENUM = "errorOnUnknownEnum";
public static final String CHECK_DUPLICATED_MODEL_NAME = "checkDuplicatedModelName";

protected String dateLibrary = "threetenbp";
protected boolean java8Mode = false;
protected boolean java11Mode = false;
protected boolean withXml = false;
protected String invokerPackage = "io.swagger";
protected String groupId = "io.swagger";
Expand Down Expand Up @@ -165,6 +167,7 @@ public AbstractJavaCodegen() {
CliOption dateLibrary = new CliOption(DATE_LIBRARY, "Option. Date library to use");
Map<String, String> dateOptions = new HashMap<String, String>();
dateOptions.put("java8", "Java 8 native JSR310 (preferred for jdk 1.8+) - note: this also sets \"" + JAVA8_MODE + "\" to true");
dateOptions.put("java11", "Java 11 native JSR384 (preferred for jdk 11+) - note: this also sets \"" + JAVA11_MODE + "\" to true");
dateOptions.put("threetenbp", "Backport of JSR310 (preferred for jdk < 1.8)");
dateOptions.put("java8-localdatetime", "Java 8 using LocalDateTime (for legacy app only)");
dateOptions.put("joda", "Joda (for legacy app only)");
Expand Down Expand Up @@ -424,12 +427,10 @@ public void processOpts() {
// used later in recursive import in postProcessingModels
importMapping.put("com.fasterxml.jackson.annotation.JsonProperty", "com.fasterxml.jackson.annotation.JsonCreator");

if(additionalProperties.containsKey(JAVA8_MODE)) {
setJava8Mode(Boolean.parseBoolean(additionalProperties.get(JAVA8_MODE).toString()));
if ( java8Mode ) {
additionalProperties.put("java8", true);
}
}
setJava8Mode(Boolean.parseBoolean(String.valueOf(additionalProperties.get(JAVA8_MODE))));
additionalProperties.put(JAVA8_MODE, java8Mode);
setJava11Mode(Boolean.parseBoolean(String.valueOf(additionalProperties.get(JAVA11_MODE))));
additionalProperties.put(JAVA11_MODE, java11Mode);

if(additionalProperties.containsKey(WITH_XML)) {
setWithXml(Boolean.parseBoolean(additionalProperties.get(WITH_XML).toString()));
Expand Down Expand Up @@ -1538,6 +1539,10 @@ public void setJava8Mode(boolean enabled) {
this.java8Mode = enabled;
}

public void setJava11Mode(boolean java11Mode) {
this.java11Mode = java11Mode;
}

@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 @@ -68,6 +68,7 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
protected boolean delegateMethod = false;
protected boolean singleContentTypes = false;
protected boolean java8 = false;
protected boolean java11 = false;
protected boolean async = false;
protected String responseWrapper = "";
protected boolean useTags = false;
Expand Down Expand Up @@ -181,10 +182,19 @@ public void processOpts() {
if (!additionalProperties.containsKey(DATE_LIBRARY)) {
setDateLibrary("java8");
}
} else {
this.defaultInterfaces = false;
}

if (additionalProperties.containsKey(JAVA11_MODE)) {
this.setJava11(Boolean.valueOf(additionalProperties.get(JAVA11_MODE).toString()));
}
if (this.java11) {
additionalProperties.put("javaVersion", "11");
additionalProperties.put("jdk11", "true");
}

additionalProperties.put("isJava8or11", this.java8 || this.java11);
this.defaultInterfaces = !(this.java8 || this.java11);

// set invokerPackage as basePackage
if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) {
this.setBasePackage((String) additionalProperties.get(CodegenConstants.INVOKER_PACKAGE));
Expand Down Expand Up @@ -383,7 +393,7 @@ public void processOpts() {
}
}

if ((!this.delegatePattern && this.java8) || this.delegateMethod) {
if ((!this.delegatePattern && (this.java8 || this.java11)) || this.delegateMethod) {
additionalProperties.put("jdk8-no-delegate", true);
}

Expand Down Expand Up @@ -800,6 +810,8 @@ public void setSingleContentTypes(boolean singleContentTypes) {

public void setJava8(boolean java8) { this.java8 = java8; }

public void setJava11(boolean java11) { this.java11 = java11; }

public void setAsync(boolean async) { this.async = async; }

public void setResponseWrapper(String responseWrapper) { this.responseWrapper = responseWrapper; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,19 @@ public class OAuth implements RequestInterceptor {
}
// If first time, get the token
if (expirationTimeMillis == null || System.currentTimeMillis() >= expirationTimeMillis) {
updateAccessToken();
updateAccessToken(template);
}
if (getAccessToken() != null) {
template.header("Authorization", "Bearer " + getAccessToken());
}
}

public synchronized void updateAccessToken() {
public synchronized void updateAccessToken(RequestTemplate template) {
OAuthJSONAccessTokenResponse accessTokenResponse;
try {
accessTokenResponse = oauthClient.accessToken(tokenRequestBuilder.buildBodyMessage());
} catch (Exception e) {
throw new RetryableException(e.getMessage(), e,null);
throw new RetryableException(400, e.getMessage(), template.request().httpMethod(), e, null, template.request());
}
if (accessTokenResponse != null && accessTokenResponse.getAccessToken() != null) {
setAccessToken(accessTokenResponse.getAccessToken(), accessTokenResponse.getExpiresIn());
Expand Down
22 changes: 19 additions & 3 deletions src/main/resources/handlebars/Java/libraries/feign/pom.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,22 @@
</plugins>
</build>
</profile>
{{#java11}}
<profile>
<id>jdk11</id>
<activation>
<jdk>[11,)</jdk>
</activation>
<dependencies>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.3.3</version>
<type>pom</type>
</dependency>
</dependencies>
</profile>
{{/java11}}
</profiles>

<dependencies>
Expand Down Expand Up @@ -279,7 +295,7 @@
</dependency>
</dependencies>
<properties>
<java.version>{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}</java.version>
<java.version>{{#java11}}11{{/java11}}{{^java11}}{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}{{/java11}}</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
{{#useOas2}}
Expand All @@ -288,8 +304,8 @@
{{^useOas2}}
<swagger-core-version>2.0.0</swagger-core-version>
{{/useOas2}}
<feign-version>9.4.0</feign-version>
<feign-form-version>2.1.0</feign-form-version>
<feign-version>11.6</feign-version>
<feign-form-version>3.8.0</feign-form-version>
<jackson-version>2.10.1</jackson-version>
{{#threetenbp}}
<jackson-threetenbp-version>2.6.4</jackson-threetenbp-version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@
</plugins>
</build>
</profile>
{{#java11}}
<profile>
<id>jdk11</id>
<activation>
<jdk>[11,)</jdk>
</activation>
<dependencies>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.3.3</version>
<type>pom</type>
</dependency>
</dependencies>
</profile>
{{/java11}}
</profiles>

<dependencies>
Expand Down Expand Up @@ -276,7 +292,7 @@
</dependency>
</dependencies>
<properties>
<java.version>{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}</java.version>
<java.version>{{#java11}}11{{/java11}}{{^java11}}{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}{{/java11}}</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
{{#useOas2}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,22 @@
</plugins>
</build>
</profile>
{{#java11}}
<profile>
<id>jdk11</id>
<activation>
<jdk>[11,)</jdk>
</activation>
<dependencies>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.3.3</version>
<type>pom</type>
</dependency>
</dependencies>
</profile>
{{/java11}}
</profiles>

<dependencies>
Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/handlebars/Java/libraries/retrofit/pom.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,22 @@
</plugins>
</build>
</profile>
{{#java11}}
<profile>
<id>jdk11</id>
<activation>
<jdk>[11,)</jdk>
</activation>
<dependencies>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.3.3</version>
<type>pom</type>
</dependency>
</dependencies>
</profile>
{{/java11}}
</profiles>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@
</plugins>
</build>
</profile>
{{#java11}}
<profile>
<id>jdk11</id>
<activation>
<jdk>[11,)</jdk>
</activation>
<dependencies>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.3.3</version>
<type>pom</type>
</dependency>
</dependencies>
</profile>
{{/java11}}
</profiles>

<dependencies>
Expand Down Expand Up @@ -323,7 +339,7 @@
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}</java.version>
<java.version>{{#java11}}11{{/java11}}{{^java11}}{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}{{/java11}}</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<gson-fire-version>1.8.0</gson-fire-version>
Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/handlebars/Java/pom.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,22 @@
</plugins>
</build>
</profile>
{{#java11}}
<profile>
<id>jdk11</id>
<activation>
<jdk>[11,)</jdk>
</activation>
<dependencies>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.3.3</version>
<type>pom</type>
</dependency>
</dependencies>
</profile>
{{/java11}}
</profiles>

<dependencies>
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/handlebars/JavaInflector/pom.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@
</repository>
</repositories>
<properties>
{{#java11}}
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
{{/java11}}
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-plugin-version>1.0.0</maven-plugin-version>
<jetty-version>9.4.9.v20180320</jetty-version>
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/handlebars/JavaJaxRS/cxf-cdi/pom.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,11 @@

</dependencies>

{{#java11}}
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
{{/java11}}

</project>
2 changes: 1 addition & 1 deletion src/main/resources/handlebars/JavaJaxRS/cxf/pom.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
</repository>
</repositories>
<properties>
<java.version>{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}</java.version>
<java.version>{{#java11}}11{{/java11}}{{^java11}}{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}{{/java11}}</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
{{#useOas2}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@
</repository>
</repositories>
<properties>
<java.version>{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}</java.version>
<java.version>{{#java11}}11{{/java11}}{{^java11}}{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}{{/java11}}</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
{{#useOas2}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
</repository>
</repositories>
<properties>
<java.version>{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}</java.version>
<java.version>{{#java11}}11{{/java11}}{{^java11}}{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}{{/java11}}</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<swagger-core-version>1.5.18</swagger-core-version>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/handlebars/JavaJaxRS/pom.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@
</repository>
</repositories>
<properties>
<java.version>{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}</java.version>
<java.version>{{#java11}}11{{/java11}}{{^java11}}{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}{{/java11}}</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
{{#useOas2}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}</source>
<target>{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}</target>
<source>{{#java11}}11{{/java11}}{{^java11}}{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}{{/java11}}</source>
<target>{{#java11}}11{{/java11}}{{^java11}}{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}{{/java11}}</target>
</configuration>
</plugin>
<plugin>
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/handlebars/JavaJaxRS/resteasy/pom.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}</source>
<target>{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}</target>
<source>{{#java11}}11{{/java11}}{{^java11}}{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}{{/java11}}</source>
<target>{{#java11}}11{{/java11}}{{^java11}}{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}{{/java11}}</target>
</configuration>
</plugin>
<plugin>
Expand Down
Loading

0 comments on commit ee42987

Please sign in to comment.