[KOTLIN Spring] add interfaceOnly option (#3050)

* move template definition to processOpts()

* add interfaceOnly option for kotlin-spring

* add template for interface only option
This commit is contained in:
Roman Müller
2019-06-27 18:23:50 +02:00
committed by Jim Schubert
parent 325ed4b72e
commit 73966a0152
12 changed files with 430 additions and 371 deletions

View File

@@ -27,4 +27,5 @@ sidebar_label: kotlin-spring
|serviceImplementation|generate stub service implementations that extends service interfaces. If this is set to true service interfaces will also be generated| |false|
|useBeanValidation|Use BeanValidation API annotations to validate data types| |true|
|reactive|use coroutines for reactive behavior| |false|
|interfaceOnly|Whether to generate only API interface stubs without the server files.| |false|
|library|library template (sub-template)|<dl><dt>**spring-boot**</dt><dd>Spring-boot Server application.</dd><dl>|spring-boot|

View File

@@ -61,7 +61,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
public static final String SERVICE_INTERFACE = "serviceInterface";
public static final String SERVICE_IMPLEMENTATION = "serviceImplementation";
public static final String REACTIVE = "reactive";
public static final String INTERFACE_ONLY = "interfaceOnly";
private String basePackage;
private String invokerPackage;
@@ -75,12 +75,11 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
private boolean serviceInterface = false;
private boolean serviceImplementation = false;
private boolean reactive = false;
private boolean interfaceOnly = false;
public KotlinSpringServerCodegen() {
super();
apiTestTemplateFiles.put("api_test.mustache", ".kt");
reservedWords.addAll(VARIABLE_RESERVED_WORDS);
outputFolder = "generated-code/kotlin-spring";
@@ -124,6 +123,7 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
"interfaces. If this is set to true service interfaces will also be generated", serviceImplementation);
addSwitch(USE_BEANVALIDATION, "Use BeanValidation API annotations to validate data types", useBeanValidation);
addSwitch(REACTIVE, "use coroutines for reactive behavior", reactive);
addSwitch(INTERFACE_ONLY, "Whether to generate only API interface stubs without the server files.", interfaceOnly);
supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application.");
setLibrary(SPRING_BOOT);
@@ -209,6 +209,10 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
return this.useBeanValidation;
}
public void setInterfaceOnly(boolean interfaceOnly) {
this.interfaceOnly = interfaceOnly;
}
@Override
public void setUseBeanValidation(boolean useBeanValidation) {
this.useBeanValidation = useBeanValidation;
@@ -322,9 +326,18 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
writePropertyBack(REACTIVE, reactive);
writePropertyBack(EXCEPTION_HANDLER, exceptionHandler);
if (additionalProperties.containsKey(INTERFACE_ONLY)) {
this.setInterfaceOnly(Boolean.valueOf(additionalProperties.get(INTERFACE_ONLY).toString()));
}
modelTemplateFiles.put("model.mustache", ".kt");
if (interfaceOnly) {
apiTemplateFiles.put("apiInterface.mustache", ".kt");
} else {
apiTemplateFiles.put("api.mustache", ".kt");
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
apiTestTemplateFiles.put("api_test.mustache", ".kt");
}
if (this.serviceInterface) {
apiTemplateFiles.put("service.mustache", "Service.kt");
@@ -335,6 +348,9 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
apiTemplateFiles.put("serviceImpl.mustache", "ServiceImpl.kt");
}
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
if (this.exceptionHandler) {
supportingFiles.add(new SupportingFile("exceptions.mustache",
sanitizeDirectory(sourceFolder + File.separator + apiPackage), "Exceptions.kt"));

View File

@@ -74,8 +74,8 @@ class {{classname}}Controller({{#serviceInterface}}@Autowired(required = true) v
value = ["{{#lambda.escapeDoubleQuote}}{{path}}{{/lambda.escapeDoubleQuote}}"],{{#singleContentTypes}}{{#hasProduces}}
produces = "{{{vendorExtensions.x-accepts}}}",{{/hasProduces}}{{#hasConsumes}}
consumes = "{{{vendorExtensions.x-contentType}}}",{{/hasConsumes}}{{/singleContentTypes}}{{^singleContentTypes}}{{#hasProduces}}
produces = [{{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}], {{/hasProduces}}{{#hasConsumes}}
consumes = [{{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}}],{{/hasConsumes}}{{/singleContentTypes}}
produces = [{{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}}], {{/hasProduces}}{{#hasConsumes}}
consumes = [{{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}}],{{/hasConsumes}}{{/singleContentTypes}}
method = [RequestMethod.{{httpMethod}}])
{{#reactive}}{{^isListContainer}}suspend {{/isListContainer}}{{/reactive}}fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/allParams}}): ResponseEntity<{{>returnTypes}}> {
return {{>returnValue}}

View File

@@ -0,0 +1,83 @@
package {{package}}
{{#imports}}import {{import}}
{{/imports}}
{{#swaggerAnnotations}}
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import io.swagger.annotations.ApiResponse
import io.swagger.annotations.ApiResponses
import io.swagger.annotations.Authorization
import io.swagger.annotations.AuthorizationScope
{{/swaggerAnnotations}}
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestPart
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestHeader
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestMapping
{{#useBeanValidation}}
import org.springframework.validation.annotation.Validated
{{/useBeanValidation}}
import org.springframework.web.context.request.NativeWebRequest
import org.springframework.beans.factory.annotation.Autowired
{{#useBeanValidation}}
import javax.validation.Valid
import javax.validation.constraints.DecimalMax
import javax.validation.constraints.DecimalMin
import javax.validation.constraints.Max
import javax.validation.constraints.Min
import javax.validation.constraints.NotNull
import javax.validation.constraints.Pattern
import javax.validation.constraints.Size
{{/useBeanValidation}}
{{#reactive}}
import kotlinx.coroutines.flow.Flow;
{{/reactive}}
import kotlin.collections.List
import kotlin.collections.Map
{{#useBeanValidation}}
@Validated
{{/useBeanValidation}}
{{#swaggerAnnotations}}
@Api(value = "{{{baseName}}}", description = "The {{{baseName}}} API")
{{/swaggerAnnotations}}
{{=<% %>=}}
@RequestMapping("\${api.base-path:<%contextPath%>}")
<%={{ }}=%>
{{#operations}}
interface {{classname}} {
{{#operation}}
{{#swaggerAnnotations}}
@ApiOperation(
value = "{{{summary}}}",
nickname = "{{{operationId}}}",
notes = "{{{notes}}}"{{#returnBaseType}},
response = {{{returnBaseType}}}::class{{/returnBaseType}}{{#returnContainer}},
responseContainer = "{{{returnContainer}}}"{{/returnContainer}}{{#hasAuthMethods}},
authorizations = [{{#authMethods}}Authorization(value = "{{name}}"{{#isOAuth}}, scopes = [{{#scopes}}AuthorizationScope(scope = "{{scope}}", description = "{{description}}"){{#hasMore}}, {{/hasMore}}{{/scopes}}]{{/isOAuth}}){{#hasMore}}, {{/hasMore}}{{/authMethods}}]{{/hasAuthMethods}})
@ApiResponses(
value = [{{#responses}}ApiResponse(code = {{{code}}}, message = "{{{message}}}"{{#baseType}}, response = {{{baseType}}}::class{{/baseType}}{{#containerType}}, responseContainer = "{{{containerType}}}"{{/containerType}}){{#hasMore}},{{/hasMore}}{{/responses}}]){{/swaggerAnnotations}}
@RequestMapping(
value = ["{{#lambda.escapeDoubleQuote}}{{path}}{{/lambda.escapeDoubleQuote}}"],{{#singleContentTypes}}{{#hasProduces}}
produces = "{{{vendorExtensions.x-accepts}}}",{{/hasProduces}}{{#hasConsumes}}
consumes = "{{{vendorExtensions.x-contentType}}}",{{/hasConsumes}}{{/singleContentTypes}}{{^singleContentTypes}}{{#hasProduces}}
produces = [{{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}], {{/hasProduces}}{{#hasConsumes}}
consumes = [{{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}}],{{/hasConsumes}}{{/singleContentTypes}}
method = [RequestMethod.{{httpMethod}}])
{{#reactive}}{{^isListContainer}}suspend {{/isListContainer}}{{/reactive}}fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/allParams}}): ResponseEntity<{{>returnTypes}}> {
return {{>returnValue}}
}
{{/operation}}
}
{{/operations}}

View File

@@ -13,7 +13,7 @@ Method | HTTP request | Description
# **call_123_test_special_tags**
> Client call_123_test_special_tags(body => $body)
> Client call_123_test_special_tags(client => $client)
To test special tags
@@ -26,10 +26,10 @@ use WWW::OpenAPIClient::AnotherFakeApi;
my $api_instance = WWW::OpenAPIClient::AnotherFakeApi->new(
);
my $body = WWW::OpenAPIClient::Object::Client->new(); # Client | client model
my $client = WWW::OpenAPIClient::Object::Client->new(); # Client | client model
eval {
my $result = $api_instance->call_123_test_special_tags(body => $body);
my $result = $api_instance->call_123_test_special_tags(client => $client);
print Dumper($result);
};
if ($@) {
@@ -41,7 +41,7 @@ if ($@) {
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**body** | [**Client**](Client.md)| client model |
**client** | [**Client**](Client.md)| client model |
### Return type

View File

@@ -1,41 +0,0 @@
=begin comment
OpenAPI Petstore
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
The version of the OpenAPI document: 1.0.0
Generated by: https://openapi-generator.tech
=end comment
=cut
#
# NOTE: This class is auto generated by OpenAPI Generator
# Please update the test cases below to test the API endpoints.
# Ref: https://openapi-generator.tech
#
use Test::More tests => 1; #TODO update number of test cases
use Test::Exception;
use lib 'lib';
use strict;
use warnings;
use_ok('WWW::OpenAPIClient::AnotherFakeApi');
my $api = WWW::OpenAPIClient::AnotherFakeApi->new();
isa_ok($api, 'WWW::OpenAPIClient::AnotherFakeApi');
#
# call_123_test_special_tags test
#
{
my $body = undef; # replace NULL with a proper value
my $result = $api->call_123_test_special_tags(body => $body);
}
1;