From b4d799f010a402047356f077575ddfaa018566e2 Mon Sep 17 00:00:00 2001 From: Peter Storch Date: Sun, 11 May 2025 19:53:23 +0200 Subject: [PATCH] add tests for issue #21238 --- .../spring/KotlinSpringServerCodegenTest.java | 55 +++++++++++++++++ .../issue21238_queryParam_validation.yaml | 61 +++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 modules/openapi-generator/src/test/resources/3_0/kotlin/issue21238_queryParam_validation.yaml diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java index c0c1f9d27c6..ba7b73e78ee 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java @@ -1137,4 +1137,59 @@ public class KotlinSpringServerCodegenTest { assertFileNotContains(Paths.get(output + "/src/main/kotlin/org/openapitools/api/TestV1ApiService.kt"), "Flow"); } + + @Test + public void testValidationsInQueryParams_issue21238_Controller() throws IOException { + File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); + KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen(); + codegen.setOutputDir(output.getAbsolutePath()); + + List files = new DefaultGenerator() + .opts( + new ClientOptInput() + .openAPI(TestUtils.parseSpec("src/test/resources/3_0/kotlin/issue21238_queryParam_validation.yaml")) + .config(codegen) + ) + .generate(); + + Assertions.assertThat(files).contains( + new File(output, "src/main/kotlin/org/openapitools/api/PetApiController.kt"), + new File(output, "src/main/kotlin/org/openapitools/api/UserApiController.kt") + ); + + assertFileContains(Paths.get(output + "/src/main/kotlin/org/openapitools/api/PetApiController.kt"), + "@NotNull", "@Valid"); + assertFileContains(Paths.get(output + "/src/main/kotlin/org/openapitools/api/UserApiController.kt"), + "@NotNull", "@Valid", + "@Pattern(regexp=\"^[a-zA-Z0-9]+[a-zA-Z0-9\\\\.\\\\-_]*[a-zA-Z0-9]+$\")", + "@Parameter(description = \"The user name for login\", required = true)", + "@Parameter(description = \"The password for login in clear text\", required = true)"); + } + + @Test + public void testValidationsInQueryParams_issue21238_Api_Delegate() throws IOException { + File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); + KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen(); + codegen.setOutputDir(output.getAbsolutePath()); + codegen.additionalProperties().put(KotlinSpringServerCodegen.DELEGATE_PATTERN, true); + + List files = new DefaultGenerator() + .opts( + new ClientOptInput() + .openAPI(TestUtils.parseSpec("src/test/resources/3_0/kotlin/issue21238_queryParam_validation.yaml")) + .config(codegen) + ) + .generate(); + + Assertions.assertThat(files).contains( + new File(output, "src/main/kotlin/org/openapitools/api/PetApi.kt"), + new File(output, "src/main/kotlin/org/openapitools/api/UserApi.kt") + ); + + assertFileContains(Paths.get(output + "/src/main/kotlin/org/openapitools/api/PetApi.kt"), + "@NotNull", "@Valid"); + assertFileContains(Paths.get(output + "/src/main/kotlin/org/openapitools/api/UserApi.kt"), + "@NotNull", "@Valid", "@Pattern(regexp=\"^[a-zA-Z0-9]+[a-zA-Z0-9\\\\.\\\\-_]*[a-zA-Z0-9]+$\")"); + } + } diff --git a/modules/openapi-generator/src/test/resources/3_0/kotlin/issue21238_queryParam_validation.yaml b/modules/openapi-generator/src/test/resources/3_0/kotlin/issue21238_queryParam_validation.yaml new file mode 100644 index 00000000000..411cfb2cf79 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/kotlin/issue21238_queryParam_validation.yaml @@ -0,0 +1,61 @@ +openapi: 3.0.0 +info: + description: "Example to test fix for issue 21238, queryParam validation" + license: + name: Apache-2.0 + url: https://www.apache.org/licenses/LICENSE-2.0.html + title: OpenAPI Query Param VAlidation + version: 1.0.0 +paths: + /pet/findByStatus: + get: + description: Multiple status values can be provided with comma separated strings + operationId: findPetsByStatus + parameters: + - deprecated: true + description: Status values that need to be considered for filter + explode: false + in: query + name: status + required: true + schema: + items: + default: available + enum: + - available + - pending + - sold + type: string + type: array + style: form + responses: + "200": + summary: Finds Pets by status + tags: + - pet + /user/login: + get: + operationId: loginUser + parameters: + - description: The user name for login + explode: true + in: query + name: username + required: true + schema: + pattern: "^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$" + type: string + style: form + - description: The password for login in clear text + explode: true + in: query + name: password + required: true + schema: + type: string + style: form + responses: + "200": + summary: Logs user into the system + tags: + - user