From ad7a8200f085237c20ebf6ef888c2f36d1543d9c Mon Sep 17 00:00:00 2001 From: David Noel Date: Mon, 18 Dec 2017 09:43:15 -0500 Subject: [PATCH] Issue #6471 Add java8, interfaceOnly, delegatePattern to generated server stubs (#6476) * java 8 interface only delegate method mode * typo correction * Update samples with ./bin/spring-all-petstore.sh * change delegate prefix to "do" and make delegate method a default method that returns an OK status code * remove temp debug printout * prefix spring annotated method with "_" to indicate that is private * resolve merge issues from Merge of remote-tracking branch 'upstream/master' * tuned whitespace in api.mustache and re-ran spring-all-petstore.sh * removed special spring cloud library check as it was no longer being referenced in api.mustache re-ran spring-all-petstore.sh --- .../swagger/codegen/languages/SpringCodegen.java | 15 +++++++++++---- .../src/main/resources/JavaSpring/api.mustache | 11 +++++++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringCodegen.java index 703555f1153..a586e4f394e 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringCodegen.java @@ -39,6 +39,7 @@ public class SpringCodegen extends AbstractJavaCodegen protected String basePackage = "io.swagger"; protected boolean interfaceOnly = false; protected boolean delegatePattern = false; + protected boolean delegateMethod = false; protected boolean singleContentTypes = false; protected boolean java8 = false; protected boolean async = false; @@ -206,8 +207,14 @@ public class SpringCodegen extends AbstractJavaCodegen } if (this.interfaceOnly && this.delegatePattern) { - throw new IllegalArgumentException( - String.format("Can not generate code with `%s` and `%s` both true.", DELEGATE_PATTERN, INTERFACE_ONLY)); + if (this.java8) { + this.delegateMethod = true; + additionalProperties.put("delegate-method", true); + } else { + throw new IllegalArgumentException( + String.format("Can not generate code with `%s` and `%s` true while `%s` is false.", + DELEGATE_PATTERN, INTERFACE_ONLY, JAVA_8)); + } } if (!this.interfaceOnly) { @@ -273,12 +280,12 @@ public class SpringCodegen extends AbstractJavaCodegen } } - if (!this.delegatePattern && this.java8) { + if ((!this.delegatePattern && this.java8) || this.delegateMethod) { additionalProperties.put("jdk8-no-delegate", true); } - if (this.delegatePattern) { + if (this.delegatePattern && !this.delegateMethod) { additionalProperties.put("isDelegate", "true"); apiTemplateFiles.put("apiDelegate.mustache", "Delegate.java"); } diff --git a/modules/swagger-codegen/src/main/resources/JavaSpring/api.mustache b/modules/swagger-codegen/src/main/resources/JavaSpring/api.mustache index 58bfee5f8ba..f882182c18b 100644 --- a/modules/swagger-codegen/src/main/resources/JavaSpring/api.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaSpring/api.mustache @@ -100,7 +100,14 @@ public interface {{classname}} { produces = { {{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }, {{/hasProduces}}{{#hasConsumes}} consumes = { {{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} },{{/hasConsumes}}{{/singleContentTypes}} method = RequestMethod.{{httpMethod}}) - {{#jdk8}}default {{/jdk8}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/allParams}}){{^jdk8}};{{/jdk8}}{{#jdk8}} { + {{#jdk8}}default {{/jdk8}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{#delegate-method}}_{{/delegate-method}}{{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/allParams}}){{^jdk8}};{{/jdk8}}{{#jdk8}} { + {{#delegate-method}} + return {{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); + } + + // Override this method + default {{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{^isFile}}{{{dataType}}}{{/isFile}}{{#isFile}}MultipartFile{{/isFile}} {{paramName}}{{#hasMore}},{{/hasMore}}{{/allParams}}) { + {{/delegate-method}} {{^isDelegate}} if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) { {{#examples}} @@ -125,4 +132,4 @@ public interface {{classname}} { {{/operation}} } -{{/operations}} +{{/operations}} \ No newline at end of file