Remove JDK7 support from Java Spring generators (#11561)

* remove jdk8 support from spring generators

* update tests, remove commented code in AbstractJavaCodegen

* add back implementation

* add back import

* generate code for non reactive
This commit is contained in:
William Cheng
2022-02-14 17:54:31 +08:00
committed by GitHub
parent 905e59c238
commit 380aaa55a1
344 changed files with 14900 additions and 1109 deletions

View File

@@ -884,13 +884,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
pattern = "new " + arrInstantiationType + "<%s>()";
}
Schema<?> items = getSchemaItems((ArraySchema) schema);
// comment out below for JDK7
//String typeDeclaration = getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, items));
String typeDeclaration = "";
return String.format(Locale.ROOT, pattern, typeDeclaration);
return String.format(Locale.ROOT, pattern, "");
} else if (ModelUtils.isMapSchema(schema) && !(schema instanceof ComposedSchema)) {
if (schema.getProperties() != null && schema.getProperties().size() > 0) {
// object is complex object with free-form additional properties
@@ -907,11 +901,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
return null;
}
// comment out below for JDK7
//String typeDeclaration = String.format(Locale.ROOT, "String, %s", getTypeDeclaration(getAdditionalProperties(schema)));
String typeDeclaration = "";
return String.format(Locale.ROOT, pattern, typeDeclaration);
return String.format(Locale.ROOT, pattern, "");
} else if (ModelUtils.isIntegerSchema(schema)) {
if (schema.getDefault() != null) {
if (SchemaTypeUtil.INTEGER64_FORMAT.equals(schema.getFormat())) {

View File

@@ -79,7 +79,6 @@ public class SpringCodegen extends AbstractJavaCodegen
public static final String VIRTUAL_SERVICE = "virtualService";
public static final String SKIP_DEFAULT_INTERFACE = "skipDefaultInterface";
public static final String JAVA_8 = "java8";
public static final String ASYNC = "async";
public static final String REACTIVE = "reactive";
public static final String RESPONSE_WRAPPER = "responseWrapper";
@@ -103,7 +102,6 @@ public class SpringCodegen extends AbstractJavaCodegen
protected boolean delegatePattern = false;
protected boolean delegateMethod = false;
protected boolean singleContentTypes = false;
protected boolean java8 = true;
protected boolean async = false;
protected boolean reactive = false;
protected String responseWrapper = "";
@@ -274,13 +272,7 @@ public class SpringCodegen extends AbstractJavaCodegen
// Process java8 option before common java ones to change the default
// dateLibrary to java8.
LOGGER.info("----------------------------------");
if (additionalProperties.containsKey(JAVA_8)) {
this.setJava8(Boolean.parseBoolean(additionalProperties.get(JAVA_8).toString()));
additionalProperties.put(JAVA_8, java8);
LOGGER.warn(
"java8 option has been deprecated as it's set to true by default (JDK7 support has been deprecated)");
}
if (java8 && !additionalProperties.containsKey(DATE_LIBRARY)) {
if (!additionalProperties.containsKey(DATE_LIBRARY)) {
setDateLibrary("java8");
}
@@ -425,14 +417,8 @@ public class SpringCodegen extends AbstractJavaCodegen
}
if (interfaceOnly && delegatePattern) {
if (java8) {
delegateMethod = true;
additionalProperties.put("delegate-method", true);
} else {
throw new IllegalArgumentException(
String.format(Locale.ROOT, "Can not generate code with `%s` and `%s` true while `%s` is false.",
DELEGATE_PATTERN, INTERFACE_ONLY, JAVA_8));
}
delegateMethod = true;
additionalProperties.put("delegate-method", true);
}
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
@@ -499,7 +485,7 @@ public class SpringCodegen extends AbstractJavaCodegen
}
}
if ((!delegatePattern && java8) || delegateMethod) {
if (!delegatePattern || delegateMethod) {
additionalProperties.put("jdk8-no-delegate", true);
}
@@ -508,27 +494,22 @@ public class SpringCodegen extends AbstractJavaCodegen
apiTemplateFiles.put("apiDelegate.mustache", "Delegate.java");
}
if (java8) {
additionalProperties.put("javaVersion", "1.8");
if (SPRING_CLOUD_LIBRARY.equals(library)) {
additionalProperties.put("jdk8-default-interface", false);
} else {
additionalProperties.put("jdk8-default-interface", !skipDefaultInterface);
}
additionalProperties.put("jdk8", true);
if (async) {
additionalProperties.put(RESPONSE_WRAPPER, "CompletableFuture");
}
if (reactive) {
additionalProperties.put(RESPONSE_WRAPPER, "Mono");
}
} else if (async) {
additionalProperties.put(RESPONSE_WRAPPER, "Callable");
additionalProperties.put("javaVersion", "1.8");
if (SPRING_CLOUD_LIBRARY.equals(library)) {
additionalProperties.put("jdk8-default-interface", false);
} else {
additionalProperties.put("jdk8-default-interface", !skipDefaultInterface);
}
if (async) {
additionalProperties.put(RESPONSE_WRAPPER, "CompletableFuture");
}
if (reactive) {
additionalProperties.put(RESPONSE_WRAPPER, "Mono");
}
// Some well-known Spring or Spring-Cloud response wrappers
if (isNotEmpty(responseWrapper)) {
additionalProperties.put("jdk8", false);
additionalProperties.put("jdk8-default-interface", false);
switch (responseWrapper) {
case "Future":
@@ -846,10 +827,6 @@ public class SpringCodegen extends AbstractJavaCodegen
this.skipDefaultInterface = skipDefaultInterface;
}
public void setJava8(boolean java8) {
this.java8 = java8;
}
public void setVirtualService(boolean virtualService) {
this.virtualService = virtualService;
}

View File

@@ -64,7 +64,7 @@ import java.util.Optional;
{{/useOptional}}
{{/jdk8-no-delegate}}
{{#async}}
import java.util.concurrent.{{^jdk8}}Callable{{/jdk8}}{{#jdk8}}CompletableFuture{{/jdk8}};
import java.util.concurrent.CompletableFuture;
{{/async}}
import javax.annotation.Generated;

View File

@@ -1,8 +1,8 @@
package {{package}};
{{^jdk8}}
{{#imports}}import {{import}};
{{/imports}}
{{#swagger2AnnotationLibrary}}
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
@@ -12,52 +12,35 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
{{/swagger2AnnotationLibrary}}
{{^swagger1AnnotationLibrary}}
{{#swagger1AnnotationLibrary}}
import io.swagger.annotations.*;
{{/swagger1AnnotationLibrary}}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
{{/jdk8}}
import org.springframework.stereotype.Controller;
{{^jdk8}}
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
{{/jdk8}}
import org.springframework.web.bind.annotation.RequestMapping;
{{^jdk8}}
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
{{/jdk8}}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
{{^isDelegate}}
import org.springframework.web.context.request.NativeWebRequest;
{{/isDelegate}}
{{^jdk8}}
import org.springframework.web.multipart.MultipartFile;
{{#useBeanValidation}}
import javax.validation.constraints.*;
import javax.validation.Valid;
{{/useBeanValidation}}
{{/jdk8}}
{{#jdk8}}
import java.util.Optional;
{{/jdk8}}
{{^jdk8}}
{{#useOptional}}
import java.util.Optional;
{{/useOptional}}
{{/jdk8}}
{{^jdk8}}
import java.util.List;
import java.util.Map;
{{#async}}
import java.util.concurrent.Callable;
{{/async}}
{{/jdk8}}
import java.util.Optional;
import javax.annotation.Generated;
{{>generatedAnnotation}}
@@ -72,7 +55,6 @@ public class {{classname}}Controller implements {{classname}} {
private final {{classname}}Delegate delegate;
public {{classname}}Controller(@Autowired(required = false) {{classname}}Delegate delegate) {
{{#jdk8}}
this.delegate = Optional.ofNullable(delegate).orElse(new {{classname}}Delegate() {});
}
@@ -80,34 +62,25 @@ public class {{classname}}Controller implements {{classname}} {
public {{classname}}Delegate getDelegate() {
return delegate;
}
{{/jdk8}}
{{^jdk8}}
this.delegate = delegate;
}
{{/jdk8}}
{{/isDelegate}}
{{^isDelegate}}
{{^reactive}}
{{^jdk8}}
{{/jdk8}}
private final NativeWebRequest request;
@Autowired
public {{classname}}Controller(NativeWebRequest request) {
this.request = request;
}
{{#jdk8}}
@Override
public Optional<NativeWebRequest> getRequest() {
return Optional.ofNullable(request);
}
{{/jdk8}}
{{/reactive}}
{{/isDelegate}}
{{^jdk8}}
{{^reactive}}
{{#operation}}
/**
* {{httpMethod}} {{{path}}}{{#summary}} : {{.}}{{/summary}}
@@ -153,6 +126,6 @@ public class {{classname}}Controller implements {{classname}} {
}
{{/operation}}
{{/jdk8}}
{{/reactive}}
}
{{/operations}}

View File

@@ -2,14 +2,10 @@ package {{package}};
{{#imports}}import {{import}};
{{/imports}}
{{#jdk8}}
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
{{/jdk8}}
import org.springframework.http.ResponseEntity;
{{#jdk8}}
import org.springframework.web.context.request.NativeWebRequest;
{{/jdk8}}
import org.springframework.web.multipart.MultipartFile;
{{#reactive}}
import org.springframework.web.server.ServerWebExchange;
@@ -20,16 +16,9 @@ import org.springframework.http.codec.multipart.Part;
import java.util.List;
import java.util.Map;
{{#jdk8}}
import java.util.Optional;
{{/jdk8}}
{{^jdk8}}
{{#useOptional}}
import java.util.Optional;
{{/useOptional}}
{{/jdk8}}
{{#async}}
import java.util.concurrent.{{^jdk8}}Callable{{/jdk8}}{{#jdk8}}CompletableFuture{{/jdk8}};
import java.util.concurrent.CompletableFuture;
{{/async}}
import javax.annotation.Generated;

View File

@@ -1,32 +1,27 @@
{{^reactive}}
{{#examples}}
{{#-first}}
{{#jdk8}}
{{#async}}
return CompletableFuture.supplyAsync(()-> {
{{/async}}getRequest().ifPresent(request -> {
{{#async}} {{/async}} {{/jdk8}}for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
{{#async}} {{/async}} for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
{{/-first}}
{{#async}} {{/async}}{{^async}}{{#jdk8}} {{/jdk8}}{{/async}} if (mediaType.isCompatibleWith(MediaType.valueOf("{{{contentType}}}"))) {
{{#async}} {{/async}}{{^async}}{{#jdk8}} {{/jdk8}}{{/async}} String exampleString = {{>exampleString}};
{{#async}} {{/async}}{{^async}}{{#jdk8}} {{/jdk8}}{{/async}} ApiUtil.setExampleResponse(request, "{{{contentType}}}", exampleString);
{{#async}} {{/async}}{{^async}}{{#jdk8}} {{/jdk8}}{{/async}} break;
{{#async}} {{/async}}{{^async}}{{#jdk8}} {{/jdk8}}{{/async}} }
{{#async}} {{/async}}{{^async}} {{/async}} if (mediaType.isCompatibleWith(MediaType.valueOf("{{{contentType}}}"))) {
{{#async}} {{/async}}{{^async}} {{/async}} String exampleString = {{>exampleString}};
{{#async}} {{/async}}{{^async}} {{/async}} ApiUtil.setExampleResponse(request, "{{{contentType}}}", exampleString);
{{#async}} {{/async}}{{^async}} {{/async}} break;
{{#async}} {{/async}}{{^async}} {{/async}} }
{{#-last}}
{{#async}} {{/async}}{{^async}}{{#jdk8}} {{/jdk8}}{{/async}} }
{{#jdk8}}
{{#async}} {{/async}}{{^async}} {{/async}} }
{{#async}} {{/async}} });
{{/jdk8}}
{{#async}} {{/async}} return new ResponseEntity<>({{#returnSuccessCode}}HttpStatus.valueOf({{{statusCode}}}){{/returnSuccessCode}}{{^returnSuccessCode}}HttpStatus.NOT_IMPLEMENTED{{/returnSuccessCode}});
{{#jdk8}}
{{#async}}
}, Runnable::run);
{{/async}}
{{/jdk8}}
{{/-last}}
{{/examples}}
{{^examples}}
return {{#jdk8}}{{#async}}CompletableFuture.completedFuture({{/async}}{{/jdk8}}new ResponseEntity<>({{#returnSuccessCode}}HttpStatus.OK{{/returnSuccessCode}}{{^returnSuccessCode}}HttpStatus.NOT_IMPLEMENTED{{/returnSuccessCode}}){{#jdk8}}{{#async}}){{/async}}{{/jdk8}};
return {{#async}}CompletableFuture.completedFuture({{/async}}new ResponseEntity<>({{#returnSuccessCode}}HttpStatus.OK{{/returnSuccessCode}}{{^returnSuccessCode}}HttpStatus.NOT_IMPLEMENTED{{/returnSuccessCode}}){{#async}}){{/async}};
{{/examples}}
{{/reactive}}
{{#reactive}}
@@ -49,4 +44,4 @@ Mono<Void> result = Mono.empty();
exchange.getResponse().setStatusCode({{#returnSuccessCode}}HttpStatus.OK{{/returnSuccessCode}}{{^returnSuccessCode}}HttpStatus.NOT_IMPLEMENTED{{/returnSuccessCode}});
{{/examples}}
return result.then(Mono.empty());
{{/reactive}}
{{/reactive}}

View File

@@ -10,9 +10,7 @@ import org.openapitools.jackson.nullable.JsonNullable;
{{#serializableModel}}
import java.io.Serializable;
{{/serializableModel}}
{{#jdk8}}
import java.time.OffsetDateTime;
{{/jdk8}}
{{#useBeanValidation}}
import javax.validation.Valid;
import javax.validation.constraints.*;

View File

@@ -250,11 +250,11 @@ public class SpringCodegenTest {
@Test
public void interfaceDefaultImplDisableWithResponseWrapper() {
final SpringCodegen codegen = new SpringCodegen();
codegen.additionalProperties().put(SpringCodegen.JAVA_8, true);
codegen.additionalProperties().put(RESPONSE_WRAPPER, "aWrapper");
codegen.processOpts();
Assert.assertEquals(codegen.additionalProperties().get("jdk8"), false);
// jdk8 tag has been removed
Assert.assertEquals(codegen.additionalProperties().get("jdk8"), null);
}
@Test(expectedExceptions = IllegalArgumentException.class)
@@ -331,7 +331,6 @@ public class SpringCodegenTest {
@Test
public void springcloudWithAsyncAndJava8HasResponseWrapperCompletableFuture() {
final SpringCodegen codegen = new SpringCodegen();
codegen.additionalProperties().put(SpringCodegen.JAVA_8, true);
codegen.additionalProperties().put(SpringCodegen.ASYNC, true);
codegen.additionalProperties().put(CodegenConstants.LIBRARY, "spring-cloud");
codegen.processOpts();
@@ -340,22 +339,9 @@ public class SpringCodegenTest {
Assert.assertEquals(codegen.additionalProperties().get(RESPONSE_WRAPPER), "CompletableFuture");
}
@Test
public void springcloudWithAsyncHasResponseWrapperCallable() {
final SpringCodegen codegen = new SpringCodegen();
codegen.additionalProperties().put(SpringCodegen.JAVA_8, false);
codegen.additionalProperties().put(SpringCodegen.ASYNC, true);
codegen.additionalProperties().put(CodegenConstants.LIBRARY, "spring-cloud");
codegen.processOpts();
Assert.assertNull(codegen.additionalProperties().get("jdk8-default-interface"));
Assert.assertEquals(codegen.additionalProperties().get(RESPONSE_WRAPPER), "Callable");
}
@Test
public void springcloudWithJava8DisableJdk8() {
final SpringCodegen codegen = new SpringCodegen();
codegen.additionalProperties().put(SpringCodegen.JAVA_8, true);
codegen.additionalProperties().put(CodegenConstants.LIBRARY, "spring-cloud");
codegen.processOpts();
@@ -630,7 +616,6 @@ public class SpringCodegenTest {
codegen.setOutputDir(output.getAbsolutePath());
codegen.setUseBeanValidation(useBeanValidation);
codegen.setPerformBeanValidation(performBeanValidation);
codegen.setJava8(java8);
ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);