Skip to content

Commit ed24e47

Browse files
eugeniykdavidricodias
authored andcommitted
[kotlin] Support @Deprecation annotation for kotlin-spring generator (OpenAPITools#23104)
* Add change to template with tests * Update test * Add required message attribute for Kotlin code * Regenerate samples
1 parent e0f75a4 commit ed24e47

35 files changed

Lines changed: 96 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ class {{classname}}Controller({{#serviceInterface}}@Autowired(required = true) v
8787
{{#vendorExtensions.x-operation-extra-annotation}}
8888
{{{.}}}
8989
{{/vendorExtensions.x-operation-extra-annotation}}
90+
{{#isDeprecated}}
91+
@Deprecated(message="Operation is deprecated")
92+
{{/isDeprecated}}
9093
@RequestMapping(
9194
method = [RequestMethod.{{httpMethod}}],
9295
// "{{#lambdaEscapeInNormalString}}{{{path}}}{{/lambdaEscapeInNormalString}}"

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ interface {{classname}} {
102102
{{#vendorExtensions.x-operation-extra-annotation}}
103103
{{{.}}}
104104
{{/vendorExtensions.x-operation-extra-annotation}}
105+
{{#isDeprecated}}
106+
@Deprecated(message="Operation is deprecated")
107+
{{/isDeprecated}}
105108
@RequestMapping(
106109
method = [RequestMethod.{{httpMethod}}],
107110
// "{{#lambdaEscapeInNormalString}}{{{path}}}{{/lambdaEscapeInNormalString}}"

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ interface {{classname}}Service {
3030
{{/externalDocs}}
3131
* @see {{classname}}#{{operationId}}
3232
*/
33+
{{#isDeprecated}}
34+
@Deprecated(message="Operation is deprecated")
35+
{{/isDeprecated}}
3336
{{#reactive}}{{^isArray}}suspend {{/isArray}}{{#isArray}}{{^useFlowForArrayReturnType}}suspend {{/useFlowForArrayReturnType}}{{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{{paramName}}}: {{^isBodyParam}}{{>optionalDataType}}{{/isBodyParam}}{{#isBodyParam}}{{^reactive}}{{>optionalDataType}}{{/reactive}}{{#reactive}}{{^isArray}}{{>optionalDataType}}{{/isArray}}{{#isArray}}Flow<{{{baseType}}}>{{/isArray}}{{/reactive}}{{/isBodyParam}}{{^-last}}, {{/-last}}{{/allParams}}): {{>returnTypes}}
3437
{{/operation}}
3538
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4663,6 +4663,49 @@ public void testSealedResponseInterfacesVoidResponse() throws IOException {
46634663
Assert.assertTrue(petContent.contains(") : CreatePetResponse {") || petContent.contains(") : CreatePetResponse"),
46644664
"Pet should implement CreatePetResponse");
46654665
}
4666+
4667+
@Test
4668+
public void testDeprecatedAnnotationOnInterface() throws IOException {
4669+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
4670+
output.deleteOnExit();
4671+
String outputPath = output.getAbsolutePath().replace('\\', '/');
4672+
4673+
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
4674+
codegen.setOutputDir(output.getAbsolutePath());
4675+
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "org.openapitools.api");
4676+
codegen.additionalProperties().put(KotlinSpringServerCodegen.INTERFACE_ONLY, true);
4677+
4678+
new DefaultGenerator().opts(new ClientOptInput()
4679+
.openAPI(TestUtils.parseSpec("src/test/resources/3_0/kotlin/support-deprecated-api.yaml"))
4680+
.config(codegen))
4681+
.generate();
4682+
4683+
assertFileContains(
4684+
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/PingApi.kt"),
4685+
"@Deprecated(message=\"Operation is deprecated\") @RequestMapping("
4686+
);
4687+
}
4688+
4689+
@Test
4690+
public void testDeprecatedAnnotationOnController() throws IOException {
4691+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
4692+
output.deleteOnExit();
4693+
String outputPath = output.getAbsolutePath().replace('\\', '/');
4694+
4695+
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
4696+
codegen.setOutputDir(output.getAbsolutePath());
4697+
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "org.openapitools.api");
4698+
4699+
new DefaultGenerator().opts(new ClientOptInput()
4700+
.openAPI(TestUtils.parseSpec("src/test/resources/3_0/kotlin/support-deprecated-api.yaml"))
4701+
.config(codegen))
4702+
.generate();
4703+
4704+
assertFileContains(
4705+
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/PingApiController.kt"),
4706+
"@Deprecated(message=\"Operation is deprecated\") @RequestMapping("
4707+
);
4708+
}
46664709
}
46674710

46684711

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
openapi: "3.0.0"
2+
info:
3+
title: Verify @Deprecated annotation
4+
version: "1.0"
5+
6+
paths:
7+
8+
/ping:
9+
get:
10+
deprecated: true
11+
operationId: ping
12+
responses:
13+
200:
14+
description: OK

samples/server/petstore/kotlin-spring-cloud/src/main/kotlin/org/openapitools/api/PetApi.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ interface PetApi {
7373
}
7474

7575

76+
@Deprecated(message="Operation is deprecated")
7677
@RequestMapping(
7778
method = [RequestMethod.GET],
7879
// "/pet/findByTags"

samples/server/petstore/kotlin-spring-default/src/main/kotlin/org/openapitools/api/PetApiController.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class PetApiController() {
106106
ApiResponse(responseCode = "400", description = "Invalid tag value") ],
107107
security = [ SecurityRequirement(name = "petstore_auth", scopes = [ "read:pets" ]) ]
108108
)
109+
@Deprecated(message="Operation is deprecated")
109110
@RequestMapping(
110111
method = [RequestMethod.GET],
111112
// "/pet/findByTags"

samples/server/petstore/kotlin-spring-sealed-interfaces/src/main/kotlin/org/openapitools/api/PetApi.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ interface PetApi {
7878
}
7979

8080

81+
@Deprecated(message="Operation is deprecated")
8182
@RequestMapping(
8283
method = [RequestMethod.GET],
8384
// "/pet/findByTags"

samples/server/petstore/kotlin-springboot-3-no-response-entity/src/main/kotlin/org/openapitools/api/PetApiController.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class PetApiController(@Autowired(required = true) val service: PetApiService) {
6969
}
7070

7171

72+
@Deprecated(message="Operation is deprecated")
7273
@RequestMapping(
7374
method = [RequestMethod.GET],
7475
// "/pet/findByTags"

samples/server/petstore/kotlin-springboot-3-no-response-entity/src/main/kotlin/org/openapitools/api/PetApiService.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ interface PetApiService {
4848
* @deprecated
4949
* @see PetApi#findPetsByTags
5050
*/
51+
@Deprecated(message="Operation is deprecated")
5152
fun findPetsByTags(tags: kotlin.collections.List<kotlin.String>): List<Pet>
5253

5354
/**

0 commit comments

Comments
 (0)