Skip to content

Commit

Permalink
[kotlin-spring] Fix cookie parameter code generation in API (#17959)
Browse files Browse the repository at this point in the history
* + 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
  • Loading branch information
oscarr-reyes committed Mar 9, 2024
1 parent 2653777 commit 7d9f9d7
Show file tree
Hide file tree
Showing 34 changed files with 2,435 additions and 2 deletions.
9 changes: 9 additions & 0 deletions bin/configs/kotlin-spring-boot-request-cookie.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
generatorName: kotlin-spring
outputDir: samples/server/petstore/kotlin-springboot-request-cookie
library: spring-boot
inputSpec: modules/openapi-generator/src/test/resources/3_0/kotlin/petstore-with-fake-endpoints-for-testing-with-cookie.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-spring
additionalProperties:
appendRequestToHandler: true
interfaceOnly: true
useSpringBoot3: true
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class {{classname}}Controller({{#serviceInterface}}@Autowired(required = true) v
produces = [{{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}]{{/hasProduces}}{{#hasConsumes}},
consumes = [{{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}}]{{/hasConsumes}}{{/singleContentTypes}}
)
{{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>requesObject}}{{^-last}},{{/-last}}{{/allParams}}): ResponseEntity<{{>returnTypes}}> {
{{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>cookieParams}}{{>bodyParams}}{{>formParams}}{{>requesObject}}{{^-last}},{{/-last}}{{/allParams}}): ResponseEntity<{{>returnTypes}}> {
return {{>returnValue}}
}
{{/operation}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ interface {{classname}} {
produces = [{{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}]{{/hasProduces}}{{#hasConsumes}},
consumes = [{{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}}]{{/hasConsumes}}{{/singleContentTypes}}
)
{{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>requesObject}}{{^-last}},{{/-last}}{{/allParams}}): ResponseEntity<{{>returnTypes}}>{{^skipDefaultInterface}} {
{{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>cookieParams}}{{>bodyParams}}{{>formParams}}{{>requesObject}}{{^-last}},{{/-last}}{{/allParams}}): ResponseEntity<{{>returnTypes}}>{{^skipDefaultInterface}} {
{{^isDelegate}}
return {{>returnValue}}
{{/isDelegate}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{#isCookieParam}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}@CookieValue(name = "{{baseName}}"{{^required}}, required = false{{/required}}{{#defaultValue}}, defaultValue = "{{{.}}}"{{/defaultValue}}) {{paramName}}: {{>optionalDataType}}{{/isCookieParam}}
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,48 @@ public void skipDefaultInterface() throws Exception {
);
}

@Test(description = "test cookie parameter generation on interface apis")
public void cookieParameterGenerationApis() throws Exception {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');

KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(KotlinSpringServerCodegen.INTERFACE_ONLY, true);
codegen.additionalProperties().put(KotlinSpringServerCodegen.SKIP_DEFAULT_INTERFACE, true);

new DefaultGenerator().opts(new ClientOptInput()
.openAPI(TestUtils.parseSpec("src/test/resources/3_0/kotlin/petstore-with-fake-endpoints-for-testing-with-cookie.yaml"))
.config(codegen))
.generate();

assertFileContains(
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/FakeApi.kt"),
"@CookieValue"
);
}

@Test(description = "test cookie parameter generation on controllers")
public void cookieParameterGenerationControllers() throws Exception {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();
String outputPath = output.getAbsolutePath().replace('\\', '/');

KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
codegen.setOutputDir(output.getAbsolutePath());

new DefaultGenerator().opts(new ClientOptInput()
.openAPI(TestUtils.parseSpec("src/test/resources/3_0/kotlin/petstore-with-fake-endpoints-for-testing-with-cookie.yaml"))
.config(codegen))
.generate();

assertFileContains(
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/FakeApiController.kt"),
"@CookieValue"
);
}

@Test(description = "use Spring boot 3 & jakarta extension")
public void useSpringBoot3() throws Exception {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
Expand Down
Loading

0 comments on commit 7d9f9d7

Please sign in to comment.