Disable jdk8 when using responseWrapper (#2873)

Fix #2872
This commit is contained in:
Thibault Duperron 2019-05-15 15:12:00 +02:00 committed by William Cheng
parent cbe39ba881
commit c3efb91e31
2 changed files with 41 additions and 24 deletions

View File

@ -21,6 +21,7 @@ import com.samskivert.mustache.Mustache;
import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem; import io.swagger.v3.oas.models.PathItem;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import org.openapitools.codegen.*; import org.openapitools.codegen.*;
import org.openapitools.codegen.languages.features.BeanValidationFeatures; import org.openapitools.codegen.languages.features.BeanValidationFeatures;
@ -36,6 +37,7 @@ import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.openapitools.codegen.utils.StringUtils.camelize; import static org.openapitools.codegen.utils.StringUtils.camelize;
public class SpringCodegen extends AbstractJavaCodegen public class SpringCodegen extends AbstractJavaCodegen
@ -377,7 +379,7 @@ public class SpringCodegen extends AbstractJavaCodegen
if (this.java8) { if (this.java8) {
additionalProperties.put("javaVersion", "1.8"); additionalProperties.put("javaVersion", "1.8");
if (!SPRING_CLOUD_LIBRARY.equals(library)) { if (!SPRING_CLOUD_LIBRARY.equals(library)) {
additionalProperties.put("jdk8", "true"); additionalProperties.put("jdk8", true);
} }
if (this.async) { if (this.async) {
additionalProperties.put(RESPONSE_WRAPPER, "CompletableFuture"); additionalProperties.put(RESPONSE_WRAPPER, "CompletableFuture");
@ -395,29 +397,32 @@ public class SpringCodegen extends AbstractJavaCodegen
// Some well-known Spring or Spring-Cloud response wrappers // Some well-known Spring or Spring-Cloud response wrappers
switch (this.responseWrapper) { if (isNotEmpty(this.responseWrapper)) {
case "Future": additionalProperties.put("jdk8", false);
case "Callable": switch (this.responseWrapper) {
case "CompletableFuture": case "Future":
additionalProperties.put(RESPONSE_WRAPPER, "java.util.concurrent." + this.responseWrapper); case "Callable":
break; case "CompletableFuture":
case "ListenableFuture": additionalProperties.put(RESPONSE_WRAPPER, "java.util.concurrent." + this.responseWrapper);
additionalProperties.put(RESPONSE_WRAPPER, "org.springframework.util.concurrent.ListenableFuture"); break;
break; case "ListenableFuture":
case "DeferredResult": additionalProperties.put(RESPONSE_WRAPPER, "org.springframework.util.concurrent.ListenableFuture");
additionalProperties.put(RESPONSE_WRAPPER, "org.springframework.web.context.request.async.DeferredResult"); break;
break; case "DeferredResult":
case "HystrixCommand": additionalProperties.put(RESPONSE_WRAPPER, "org.springframework.web.context.request.async.DeferredResult");
additionalProperties.put(RESPONSE_WRAPPER, "com.netflix.hystrix.HystrixCommand"); break;
break; case "HystrixCommand":
case "RxObservable": additionalProperties.put(RESPONSE_WRAPPER, "com.netflix.hystrix.HystrixCommand");
additionalProperties.put(RESPONSE_WRAPPER, "rx.Observable"); break;
break; case "RxObservable":
case "RxSingle": additionalProperties.put(RESPONSE_WRAPPER, "rx.Observable");
additionalProperties.put(RESPONSE_WRAPPER, "rx.Single"); break;
break; case "RxSingle":
default: additionalProperties.put(RESPONSE_WRAPPER, "rx.Single");
break; break;
default:
break;
}
} }
// add lambda for mustache templates // add lambda for mustache templates

View File

@ -25,6 +25,8 @@ import org.openapitools.codegen.languages.SpringCodegen;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import static org.openapitools.codegen.languages.SpringCodegen.RESPONSE_WRAPPER;
public class SpringCodegenTest { public class SpringCodegenTest {
@Test @Test
@ -110,4 +112,14 @@ public class SpringCodegenTest {
Assert.assertEquals(codegen.additionalProperties().get(SpringCodegen.TITLE), "someTest"); Assert.assertEquals(codegen.additionalProperties().get(SpringCodegen.TITLE), "someTest");
Assert.assertEquals(codegen.additionalProperties().get(SpringCodegen.SERVER_PORT), "8088"); Assert.assertEquals(codegen.additionalProperties().get(SpringCodegen.SERVER_PORT), "8088");
} }
@Test
public void interfaceDefaultImplDisableWithReponseWrapper() {
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);
}
} }