add tests for issue #21238

This commit is contained in:
Peter Storch 2025-05-11 19:53:23 +02:00
parent a87f55900f
commit b4d799f010
2 changed files with 116 additions and 0 deletions

View File

@ -1137,4 +1137,59 @@ public class KotlinSpringServerCodegenTest {
assertFileNotContains(Paths.get(output + "/src/main/kotlin/org/openapitools/api/TestV1ApiService.kt"),
"Flow<kotlin.String>");
}
@Test
public void testValidationsInQueryParams_issue21238_Controller() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
codegen.setOutputDir(output.getAbsolutePath());
List<File> 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<File> 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]+$\")");
}
}

View File

@ -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