Skip to content

Commit d18ee54

Browse files
sburnickiМышкин Максим
and
Мышкин Максим
authored
[kotlin-spring] Support model objects and date-time query params (#8257)
* [kotlin-spring] Support query params with model objects (#8080) Must fix #8080 * [kotlin-spring] Support parsing date and date-time parameters adopted #3860 for kotlin-spring Co-authored-by: Мышкин Максим <[email protected]>
1 parent 870ef3a commit d18ee54

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#isQueryParam}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{#swaggerAnnotations}}@ApiParam(value = "{{{description}}}"{{#required}}, required = true{{/required}}{{#allowableValues}}, allowableValues = "{{#values}}{{{.}}}{{^-last}}, {{/-last}}{{#-last}}{{/-last}}{{/values}}"{{/allowableValues}}{{^isContainer}}{{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}{{/isContainer}}) {{#useBeanValidation}}@Valid{{/useBeanValidation}}{{/swaggerAnnotations}} @RequestParam(value = "{{baseName}}"{{#required}}, required = true{{/required}}{{^required}}, required = false{{/required}}{{^isContainer}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}{{/isContainer}}) {{paramName}}: {{>optionalDataType}}{{/isQueryParam}}
1+
{{#isQueryParam}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{#swaggerAnnotations}}@ApiParam(value = "{{{description}}}"{{#required}}, required = true{{/required}}{{#allowableValues}}, allowableValues = "{{#values}}{{{.}}}{{^-last}}, {{/-last}}{{#-last}}{{/-last}}{{/values}}"{{/allowableValues}}{{^isContainer}}{{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}{{/isContainer}}) {{#useBeanValidation}}@Valid{{/useBeanValidation}}{{/swaggerAnnotations}}{{^isModel}} @RequestParam(value = "{{baseName}}"{{#required}}, required = true{{/required}}{{^required}}, required = false{{/required}}{{^isContainer}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}{{/isContainer}}){{/isModel}}{{#isDate}} @org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE){{/isDate}}{{#isDateTime}} @org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME){{/isDateTime}} {{paramName}}: {{>optionalDataType}}{{/isQueryParam}}

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

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
package org.openapitools.codegen.kotlin.spring;
22

33
import com.google.common.collect.testing.Helpers;
4+
import io.swagger.parser.OpenAPIParser;
45
import io.swagger.v3.oas.models.OpenAPI;
56
import io.swagger.v3.oas.models.info.Info;
67
import io.swagger.v3.oas.models.servers.Server;
8+
import io.swagger.v3.parser.core.models.ParseOptions;
79
import org.apache.commons.io.FileUtils;
810
import org.openapitools.codegen.ClientOptInput;
911
import org.openapitools.codegen.CodegenConstants;
1012
import org.openapitools.codegen.DefaultGenerator;
1113
import org.openapitools.codegen.TestUtils;
1214
import org.openapitools.codegen.kotlin.KotlinTestUtils;
1315
import org.openapitools.codegen.languages.KotlinSpringServerCodegen;
16+
import org.openapitools.codegen.languages.features.CXFServerFeatures;
1417
import org.testng.Assert;
1518
import org.testng.annotations.Test;
1619

1720
import java.io.File;
21+
import java.io.IOException;
1822
import java.nio.file.Files;
1923
import java.nio.file.Paths;
2024
import java.util.Collections;
@@ -253,4 +257,107 @@ public void delegateReactiveWithTags() throws Exception {
253257
assertFileNotContains(Paths.get(output + "/src/main/kotlin/org/openapitools/api/TestV2ApiDelegate.kt"),
254258
"suspend fun", "ApiUtil");
255259
}
260+
261+
@Test
262+
public void doNotGenerateRequestParamForObjectQueryParam() throws IOException {
263+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
264+
output.deleteOnExit();
265+
String outputPath = output.getAbsolutePath().replace('\\', '/');
266+
267+
OpenAPI openAPI = new OpenAPIParser()
268+
.readLocation("src/test/resources/3_0/objectQueryParam.yaml", null, new ParseOptions()).getOpenAPI();
269+
270+
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
271+
codegen.setOutputDir(output.getAbsolutePath());
272+
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
273+
274+
ClientOptInput input = new ClientOptInput();
275+
input.openAPI(openAPI);
276+
input.config(codegen);
277+
278+
DefaultGenerator generator = new DefaultGenerator();
279+
280+
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "false");
281+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
282+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
283+
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
284+
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
285+
286+
generator.opts(input).generate();
287+
288+
assertFileNotContains(Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/PonyApi.kt"), "@RequestParam");
289+
}
290+
291+
@Test
292+
public void doGenerateRequestParamForSimpleParam() throws IOException {
293+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
294+
output.deleteOnExit();
295+
String outputPath = output.getAbsolutePath().replace('\\', '/');
296+
297+
OpenAPI openAPI = new OpenAPIParser()
298+
.readLocation("src/test/resources/3_0/issue_3248.yaml", null, new ParseOptions()).getOpenAPI();
299+
300+
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
301+
codegen.setOutputDir(output.getAbsolutePath());
302+
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
303+
304+
ClientOptInput input = new ClientOptInput();
305+
input.openAPI(openAPI);
306+
input.config(codegen);
307+
308+
DefaultGenerator generator = new DefaultGenerator();
309+
310+
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "false");
311+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
312+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
313+
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
314+
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
315+
316+
generator.opts(input).generate();
317+
318+
assertFileContains(Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/MonkeysApi.kt"), "@RequestParam");
319+
assertFileContains(Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/ElephantsApi.kt"), "@RequestParam");
320+
assertFileContains(Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/ZebrasApi.kt"), "@RequestParam");
321+
assertFileContains(Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/BearsApi.kt"), "@RequestParam");
322+
assertFileContains(Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/CamelsApi.kt"), "@RequestParam");
323+
assertFileContains(Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/PandasApi.kt"), "@RequestParam");
324+
assertFileContains(Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/CrocodilesApi.kt"), "@RequestParam");
325+
assertFileContains(Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/PolarBearsApi.kt"), "@RequestParam");
326+
}
327+
328+
@Test
329+
public void generateFormatForDateAndDateTimeQueryParam() throws IOException {
330+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
331+
output.deleteOnExit();
332+
String outputPath = output.getAbsolutePath().replace('\\', '/');
333+
334+
OpenAPI openAPI = new OpenAPIParser()
335+
.readLocation("src/test/resources/3_0/issue_2053.yaml", null, new ParseOptions()).getOpenAPI();
336+
337+
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
338+
codegen.setOutputDir(output.getAbsolutePath());
339+
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
340+
341+
ClientOptInput input = new ClientOptInput();
342+
input.openAPI(openAPI);
343+
input.config(codegen);
344+
345+
DefaultGenerator generator = new DefaultGenerator();
346+
347+
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "false");
348+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
349+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
350+
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
351+
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
352+
generator.opts(input).generate();
353+
354+
assertFileContains(
355+
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/ElephantsApi.kt"),
356+
"@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE)"
357+
);
358+
assertFileContains(
359+
Paths.get(outputPath + "/src/main/kotlin/org/openapitools/api/ZebrasApi.kt"),
360+
"@org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME)"
361+
);
362+
}
256363
}

0 commit comments

Comments
 (0)