mirror of
https://github.com/jlengrand/openapi-generator.git
synced 2026-05-13 00:21:18 +00:00
[Jaxrs-cxf] Add check for useGenericResponse for jaxrs-cxf server + client (#4779)
* add check for useGenericResponse for jaxrs-cxf * move check for genericresponse to cxf codegen #4713
This commit is contained in:
@@ -1,15 +1,25 @@
|
||||
package io.swagger.codegen.languages;
|
||||
|
||||
import io.swagger.codegen.*;
|
||||
import io.swagger.codegen.languages.features.BeanValidationFeatures;
|
||||
import io.swagger.models.Operation;
|
||||
import io.swagger.models.Path;
|
||||
import io.swagger.models.Swagger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.*;
|
||||
import io.swagger.codegen.CliOption;
|
||||
import io.swagger.codegen.CodegenConstants;
|
||||
import io.swagger.codegen.CodegenOperation;
|
||||
import io.swagger.codegen.CodegenParameter;
|
||||
import io.swagger.codegen.CodegenResponse;
|
||||
import io.swagger.codegen.CodegenType;
|
||||
import io.swagger.codegen.languages.features.BeanValidationFeatures;
|
||||
import io.swagger.codegen.languages.features.UseGenericResponseFeatures;
|
||||
import io.swagger.models.Operation;
|
||||
import io.swagger.models.Path;
|
||||
import io.swagger.models.Swagger;
|
||||
|
||||
public abstract class AbstractJavaJAXRSServerCodegen extends AbstractJavaCodegen implements BeanValidationFeatures {
|
||||
/**
|
||||
@@ -25,8 +35,7 @@ public abstract class AbstractJavaJAXRSServerCodegen extends AbstractJavaCodegen
|
||||
|
||||
static Logger LOGGER = LoggerFactory.getLogger(AbstractJavaJAXRSServerCodegen.class);
|
||||
|
||||
public AbstractJavaJAXRSServerCodegen()
|
||||
{
|
||||
public AbstractJavaJAXRSServerCodegen() {
|
||||
super();
|
||||
|
||||
sourceFolder = "src/gen/java";
|
||||
@@ -46,7 +55,6 @@ public abstract class AbstractJavaJAXRSServerCodegen extends AbstractJavaCodegen
|
||||
|
||||
cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations"));
|
||||
cliOptions.add(new CliOption("serverPort", "The port on which the server should be started"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -55,8 +63,7 @@ public abstract class AbstractJavaJAXRSServerCodegen extends AbstractJavaCodegen
|
||||
// ===============
|
||||
|
||||
@Override
|
||||
public CodegenType getTag()
|
||||
{
|
||||
public CodegenType getTag() {
|
||||
return CodegenType.SERVER;
|
||||
}
|
||||
|
||||
@@ -84,7 +91,7 @@ public abstract class AbstractJavaJAXRSServerCodegen extends AbstractJavaCodegen
|
||||
swagger.setBasePath("");
|
||||
}
|
||||
|
||||
if(!this.additionalProperties.containsKey("serverPort")) {
|
||||
if (!this.additionalProperties.containsKey("serverPort")) {
|
||||
final String host = swagger.getHost();
|
||||
String port = "8080"; // Default value for a JEE Server
|
||||
if ( host != null ) {
|
||||
@@ -235,4 +242,5 @@ public abstract class AbstractJavaJAXRSServerCodegen extends AbstractJavaCodegen
|
||||
this.useBeanValidation = useBeanValidation;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -11,19 +11,22 @@ import org.slf4j.LoggerFactory;
|
||||
import io.swagger.codegen.CliOption;
|
||||
import io.swagger.codegen.CodegenModel;
|
||||
import io.swagger.codegen.CodegenOperation;
|
||||
import io.swagger.codegen.CodegenParameter;
|
||||
import io.swagger.codegen.CodegenProperty;
|
||||
import io.swagger.codegen.CodegenResponse;
|
||||
import io.swagger.codegen.CodegenType;
|
||||
import io.swagger.codegen.SupportingFile;
|
||||
import io.swagger.codegen.languages.features.BeanValidationFeatures;
|
||||
import io.swagger.codegen.languages.features.GzipTestFeatures;
|
||||
import io.swagger.codegen.languages.features.JaxbFeatures;
|
||||
import io.swagger.codegen.languages.features.LoggingTestFeatures;
|
||||
import io.swagger.codegen.languages.features.UseGenericResponseFeatures;
|
||||
import io.swagger.models.Operation;
|
||||
|
||||
public class JavaCXFClientCodegen extends AbstractJavaCodegen
|
||||
implements BeanValidationFeatures, JaxbFeatures, GzipTestFeatures, LoggingTestFeatures
|
||||
{
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(JavaCXFClientCodegen.class);
|
||||
implements BeanValidationFeatures, UseGenericResponseFeatures, JaxbFeatures, GzipTestFeatures, LoggingTestFeatures {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(JavaCXFClientCodegen.class);
|
||||
|
||||
/**
|
||||
* Name of the sub-directory in "src/main/resource" where to find the
|
||||
@@ -34,6 +37,8 @@ public class JavaCXFClientCodegen extends AbstractJavaCodegen
|
||||
protected boolean useJaxbAnnotations = true;
|
||||
|
||||
protected boolean useBeanValidation = false;
|
||||
|
||||
protected boolean useGenericResponse = false;
|
||||
|
||||
protected boolean useGzipFeatureForTests = false;
|
||||
|
||||
@@ -75,7 +80,7 @@ public class JavaCXFClientCodegen extends AbstractJavaCodegen
|
||||
cliOptions.add(CliOption.newBoolean(USE_GZIP_FEATURE_FOR_TESTS, "Use Gzip Feature for tests"));
|
||||
cliOptions.add(CliOption.newBoolean(USE_LOGGING_FEATURE_FOR_TESTS, "Use Logging Feature for tests"));
|
||||
|
||||
|
||||
cliOptions.add(CliOption.newBoolean(USE_GENERIC_RESPONSE, "Use generic response"));
|
||||
}
|
||||
|
||||
|
||||
@@ -93,6 +98,14 @@ public class JavaCXFClientCodegen extends AbstractJavaCodegen
|
||||
boolean useBeanValidationProp = convertPropertyToBooleanAndWriteBack(USE_BEANVALIDATION);
|
||||
this.setUseBeanValidation(useBeanValidationProp);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(USE_GENERIC_RESPONSE)) {
|
||||
this.setUseGenericResponse(convertPropertyToBoolean(USE_GENERIC_RESPONSE));
|
||||
}
|
||||
|
||||
if (useGenericResponse) {
|
||||
writePropertyBack(USE_GENERIC_RESPONSE, useGenericResponse);
|
||||
}
|
||||
|
||||
this.setUseGzipFeatureForTests(convertPropertyToBooleanAndWriteBack(USE_GZIP_FEATURE_FOR_TESTS));
|
||||
this.setUseLoggingFeatureForTests(convertPropertyToBooleanAndWriteBack(USE_LOGGING_FEATURE_FOR_TESTS));
|
||||
@@ -132,6 +145,28 @@ public class JavaCXFClientCodegen extends AbstractJavaCodegen
|
||||
model.imports.remove("ToStringSerializer");
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
|
||||
objs = super.postProcessOperations(objs);
|
||||
|
||||
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
|
||||
if (operations != null) {
|
||||
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
|
||||
for (CodegenOperation operation : ops) {
|
||||
|
||||
if (operation.returnType == null) {
|
||||
operation.returnType = "void";
|
||||
// set vendorExtensions.x-java-is-response-void to true as
|
||||
// returnType is set to "void"
|
||||
operation.vendorExtensions.put("x-java-is-response-void", true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return operations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelp()
|
||||
{
|
||||
@@ -155,4 +190,8 @@ public class JavaCXFClientCodegen extends AbstractJavaCodegen
|
||||
this.useLoggingFeatureForTests = useLoggingFeatureForTests;
|
||||
}
|
||||
|
||||
public void setUseGenericResponse(boolean useGenericResponse) {
|
||||
this.useGenericResponse = useGenericResponse;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,10 +17,11 @@ import io.swagger.codegen.languages.features.CXFServerFeatures;
|
||||
import io.swagger.codegen.languages.features.GzipTestFeatures;
|
||||
import io.swagger.codegen.languages.features.JaxbFeatures;
|
||||
import io.swagger.codegen.languages.features.LoggingTestFeatures;
|
||||
import io.swagger.codegen.languages.features.UseGenericResponseFeatures;
|
||||
import io.swagger.models.Operation;
|
||||
|
||||
public class JavaCXFServerCodegen extends AbstractJavaJAXRSServerCodegen
|
||||
implements CXFServerFeatures, GzipTestFeatures, LoggingTestFeatures, JaxbFeatures
|
||||
implements CXFServerFeatures, GzipTestFeatures, LoggingTestFeatures, JaxbFeatures, UseGenericResponseFeatures
|
||||
{
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(JavaCXFServerCodegen.class);
|
||||
|
||||
@@ -58,6 +59,8 @@ public class JavaCXFServerCodegen extends AbstractJavaJAXRSServerCodegen
|
||||
|
||||
protected boolean generateNonSpringApplication = false;
|
||||
|
||||
protected boolean useGenericResponse = false;
|
||||
|
||||
public JavaCXFServerCodegen()
|
||||
{
|
||||
super();
|
||||
@@ -111,6 +114,8 @@ public class JavaCXFServerCodegen extends AbstractJavaJAXRSServerCodegen
|
||||
cliOptions.add(CliOption.newBoolean(USE_ANNOTATED_BASE_PATH, "Use @Path annotations for basePath"));
|
||||
|
||||
cliOptions.add(CliOption.newBoolean(GENERATE_NON_SPRING_APPLICATION, "Generate non-Spring application"));
|
||||
cliOptions.add(CliOption.newBoolean(USE_GENERIC_RESPONSE, "Use generic response"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -127,6 +132,14 @@ public class JavaCXFServerCodegen extends AbstractJavaJAXRSServerCodegen
|
||||
if (additionalProperties.containsKey(ADD_CONSUMES_PRODUCES_JSON)) {
|
||||
this.setAddConsumesProducesJson(convertPropertyToBooleanAndWriteBack(ADD_CONSUMES_PRODUCES_JSON));
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(USE_GENERIC_RESPONSE)) {
|
||||
this.setUseGenericResponse(convertPropertyToBoolean(USE_GENERIC_RESPONSE));
|
||||
}
|
||||
|
||||
if (useGenericResponse) {
|
||||
writePropertyBack(USE_GENERIC_RESPONSE, useGenericResponse);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(GENERATE_SPRING_APPLICATION)) {
|
||||
this.setGenerateSpringApplication(convertPropertyToBooleanAndWriteBack(GENERATE_SPRING_APPLICATION));
|
||||
@@ -306,5 +319,9 @@ public class JavaCXFServerCodegen extends AbstractJavaJAXRSServerCodegen
|
||||
public void setGenerateNonSpringApplication(boolean generateNonSpringApplication) {
|
||||
this.generateNonSpringApplication = generateNonSpringApplication;
|
||||
}
|
||||
|
||||
public void setUseGenericResponse(boolean useGenericResponse) {
|
||||
this.useGenericResponse = useGenericResponse;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package io.swagger.codegen.languages.features;
|
||||
|
||||
public interface UseGenericResponseFeatures {
|
||||
|
||||
// Language supports generating generic Jaxrs or native return types
|
||||
public static final String USE_GENERIC_RESPONSE = "useGenericResponse";
|
||||
|
||||
public void setUseGenericResponse(boolean useGenericResponse);
|
||||
}
|
||||
@@ -31,7 +31,7 @@ public class {{classname}}ServiceImpl implements {{classname}} {
|
||||
public {{>returnTypes}} {{nickname}}({{#allParams}}{{>queryParamsImpl}}{{>pathParamsImpl}}{{>headerParamsImpl}}{{>bodyParamsImpl}}{{>formParamsImpl}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {
|
||||
// TODO: Implement...
|
||||
|
||||
{{^vendorExtensions.x-java-is-response-void}}return null;{{/vendorExtensions.x-java-is-response-void}}
|
||||
{{#useGenericResponse}}return Response.ok().entity("magic!").build();{{/useGenericResponse}}{{^useGenericResponse}}{{^vendorExtensions.x-java-is-response-void}}return null;{{/vendorExtensions.x-java-is-response-void}}{{/useGenericResponse}}
|
||||
}
|
||||
|
||||
{{/operation}}
|
||||
|
||||
@@ -26,12 +26,16 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
|
||||
}
|
||||
|
||||
@Override
|
||||
{{#jackson}}
|
||||
@JsonValue
|
||||
{{/jackson}}
|
||||
public String toString() {
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
{{#jackson}}
|
||||
@JsonCreator
|
||||
{{/jackson}}
|
||||
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue(String text) {
|
||||
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
|
||||
if (String.valueOf(b.value).equals(text)) {
|
||||
@@ -40,4 +44,5 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1 +1,4 @@
|
||||
{{#returnContainer}}{{#isMapContainer}}Map<String, {{{returnType}}}>{{/isMapContainer}}{{#isListContainer}}List<{{{returnType}}}>{{/isListContainer}}{{/returnContainer}}{{^returnContainer}}{{{returnType}}}{{/returnContainer}}
|
||||
{{#useGenericResponse}}Response{{/useGenericResponse}}{{! non-generic response:
|
||||
}}{{^useGenericResponse}}{{!
|
||||
}}{{#returnContainer}}{{#isMapContainer}}Map<String, {{{returnType}}}>{{/isMapContainer}}{{#isListContainer}}List<{{{returnType}}}>{{/isListContainer}}{{/returnContainer}}{{^returnContainer}}{{{returnType}}}{{/returnContainer}}{{!
|
||||
}}{{/useGenericResponse}}
|
||||
@@ -4,6 +4,7 @@ import io.swagger.codegen.AbstractOptionsTest;
|
||||
import io.swagger.codegen.CodegenConfig;
|
||||
import io.swagger.codegen.languages.JavaCXFClientCodegen;
|
||||
import io.swagger.codegen.options.JavaCXFClientOptionsProvider;
|
||||
import io.swagger.codegen.options.JavaCXFServerOptionsProvider;
|
||||
import io.swagger.codegen.options.OptionsProvider;
|
||||
import mockit.Expectations;
|
||||
import mockit.Tested;
|
||||
@@ -59,6 +60,9 @@ public class JaxrsCXFClientOptionsTest extends AbstractOptionsTest {
|
||||
|
||||
clientCodegen.setUseBeanValidation(Boolean.valueOf(JavaCXFClientOptionsProvider.USE_BEANVALIDATION));
|
||||
times = 1;
|
||||
clientCodegen.setUseGenericResponse(Boolean.valueOf(JavaCXFClientOptionsProvider.USE_GENERIC_RESPONSE));
|
||||
times = 1;
|
||||
|
||||
clientCodegen.setUseLoggingFeatureForTests(
|
||||
Boolean.valueOf(JavaCXFClientOptionsProvider.USE_LOGGING_FEATURE_FOR_TESTS));
|
||||
times = 1;
|
||||
|
||||
@@ -80,6 +80,8 @@ public class JaxrsCXFServerOptionsTest extends AbstractOptionsTest {
|
||||
clientCodegen.setUseBeanValidationFeature(
|
||||
Boolean.valueOf(JavaCXFServerOptionsProvider.USE_BEANVALIDATION_FEATURE));
|
||||
times = 1;
|
||||
clientCodegen.setUseGenericResponse(Boolean.valueOf(JavaCXFServerOptionsProvider.USE_GENERIC_RESPONSE));
|
||||
times = 1;
|
||||
|
||||
clientCodegen.setGenerateSpringBootApplication(
|
||||
Boolean.valueOf(JavaCXFServerOptionsProvider.GENERATE_SPRING_BOOT_APPLICATION));
|
||||
|
||||
@@ -16,6 +16,8 @@ public class JavaCXFClientOptionsProvider extends JavaOptionsProvider {
|
||||
|
||||
public static final String USE_LOGGING_FEATURE_FOR_TESTS = "true";
|
||||
|
||||
public static final String USE_GENERIC_RESPONSE = "true";
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isServer() {
|
||||
@@ -36,6 +38,7 @@ public class JavaCXFClientOptionsProvider extends JavaOptionsProvider {
|
||||
.putAll(parentOptions);
|
||||
|
||||
builder.put(JavaCXFClientCodegen.USE_BEANVALIDATION, JavaCXFClientOptionsProvider.USE_BEANVALIDATION);
|
||||
builder.put(JavaCXFClientCodegen.USE_GENERIC_RESPONSE, JavaCXFClientOptionsProvider.USE_GENERIC_RESPONSE);
|
||||
builder.put(JavaCXFClientCodegen.USE_JAXB_ANNOTATIONS, USE_JAXB_ANNOTATIONS);
|
||||
|
||||
builder.put(JavaCXFClientCodegen.USE_GZIP_FEATURE_FOR_TESTS, USE_GZIP_FEATURE_FOR_TESTS);
|
||||
|
||||
@@ -31,6 +31,8 @@ public class JavaCXFServerOptionsProvider extends JavaOptionsProvider {
|
||||
|
||||
public static final String USE_BEANVALIDATION_FEATURE = "true";
|
||||
|
||||
public static final String USE_GENERIC_RESPONSE = "true";
|
||||
|
||||
public static final String USE_SPRING_ANNOTATION_CONFIG = "true";
|
||||
|
||||
public static final String GENERATE_SPRING_BOOT_APPLICATION = "true";
|
||||
@@ -82,6 +84,7 @@ public class JavaCXFServerOptionsProvider extends JavaOptionsProvider {
|
||||
builder.put(JavaCXFServerCodegen.USE_LOGGING_FEATURE, USE_LOGGING_FEATURE);
|
||||
builder.put(JavaCXFServerCodegen.USE_LOGGING_FEATURE_FOR_TESTS, USE_LOGGING_FEATURE_FOR_TESTS);
|
||||
builder.put(JavaCXFServerCodegen.USE_BEANVALIDATION_FEATURE, USE_BEANVALIDATION_FEATURE);
|
||||
builder.put(JavaCXFServerCodegen.USE_GENERIC_RESPONSE, USE_GENERIC_RESPONSE);
|
||||
|
||||
builder.put(JavaCXFServerCodegen.GENERATE_SPRING_BOOT_APPLICATION, GENERATE_SPRING_BOOT_APPLICATION);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user