Skip to content

Commit d3355db

Browse files
authored
Merge pull request #9740 from sonOfRa/default-interfaces
Add defaultInterfaces config key to spring generator
2 parents 047b4ac + 727c383 commit d3355db

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class SpringCodegen extends AbstractJavaCodegen
3434
public static final String IMPLICIT_HEADERS = "implicitHeaders";
3535
public static final String SWAGGER_DOCKET_CONFIG = "swaggerDocketConfig";
3636
public static final String TARGET_OPENFEIGN = "generateForOpenFeign";
37+
public static final String DEFAULT_INTERFACES = "defaultInterfaces";
3738

3839
protected String title = "swagger-petstore";
3940
protected String configPackage = "io.swagger.configuration";
@@ -51,6 +52,7 @@ public class SpringCodegen extends AbstractJavaCodegen
5152
protected boolean swaggerDocketConfig = false;
5253
protected boolean useOptional = false;
5354
protected boolean openFeign = false;
55+
protected boolean defaultInterfaces = true;
5456

5557
public SpringCodegen() {
5658
super();
@@ -74,7 +76,7 @@ public SpringCodegen() {
7476
cliOptions.add(CliOption.newBoolean(INTERFACE_ONLY, "Whether to generate only API interface stubs without the server files."));
7577
cliOptions.add(CliOption.newBoolean(DELEGATE_PATTERN, "Whether to generate the server files using the delegate pattern"));
7678
cliOptions.add(CliOption.newBoolean(SINGLE_CONTENT_TYPES, "Whether to select only one produces/consumes content-type by operation."));
77-
cliOptions.add(CliOption.newBoolean(JAVA_8, "use java8 default interface"));
79+
cliOptions.add(CliOption.newBoolean(JAVA_8, "use java8 features like the new date library"));
7880
cliOptions.add(CliOption.newBoolean(ASYNC, "use async Callable controllers"));
7981
cliOptions.add(new CliOption(RESPONSE_WRAPPER, "wrap the responses in given type (Future,Callable,CompletableFuture,ListenableFuture,DeferredResult,HystrixCommand,RxObservable,RxSingle or fully qualified type)"));
8082
cliOptions.add(CliOption.newBoolean(USE_TAGS, "use tags for creating interface and controller classnames"));
@@ -84,6 +86,7 @@ public SpringCodegen() {
8486
cliOptions.add(CliOption.newBoolean(USE_OPTIONAL,
8587
"Use Optional container for optional parameters"));
8688
cliOptions.add(CliOption.newBoolean(TARGET_OPENFEIGN,"Generate for usage with OpenFeign (instead of feign)"));
89+
cliOptions.add(CliOption.newBoolean(DEFAULT_INTERFACES, "Generate default implementations for interfaces").defaultValue("true"));
8790

8891
supportedLibraries.put(DEFAULT_LIBRARY, "Spring-boot Server application using the SpringFox integration.");
8992
supportedLibraries.put(SPRING_MVC_LIBRARY, "Spring-MVC Server application using the SpringFox integration.");
@@ -194,6 +197,11 @@ public void processOpts() {
194197
this.setOpenFeign(convertPropertyToBoolean(TARGET_OPENFEIGN));
195198
}
196199

200+
if (additionalProperties.containsKey(DEFAULT_INTERFACES)) {
201+
this.setDefaultInterfaces(convertPropertyToBoolean(DEFAULT_INTERFACES));
202+
}
203+
additionalProperties.put(DEFAULT_INTERFACES, this.defaultInterfaces);
204+
197205
if (useBeanValidation) {
198206
writePropertyBack(USE_BEANVALIDATION, useBeanValidation);
199207
}
@@ -697,4 +705,8 @@ public void setUseOptional(boolean useOptional) {
697705
public void setOpenFeign(boolean openFeign) {
698706
this.openFeign = openFeign;
699707
}
708+
709+
public void setDefaultInterfaces(boolean defaultInterfaces) {
710+
this.defaultInterfaces = defaultInterfaces;
711+
}
700712
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ 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}} {{#delegate-method}}_{{/delegate-method}}{{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/allParams}}){{^jdk8}};{{/jdk8}}{{#jdk8}} {
103+
{{#defaultInterfaces}}default {{/defaultInterfaces}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{#delegate-method}}_{{/delegate-method}}{{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/allParams}}){{^defaultInterfaces}};{{/defaultInterfaces}}{{#defaultInterfaces}} {
104104
{{#delegate-method}}
105105
return {{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
106106
}
@@ -128,7 +128,7 @@ public interface {{classname}} {
128128
{{#isDelegate}}
129129
return getDelegate().{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
130130
{{/isDelegate}}
131-
}{{/jdk8}}
131+
}{{/defaultInterfaces}}
132132

133133
{{/operation}}
134134
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public interface {{classname}}Delegate {
6161
/**
6262
* @see {{classname}}#{{operationId}}
6363
*/
64-
{{#jdk8}}default {{/jdk8}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{^isFile}} {{>optionalDataType}} {{/isFile}}{{#isFile}}MultipartFile{{/isFile}} {{paramName}}{{#hasMore}},
65-
{{/hasMore}}{{/allParams}}){{^jdk8}};{{/jdk8}}{{#jdk8}} {
64+
{{#defaultInterfaces}}default {{/defaultInterfaces}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{^isFile}} {{>optionalDataType}} {{/isFile}}{{#isFile}}MultipartFile{{/isFile}} {{paramName}}{{#hasMore}},
65+
{{/hasMore}}{{/allParams}}){{^defaultInterfaces}};{{/defaultInterfaces}}{{#defaultInterfaces}} {
6666
if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
6767
{{#examples}}
6868
if (getAcceptHeader().get().contains("{{{contentType}}}")) {
@@ -78,7 +78,7 @@ public interface {{classname}}Delegate {
7878
log.warn("ObjectMapper or HttpServletRequest not configured in default {{classname}} interface so no example is generated");
7979
}
8080
return {{#async}}CompletableFuture.completedFuture({{/async}}new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED){{#async}}){{/async}};
81-
}{{/jdk8}}
81+
}{{/defaultInterfaces}}
8282

8383
{{/operation}}
8484
}

modules/swagger-codegen/src/test/java/io/swagger/codegen/options/SpringOptionsProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class SpringOptionsProvider extends JavaOptionsProvider {
2323
public static final String SWAGGER_DOCKET_CONFIG = "false";
2424
public static final String USE_OPTIONAL = "false";
2525
public static final String TARGET_OPENFEIGN = "false";
26+
public static final String DEFAULT_INTERFACES = "true";
2627

2728
@Override
2829
public String getLanguage() {
@@ -48,6 +49,7 @@ public Map<String, String> createOptions() {
4849
options.put(SpringCodegen.SWAGGER_DOCKET_CONFIG, SWAGGER_DOCKET_CONFIG);
4950
options.put(SpringCodegen.USE_OPTIONAL, USE_OPTIONAL);
5051
options.put(SpringCodegen.TARGET_OPENFEIGN, TARGET_OPENFEIGN);
52+
options.put(SpringCodegen.DEFAULT_INTERFACES, DEFAULT_INTERFACES);
5153

5254
return options;
5355
}

modules/swagger-codegen/src/test/java/io/swagger/codegen/spring/SpringOptionsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ protected void setExpectations() {
8080
clientCodegen.setUseOptional(
8181
Boolean.valueOf(SpringOptionsProvider.USE_OPTIONAL));
8282
times = 1;
83+
clientCodegen.setDefaultInterfaces(Boolean.valueOf(SpringOptionsProvider.DEFAULT_INTERFACES));
84+
times = 1;
8385
}};
8486
}
8587
}

0 commit comments

Comments
 (0)