forked from loafle/openapi-generator-original
Implicit headers for SpringCodegen (#4858)
* Add a new option implicitHeaders with default false * Change JavaSpring templates and codegen to handle implicit headers * Add script to generate sample for implicitHeader option * Add generated implicitHeader sample * Fix alignment
This commit is contained in:
@@ -23,6 +23,7 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
|
||||
public static final String USE_TAGS = "useTags";
|
||||
public static final String SPRING_MVC_LIBRARY = "spring-mvc";
|
||||
public static final String SPRING_CLOUD_LIBRARY = "spring-cloud";
|
||||
public static final String IMPLICIT_HEADERS = "implicitHeaders";
|
||||
|
||||
protected String title = "swagger-petstore";
|
||||
protected String configPackage = "io.swagger.configuration";
|
||||
@@ -34,7 +35,8 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
|
||||
protected boolean async = false;
|
||||
protected String responseWrapper = "";
|
||||
protected boolean useTags = false;
|
||||
protected boolean useBeanValidation = true;
|
||||
protected boolean useBeanValidation = true;
|
||||
protected boolean implicitHeaders = false;
|
||||
|
||||
public SpringCodegen() {
|
||||
super();
|
||||
@@ -63,6 +65,7 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
|
||||
cliOptions.add(new CliOption(RESPONSE_WRAPPER, "wrap the responses in given type (Future,Callable,CompletableFuture,ListenableFuture,DeferredResult,HystrixCommand,RxObservable,RxSingle or fully qualified type)"));
|
||||
cliOptions.add(CliOption.newBoolean(USE_TAGS, "use tags for creating interface and controller classnames"));
|
||||
cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations"));
|
||||
cliOptions.add(CliOption.newBoolean(IMPLICIT_HEADERS, "Use of @ApiImplicitParams for headers."));
|
||||
|
||||
supportedLibraries.put(DEFAULT_LIBRARY, "Spring-boot Server application using the SpringFox integration.");
|
||||
supportedLibraries.put(SPRING_MVC_LIBRARY, "Spring-MVC Server application using the SpringFox integration.");
|
||||
@@ -149,6 +152,11 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
|
||||
writePropertyBack(USE_BEANVALIDATION, useBeanValidation);
|
||||
}
|
||||
|
||||
|
||||
if (additionalProperties.containsKey(IMPLICIT_HEADERS)) {
|
||||
this.setImplicitHeaders(Boolean.valueOf(additionalProperties.get(IMPLICIT_HEADERS).toString()));
|
||||
}
|
||||
|
||||
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
|
||||
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
||||
|
||||
@@ -388,12 +396,36 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
|
||||
operation.returnContainer = "Set";
|
||||
}
|
||||
}
|
||||
|
||||
if(implicitHeaders){
|
||||
removeHeadersFromAllParams(operation.allParams);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return objs;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method removes header parameters from the list of parameters and also
|
||||
* corrects last allParams hasMore state.
|
||||
* @param allParams list of all parameters
|
||||
*/
|
||||
private void removeHeadersFromAllParams(List<CodegenParameter> allParams) {
|
||||
if(allParams.isEmpty()){
|
||||
return;
|
||||
}
|
||||
final ArrayList<CodegenParameter> copy = new ArrayList<>(allParams);
|
||||
allParams.clear();
|
||||
|
||||
for(CodegenParameter p : copy){
|
||||
if(!p.isHeaderParam){
|
||||
allParams.add(p);
|
||||
}
|
||||
}
|
||||
allParams.get(allParams.size()-1).hasMore =false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
|
||||
if(library.equals(SPRING_CLOUD_LIBRARY)) {
|
||||
@@ -446,6 +478,10 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
|
||||
this.useTags = useTags;
|
||||
}
|
||||
|
||||
public void setImplicitHeaders(boolean implicitHeaders) {
|
||||
this.implicitHeaders = implicitHeaders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
|
||||
super.postProcessModelProperty(model, property);
|
||||
@@ -495,4 +531,4 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
|
||||
this.useBeanValidation = useBeanValidation;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user