Skip to content

Commit 7d9f9d7

Browse files
authored
[kotlin-spring] Fix cookie parameter code generation in API (#17959)
* + Cookie parameter generation fixed * Added cookie parameter mustache template for generating cookie related code * Adapted kotlin-spring api templates to include cookie parameters * Added tests for evaluating cookie parameter code generation * Added configuration sample for the new cookie use case * - Unused fake cases removed * Removed fake cases from openapi spec that were not related to cookie usage * Cleaned sample files
1 parent 2653777 commit 7d9f9d7

34 files changed

Lines changed: 2435 additions & 2 deletions
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
generatorName: kotlin-spring
2+
outputDir: samples/server/petstore/kotlin-springboot-request-cookie
3+
library: spring-boot
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/kotlin/petstore-with-fake-endpoints-for-testing-with-cookie.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/kotlin-spring
6+
additionalProperties:
7+
appendRequestToHandler: true
8+
interfaceOnly: true
9+
useSpringBoot3: true

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class {{classname}}Controller({{#serviceInterface}}@Autowired(required = true) v
9090
produces = [{{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}]{{/hasProduces}}{{#hasConsumes}},
9191
consumes = [{{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}}]{{/hasConsumes}}{{/singleContentTypes}}
9292
)
93-
{{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>requesObject}}{{^-last}},{{/-last}}{{/allParams}}): ResponseEntity<{{>returnTypes}}> {
93+
{{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>cookieParams}}{{>bodyParams}}{{>formParams}}{{>requesObject}}{{^-last}},{{/-last}}{{/allParams}}): ResponseEntity<{{>returnTypes}}> {
9494
return {{>returnValue}}
9595
}
9696
{{/operation}}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ interface {{classname}} {
102102
produces = [{{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}]{{/hasProduces}}{{#hasConsumes}},
103103
consumes = [{{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}}]{{/hasConsumes}}{{/singleContentTypes}}
104104
)
105-
{{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>requesObject}}{{^-last}},{{/-last}}{{/allParams}}): ResponseEntity<{{>returnTypes}}>{{^skipDefaultInterface}} {
105+
{{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>cookieParams}}{{>bodyParams}}{{>formParams}}{{>requesObject}}{{^-last}},{{/-last}}{{/allParams}}): ResponseEntity<{{>returnTypes}}>{{^skipDefaultInterface}} {
106106
{{^isDelegate}}
107107
return {{>returnValue}}
108108
{{/isDelegate}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#isCookieParam}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}@CookieValue(name = "{{baseName}}"{{^required}}, required = false{{/required}}{{#defaultValue}}, defaultValue = "{{{.}}}"{{/defaultValue}}) {{paramName}}: {{>optionalDataType}}{{/isCookieParam}}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,48 @@ public void skipDefaultInterface() throws Exception {
435435
);
436436
}
437437

438+
@Test(description = "test cookie parameter generation on interface apis")
439+
public void cookieParameterGenerationApis() throws Exception {
440+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
441+
output.deleteOnExit();
442+
String outputPath = output.getAbsolutePath().replace('\\', '/');
443+
444+
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
445+
codegen.setOutputDir(output.getAbsolutePath());
446+
codegen.additionalProperties().put(KotlinSpringServerCodegen.INTERFACE_ONLY, true);
447+
codegen.additionalProperties().put(KotlinSpringServerCodegen.SKIP_DEFAULT_INTERFACE, true);
448+
449+
new DefaultGenerator().opts(new ClientOptInput()
450+
.openAPI(TestUtils.parseSpec("src/test/resources/3_0/kotlin/petstore-with-fake-endpoints-for-testing-with-cookie.yaml"))
451+
.config(codegen))
452+
.generate();
453+
454+
assertFileContains(
455+
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/FakeApi.kt"),
456+
"@CookieValue"
457+
);
458+
}
459+
460+
@Test(description = "test cookie parameter generation on controllers")
461+
public void cookieParameterGenerationControllers() throws Exception {
462+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
463+
output.deleteOnExit();
464+
String outputPath = output.getAbsolutePath().replace('\\', '/');
465+
466+
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
467+
codegen.setOutputDir(output.getAbsolutePath());
468+
469+
new DefaultGenerator().opts(new ClientOptInput()
470+
.openAPI(TestUtils.parseSpec("src/test/resources/3_0/kotlin/petstore-with-fake-endpoints-for-testing-with-cookie.yaml"))
471+
.config(codegen))
472+
.generate();
473+
474+
assertFileContains(
475+
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/FakeApiController.kt"),
476+
"@CookieValue"
477+
);
478+
}
479+
438480
@Test(description = "use Spring boot 3 & jakarta extension")
439481
public void useSpringBoot3() throws Exception {
440482
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();

0 commit comments

Comments
 (0)