Skip to content

Commit e040bec

Browse files
committed
add tests for both exploded and non-exploded params
1 parent 8aad0ee commit e040bec

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4483,6 +4483,31 @@ public void generateSortValidationWorksForExternalParamRefArraySort() throws Exc
44834483
"findPetsWithExternalParamRefArraySort should have a pageable: Pageable parameter");
44844484
}
44854485

4486+
@Test
4487+
public void generateSortValidationWorksForNonExplodedExternalParamRefArraySort() throws Exception {
4488+
Map<String, Object> additionalProperties = new HashMap<>();
4489+
additionalProperties.put(USE_TAGS, "true");
4490+
additionalProperties.put(INTERFACE_ONLY, "true");
4491+
additionalProperties.put(SKIP_DEFAULT_INTERFACE, "true");
4492+
additionalProperties.put(GENERATE_SORT_VALIDATION, "true");
4493+
4494+
Map<String, File> files = generateFromContract("src/test/resources/3_0/spring/petstore-sort-validation.yaml", additionalProperties);
4495+
4496+
File petApi = files.get("PetApi.kt");
4497+
String content = Files.readString(petApi.toPath());
4498+
4499+
// findPetsWithNonExplodedExternalParamRefArraySort: sort param $ref to external file,
4500+
// explode: false — Spring parses ?sort=id,asc,name,desc as sequential token pairs.
4501+
// @ValidSort validation works the same way since it operates on the deserialized Pageable.
4502+
int methodStart = content.indexOf("fun findPetsWithNonExplodedExternalParamRefArraySort(");
4503+
Assert.assertTrue(methodStart >= 0, "findPetsWithNonExplodedExternalParamRefArraySort method should exist");
4504+
String paramBlock = content.substring(methodStart, Math.min(content.length(), methodStart + 500));
4505+
Assert.assertTrue(paramBlock.contains("@ValidSort(allowedValues = [\"name,asc\", \"name,desc\", \"id,asc\", \"id,desc\"])"),
4506+
"@ValidSort with PetSortEnum values should appear even for non-exploded array sort param");
4507+
Assert.assertTrue(paramBlock.contains("pageable: Pageable"),
4508+
"findPetsWithNonExplodedExternalParamRefArraySort should have a pageable: Pageable parameter");
4509+
}
4510+
44864511
// ========== PAGEABLE DEFAULTS TESTS ==========
44874512

44884513
@Test

modules/openapi-generator/src/test/resources/3_0/spring/petstore-sort-validation-components.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ components:
1010
default: []
1111
items:
1212
$ref: '#/components/schemas/PetSortEnum'
13+
PetSortParamNonExploded:
14+
name: sort
15+
in: query
16+
required: false
17+
style: form
18+
explode: false
19+
description: Sort order — non-exploded multi-column (e.g. sort=id,asc,name,desc), each token pair must match the allowed enum values
20+
schema:
21+
type: array
22+
items:
23+
$ref: '#/components/schemas/PetSortEnum'
1324
schemas:
1425
PetSortEnum:
1526
type: string

modules/openapi-generator/src/test/resources/3_0/spring/petstore-sort-validation.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,34 @@ paths:
512512
type: array
513513
items:
514514
$ref: '#/components/schemas/Pet'
515+
/pet/findWithNonExplodedExternalParamRefArraySort:
516+
get:
517+
tags:
518+
- pet
519+
summary: Find pets with x-spring-paginated and non-exploded sort param referenced from an external components file
520+
operationId: findPetsWithNonExplodedExternalParamRefArraySort
521+
x-spring-paginated: true
522+
parameters:
523+
- name: page
524+
in: query
525+
schema:
526+
type: integer
527+
default: 0
528+
- name: size
529+
in: query
530+
schema:
531+
type: integer
532+
default: 20
533+
- $ref: './petstore-sort-validation-components.yaml#/components/parameters/PetSortParamNonExploded'
534+
responses:
535+
200:
536+
description: successful operation
537+
content:
538+
application/json:
539+
schema:
540+
type: array
541+
items:
542+
$ref: '#/components/schemas/Pet'
515543
components:
516544
schemas:
517545
PetSort:

0 commit comments

Comments
 (0)