Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove JDK7 support from Java Spring generators #11561

Merged
merged 9 commits into from Feb 14, 2022
  •  
  •  
  •  
Expand Up @@ -884,13 +884,7 @@ public String toDefaultValue(Schema schema) {
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
Expand All @@ -907,11 +901,7 @@ public String toDefaultValue(Schema schema) {
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())) {
Expand Down
Expand Up @@ -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";
Expand All @@ -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 = "";
Expand Down Expand Up @@ -274,13 +272,7 @@ public void processOpts() {
// 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");
}

Expand Down Expand Up @@ -425,14 +417,8 @@ public void processOpts() {
}

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"));
Expand Down Expand Up @@ -499,7 +485,7 @@ public void processOpts() {
}
}

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

Expand All @@ -508,27 +494,22 @@ public void processOpts() {
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":
Expand Down Expand Up @@ -846,10 +827,6 @@ public void setSkipDefaultInterface(boolean skipDefaultInterface) {
this.skipDefaultInterface = skipDefaultInterface;
}

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

public void setVirtualService(boolean virtualService) {
this.virtualService = virtualService;
}
Expand Down
Expand Up @@ -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;

Expand Down
@@ -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;
Expand All @@ -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}}
Expand All @@ -72,42 +55,32 @@ 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() {});
}

@Override
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}}
Expand Down Expand Up @@ -153,6 +126,6 @@ public class {{classname}}Controller implements {{classname}} {
}

{{/operation}}
{{/jdk8}}
{{/reactive}}
}
{{/operations}}
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
@@ -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}}
Expand All @@ -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}}
Expand Up @@ -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.*;
Expand Down