Skip to content

Commit 0e70d1f

Browse files
authored
[Kotlin][Spring] use flag delegatePattern together with skipDefaultInterface (#19212)
* fix #19211 kotlin-spring flag delegatePattern together with skipDefaultInterface generates broken code * add kotlin-springboot-delegate-nodefaults to the workflow * fix Platform declaration clash * move kotlin-springboot-delegate-nodefaults to the kotlin-server-jdk17 file * fixed empty line
1 parent 6ad5864 commit 0e70d1f

File tree

36 files changed

+1418
-11
lines changed

36 files changed

+1418
-11
lines changed

.github/workflows/samples-kotlin-server-jdk17.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
sample:
3030
# server
3131
- samples/server/petstore/kotlin-springboot-3
32+
- samples/server/petstore/kotlin-springboot-delegate-nodefaults
3233
- samples/server/petstore/kotlin-springboot-request
3334
- samples/server/petstore/kotlin-springboot-request-cookie
3435
- samples/server/petstore/kotlin-server/javalin
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
generatorName: kotlin-spring
2+
outputDir: samples/server/petstore/kotlin-springboot-delegate-nodefaults
3+
library: spring-boot
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/kotlin-spring
6+
additionalProperties:
7+
documentationProvider: none
8+
annotationLibrary: swagger2
9+
useSwaggerUI: "true"
10+
delegatePattern: "true"
11+
skipDefaultInterface: "true"
12+
beanValidations: "true"
13+
requestMappingMode: "api_interface"
14+
useSpringBoot3: "true"

docs/generators/kotlin-spring.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
4646
|serverPort|configuration the port in which the sever is to run on| |8080|
4747
|serviceImplementation|generate stub service implementations that extends service interfaces. If this is set to true service interfaces will also be generated| |false|
4848
|serviceInterface|generate service interfaces to go alongside controllers. In most cases this option would be used to update an existing project, so not to override implementations. Useful to help facilitate the generation gap pattern| |false|
49-
|skipDefaultInterface|Whether to skip generation of default implementations for interfaces| |false|
49+
|skipDefaultInterface|Whether to skip generation of default implementations for interfaces (Api interfaces or Delegate interfaces depending on the delegatePattern option)| |false|
5050
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |null|
5151
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |null|
5252
|sourceFolder|source folder for generated code| |src/main/kotlin|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinSpringServerCodegen.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
9494
public static final String SERVICE_INTERFACE = "serviceInterface";
9595
public static final String SERVICE_IMPLEMENTATION = "serviceImplementation";
9696
public static final String SKIP_DEFAULT_INTERFACE = "skipDefaultInterface";
97+
public static final String SKIP_DEFAULT_API_INTERFACE = "skipDefaultApiInterface";
98+
public static final String SKIP_DEFAULT_DELEGATE_INTERFACE = "skipDefaultDelegateInterface";
9799
public static final String REACTIVE = "reactive";
98100
public static final String INTERFACE_ONLY = "interfaceOnly";
99101
public static final String USE_FEIGN_CLIENT_URL = "useFeignClientUrl";
@@ -135,6 +137,8 @@ public String getDescription() {
135137
private String title = "OpenAPI Kotlin Spring";
136138
private boolean useBeanValidation = true;
137139
@Setter private boolean skipDefaultInterface = false;
140+
@Setter private boolean skipDefaultApiInterface = false;
141+
@Setter private boolean skipDefaultDelegateInterface = false;
138142
@Setter private boolean exceptionHandler = true;
139143
@Setter private boolean gradleBuildFile = true;
140144
private boolean useSwaggerUI = true;
@@ -222,7 +226,7 @@ public KotlinSpringServerCodegen() {
222226
addSwitch(SERVICE_IMPLEMENTATION, "generate stub service implementations that extends service " +
223227
"interfaces. If this is set to true service interfaces will also be generated", serviceImplementation);
224228
addSwitch(USE_BEANVALIDATION, "Use BeanValidation API annotations to validate data types", useBeanValidation);
225-
addSwitch(SKIP_DEFAULT_INTERFACE, "Whether to skip generation of default implementations for interfaces", skipDefaultInterface);
229+
addSwitch(SKIP_DEFAULT_INTERFACE, "Whether to skip generation of default implementations for interfaces (Api interfaces or Delegate interfaces depending on the delegatePattern option)", skipDefaultInterface);
226230
addSwitch(REACTIVE, "use coroutines for reactive behavior", reactive);
227231
addSwitch(INTERFACE_ONLY, "Whether to generate only API interface stubs without the server files.", interfaceOnly);
228232
addSwitch(USE_FEIGN_CLIENT_URL, "Whether to generate Feign client with url parameter.", useFeignClientUrl);
@@ -562,6 +566,16 @@ public void processOpts() {
562566
this.setDelegatePattern(Boolean.parseBoolean(additionalProperties.get(DELEGATE_PATTERN).toString()));
563567
}
564568

569+
if (skipDefaultInterface) {
570+
if (delegatePattern) {
571+
this.setSkipDefaultDelegateInterface(true);
572+
} else {
573+
this.setSkipDefaultApiInterface(true);
574+
}
575+
}
576+
writePropertyBack(SKIP_DEFAULT_API_INTERFACE, skipDefaultApiInterface);
577+
writePropertyBack(SKIP_DEFAULT_DELEGATE_INTERFACE, skipDefaultDelegateInterface);
578+
565579
if (additionalProperties.containsKey(USE_TAGS)) {
566580
this.setUseTags(Boolean.parseBoolean(additionalProperties.get(USE_TAGS).toString()));
567581
}

modules/openapi-generator/src/main/resources/kotlin-spring/apiController.mustache

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@ import java.util.Optional
1313
{{/useRequestMappingOnController}}
1414
{{#operations}}
1515
class {{classname}}Controller(
16-
@org.springframework.beans.factory.annotation.Autowired(required = false) delegate: {{classname}}Delegate?
16+
{{#skipDefaultDelegateInterface}}
17+
private val delegate: {{classname}}Delegate
18+
{{/skipDefaultDelegateInterface}}
19+
{{^skipDefaultDelegateInterface}}
20+
delegate: {{classname}}Delegate?
21+
{{/skipDefaultDelegateInterface}}
1722
) : {{classname}} {
23+
{{^skipDefaultDelegateInterface}}
1824
private lateinit var delegate: {{classname}}Delegate
1925

2026
init {
2127
this.delegate = Optional.ofNullable(delegate).orElse(object : {{classname}}Delegate {})
2228
}
29+
{{/skipDefaultDelegateInterface}}
2330

2431
override fun getDelegate(): {{classname}}Delegate = delegate
2532
}

modules/openapi-generator/src/main/resources/kotlin-spring/apiDelegate.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ interface {{classname}}Delegate {
3434
* @see {{classname}}#{{operationId}}
3535
*/
3636
{{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{{paramName}}}: {{^isFile}}{{^reactive}}{{>optionalDataType}}{{/reactive}}{{#reactive}}{{^isArray}}{{>optionalDataType}}{{/isArray}}{{#isArray}}{{#isBodyParam}}Flow<{{{baseType}}}>{{/isBodyParam}}{{^isBodyParam}}{{>optionalDataType}}{{/isBodyParam}}{{/isArray}}{{/reactive}}{{/isFile}}{{#isFile}}Resource?{{/isFile}}{{^-last}},
37-
{{/-last}}{{/allParams}}): {{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {
37+
{{/-last}}{{/allParams}}): {{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}}{{^skipDefaultDelegateInterface}} {
3838
{{>methodBody}}
39-
}
39+
}{{/skipDefaultDelegateInterface}}
4040

4141
{{/operation}}
4242
}

modules/openapi-generator/src/main/resources/kotlin-spring/apiInterface.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import kotlin.collections.Map
7171
interface {{classname}} {
7272
{{#isDelegate}}
7373

74-
fun getDelegate(): {{classname}}Delegate = object: {{classname}}Delegate {}
74+
fun getDelegate(): {{classname}}Delegate{{^skipDefaultDelegateInterface}} = object: {{classname}}Delegate {}{{/skipDefaultDelegateInterface}}
7575
{{/isDelegate}}
7676
{{#operation}}
7777

@@ -103,14 +103,14 @@ interface {{classname}} {
103103
produces = [{{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}]{{/hasProduces}}{{#hasConsumes}},
104104
consumes = [{{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}}]{{/hasConsumes}}{{/singleContentTypes}}
105105
)
106-
{{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>cookieParams}}{{>bodyParams}}{{>formParams}}{{>requesObject}}{{^-last}},{{/-last}}{{/allParams}}): ResponseEntity<{{>returnTypes}}>{{^skipDefaultInterface}} {
106+
{{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>cookieParams}}{{>bodyParams}}{{>formParams}}{{>requesObject}}{{^-last}},{{/-last}}{{/allParams}}): ResponseEntity<{{>returnTypes}}>{{^skipDefaultApiInterface}} {
107107
{{^isDelegate}}
108108
return {{>returnValue}}
109109
{{/isDelegate}}
110110
{{#isDelegate}}
111111
return getDelegate().{{operationId}}({{#allParams}}{{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}})
112112
{{/isDelegate}}
113-
}{{/skipDefaultInterface}}
113+
}{{/skipDefaultApiInterface}}
114114
{{/operation}}
115115
}
116116
{{/operations}}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
README.md
2+
build.gradle.kts
3+
pom.xml
4+
settings.gradle
5+
src/main/kotlin/org/openapitools/Application.kt
6+
src/main/kotlin/org/openapitools/api/ApiUtil.kt
7+
src/main/kotlin/org/openapitools/api/Exceptions.kt
8+
src/main/kotlin/org/openapitools/api/PetApi.kt
9+
src/main/kotlin/org/openapitools/api/PetApiController.kt
10+
src/main/kotlin/org/openapitools/api/PetApiDelegate.kt
11+
src/main/kotlin/org/openapitools/api/StoreApi.kt
12+
src/main/kotlin/org/openapitools/api/StoreApiController.kt
13+
src/main/kotlin/org/openapitools/api/StoreApiDelegate.kt
14+
src/main/kotlin/org/openapitools/api/UserApi.kt
15+
src/main/kotlin/org/openapitools/api/UserApiController.kt
16+
src/main/kotlin/org/openapitools/api/UserApiDelegate.kt
17+
src/main/kotlin/org/openapitools/model/Category.kt
18+
src/main/kotlin/org/openapitools/model/ModelApiResponse.kt
19+
src/main/kotlin/org/openapitools/model/Order.kt
20+
src/main/kotlin/org/openapitools/model/Pet.kt
21+
src/main/kotlin/org/openapitools/model/Tag.kt
22+
src/main/kotlin/org/openapitools/model/User.kt
23+
src/main/resources/application.yaml
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.8.0-SNAPSHOT

0 commit comments

Comments
 (0)