Skip to content

Commit ad7a820

Browse files
david-noelwing328
authored andcommitted
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
1 parent cccef5d commit ad7a820

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringCodegen.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class SpringCodegen extends AbstractJavaCodegen
3939
protected String basePackage = "io.swagger";
4040
protected boolean interfaceOnly = false;
4141
protected boolean delegatePattern = false;
42+
protected boolean delegateMethod = false;
4243
protected boolean singleContentTypes = false;
4344
protected boolean java8 = false;
4445
protected boolean async = false;
@@ -206,8 +207,14 @@ public void processOpts() {
206207
}
207208

208209
if (this.interfaceOnly && this.delegatePattern) {
209-
throw new IllegalArgumentException(
210-
String.format("Can not generate code with `%s` and `%s` both true.", DELEGATE_PATTERN, INTERFACE_ONLY));
210+
if (this.java8) {
211+
this.delegateMethod = true;
212+
additionalProperties.put("delegate-method", true);
213+
} else {
214+
throw new IllegalArgumentException(
215+
String.format("Can not generate code with `%s` and `%s` true while `%s` is false.",
216+
DELEGATE_PATTERN, INTERFACE_ONLY, JAVA_8));
217+
}
211218
}
212219

213220
if (!this.interfaceOnly) {
@@ -273,12 +280,12 @@ public void processOpts() {
273280
}
274281
}
275282

276-
if (!this.delegatePattern && this.java8) {
283+
if ((!this.delegatePattern && this.java8) || this.delegateMethod) {
277284
additionalProperties.put("jdk8-no-delegate", true);
278285
}
279286

280287

281-
if (this.delegatePattern) {
288+
if (this.delegatePattern && !this.delegateMethod) {
282289
additionalProperties.put("isDelegate", "true");
283290
apiTemplateFiles.put("apiDelegate.mustache", "Delegate.java");
284291
}

modules/swagger-codegen/src/main/resources/JavaSpring/api.mustache

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,14 @@ public interface {{classname}} {
100100
produces = { {{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }, {{/hasProduces}}{{#hasConsumes}}
101101
consumes = { {{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} },{{/hasConsumes}}{{/singleContentTypes}}
102102
method = RequestMethod.{{httpMethod}})
103-
{{#jdk8}}default {{/jdk8}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/allParams}}){{^jdk8}};{{/jdk8}}{{#jdk8}} {
103+
{{#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}} {
104+
{{#delegate-method}}
105+
return {{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
106+
}
107+
108+
// Override this method
109+
default {{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{^isFile}}{{{dataType}}}{{/isFile}}{{#isFile}}MultipartFile{{/isFile}} {{paramName}}{{#hasMore}},{{/hasMore}}{{/allParams}}) {
110+
{{/delegate-method}}
104111
{{^isDelegate}}
105112
if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
106113
{{#examples}}
@@ -125,4 +132,4 @@ public interface {{classname}} {
125132

126133
{{/operation}}
127134
}
128-
{{/operations}}
135+
{{/operations}}

0 commit comments

Comments
 (0)