Skip to content

Commit 9713d9f

Browse files
committed
implement tests
1 parent f0e587e commit 9713d9f

2 files changed

Lines changed: 202 additions & 1 deletion

File tree

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

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3813,7 +3813,7 @@ public void customPageableSchemaNotOverridden_issue13052() throws Exception {
38133813
additionalProperties.put(DOCUMENTATION_PROVIDER, "springdoc");
38143814
additionalProperties.put(INTERFACE_ONLY, "true");
38153815
additionalProperties.put(SKIP_DEFAULT_INTERFACE, "true");
3816-
3816+
38173817
Map<String, File> files = generateFromContract("src/test/resources/bugs/issue_13052.yaml", additionalProperties);
38183818

38193819
// Custom Pageable model should be used instead of Spring's Pageable
@@ -3824,6 +3824,155 @@ public void customPageableSchemaNotOverridden_issue13052() throws Exception {
38243824
assertFileContains(petApi.toPath(), "pageable: Pageable");
38253825
}
38263826

3827+
@Test
3828+
public void springPaginatedWithNoDocumentationProvider() throws Exception {
3829+
Map<String, Object> additionalProperties = new HashMap<>();
3830+
additionalProperties.put(USE_TAGS, "true");
3831+
additionalProperties.put(DOCUMENTATION_PROVIDER, "none");
3832+
additionalProperties.put(INTERFACE_ONLY, "true");
3833+
additionalProperties.put(SKIP_DEFAULT_INTERFACE, "true");
3834+
3835+
Map<String, File> files = generateFromContract("src/test/resources/3_0/spring/petstore-with-spring-pageable.yaml", additionalProperties);
3836+
3837+
// Pageable should be added but no annotation imports
3838+
File petApi = files.get("PetApi.kt");
3839+
assertFileContains(petApi.toPath(), "import org.springframework.data.domain.Pageable");
3840+
assertFileContains(petApi.toPath(), "pageable: Pageable");
3841+
assertFileNotContains(petApi.toPath(), "import springfox.documentation.annotations.ApiIgnore");
3842+
assertFileNotContains(petApi.toPath(), "import org.springdoc.api.annotations.ParameterObject");
3843+
assertFileNotContains(petApi.toPath(), "@ApiIgnore pageable");
3844+
assertFileNotContains(petApi.toPath(), "@ParameterObject pageable");
3845+
}
3846+
3847+
@Test
3848+
public void springPaginatedWithSwagger1AnnotationLibrary() throws Exception {
3849+
Map<String, Object> additionalProperties = new HashMap<>();
3850+
additionalProperties.put(USE_TAGS, "true");
3851+
additionalProperties.put(DOCUMENTATION_PROVIDER, "springfox");
3852+
additionalProperties.put(ANNOTATION_LIBRARY, "swagger1");
3853+
additionalProperties.put(INTERFACE_ONLY, "true");
3854+
additionalProperties.put(SKIP_DEFAULT_INTERFACE, "true");
3855+
3856+
Map<String, File> files = generateFromContract("src/test/resources/3_0/spring/petstore-with-spring-pageable.yaml", additionalProperties);
3857+
3858+
// Test with swagger1 annotations
3859+
File petApi = files.get("PetApi.kt");
3860+
assertFileContains(petApi.toPath(), "import org.springframework.data.domain.Pageable");
3861+
assertFileContains(petApi.toPath(), "import springfox.documentation.annotations.ApiIgnore");
3862+
assertFileContains(petApi.toPath(), "@ApiParam(hidden = true) pageable: Pageable");
3863+
}
3864+
3865+
@Test
3866+
public void springPaginatedNoParamsNoContext() throws Exception {
3867+
Map<String, Object> additionalProperties = new HashMap<>();
3868+
additionalProperties.put(USE_TAGS, "true");
3869+
additionalProperties.put(DOCUMENTATION_PROVIDER, "springdoc");
3870+
additionalProperties.put(INTERFACE_ONLY, "true");
3871+
additionalProperties.put(SKIP_DEFAULT_INTERFACE, "true");
3872+
3873+
Map<String, File> files = generateFromContract("src/test/resources/3_0/spring/petstore-with-spring-pageable.yaml", additionalProperties);
3874+
3875+
// Test operation listAllPets which has no parameters except pageable
3876+
File petApi = files.get("PetApi.kt");
3877+
assertFileContains(petApi.toPath(), "fun listAllPets(@Parameter(hidden = true) pageable: Pageable)");
3878+
}
3879+
3880+
@Test
3881+
public void springPaginatedMixedOperations() throws Exception {
3882+
Map<String, Object> additionalProperties = new HashMap<>();
3883+
additionalProperties.put(USE_TAGS, "true");
3884+
additionalProperties.put(DOCUMENTATION_PROVIDER, "springdoc");
3885+
additionalProperties.put(INTERFACE_ONLY, "true");
3886+
additionalProperties.put(SKIP_DEFAULT_INTERFACE, "true");
3887+
3888+
Map<String, File> files = generateFromContract("src/test/resources/3_0/spring/petstore-with-spring-pageable.yaml", additionalProperties);
3889+
3890+
File petApi = files.get("PetApi.kt");
3891+
3892+
// Operation with x-spring-paginated should have Pageable
3893+
assertFileContains(petApi.toPath(), "fun findPetsByStatus(");
3894+
assertFileContains(petApi.toPath(), "pageable: Pageable");
3895+
3896+
// Operation without x-spring-paginated should NOT have Pageable
3897+
assertFileContains(petApi.toPath(), "fun addPet(");
3898+
// Verify addPet doesn't have pageable (it has body param only)
3899+
String content = new String(java.nio.file.Files.readAllBytes(petApi.toPath()));
3900+
String addPetMethod = content.substring(
3901+
content.indexOf("fun addPet("),
3902+
content.indexOf(")", content.indexOf("fun addPet(")) + 1
3903+
);
3904+
Assert.assertFalse(addPetMethod.contains("pageable"),
3905+
"addPet should not have pageable parameter");
3906+
}
3907+
3908+
@Test
3909+
public void springPaginatedWithServiceInterface() throws Exception {
3910+
Map<String, Object> additionalProperties = new HashMap<>();
3911+
additionalProperties.put(USE_TAGS, "true");
3912+
additionalProperties.put(DOCUMENTATION_PROVIDER, "springdoc");
3913+
additionalProperties.put(SERVICE_INTERFACE, "true");
3914+
3915+
Map<String, File> files = generateFromContract("src/test/resources/3_0/spring/petstore-with-spring-pageable.yaml", additionalProperties);
3916+
3917+
// Test that pageable is in service interface
3918+
File petService = files.get("PetService.kt");
3919+
if (petService != null) {
3920+
assertFileContains(petService.toPath(), "import org.springframework.data.domain.Pageable");
3921+
assertFileContains(petService.toPath(), "pageable: Pageable");
3922+
}
3923+
}
3924+
3925+
@Test
3926+
public void springPaginatedParameterOrdering() throws Exception {
3927+
Map<String, Object> additionalProperties = new HashMap<>();
3928+
additionalProperties.put(USE_TAGS, "true");
3929+
additionalProperties.put(DOCUMENTATION_PROVIDER, "springdoc");
3930+
additionalProperties.put(INTERFACE_ONLY, "true");
3931+
additionalProperties.put(SKIP_DEFAULT_INTERFACE, "true");
3932+
additionalProperties.put(INCLUDE_HTTP_REQUEST_CONTEXT, "true");
3933+
3934+
Map<String, File> files = generateFromContract("src/test/resources/3_0/spring/petstore-with-spring-pageable.yaml", additionalProperties);
3935+
3936+
// Verify exact parameter ordering: allParams -> request -> pageable
3937+
File petApi = files.get("PetApi.kt");
3938+
String content = new String(java.nio.file.Files.readAllBytes(petApi.toPath()));
3939+
3940+
// Find findPetsByStatus method
3941+
int methodStart = content.indexOf("fun findPetsByStatus(");
3942+
int methodEnd = content.indexOf("): ResponseEntity", methodStart);
3943+
String methodSignature = content.substring(methodStart, methodEnd);
3944+
3945+
// Verify order: status param comes before request, request comes before pageable
3946+
int statusPos = methodSignature.indexOf("status:");
3947+
int requestPos = methodSignature.indexOf("request:");
3948+
int pageablePos = methodSignature.indexOf("pageable:");
3949+
3950+
Assert.assertTrue(statusPos > 0, "status parameter should exist");
3951+
Assert.assertTrue(requestPos > statusPos, "request should come after status");
3952+
Assert.assertTrue(pageablePos > requestPos, "pageable should come after request");
3953+
}
3954+
3955+
@Test
3956+
public void springPaginatedDelegateCallPassesPageable() throws Exception {
3957+
Map<String, Object> additionalProperties = new HashMap<>();
3958+
additionalProperties.put(USE_TAGS, "true");
3959+
additionalProperties.put(DOCUMENTATION_PROVIDER, "springdoc");
3960+
additionalProperties.put(DELEGATE_PATTERN, "true");
3961+
additionalProperties.put(INTERFACE_ONLY, "false");
3962+
3963+
Map<String, File> files = generateFromContract("src/test/resources/3_0/spring/petstore-with-spring-pageable.yaml", additionalProperties);
3964+
3965+
// Verify that interface method calls delegate with pageable parameter
3966+
File petApi = files.get("PetApi.kt");
3967+
String content = new String(java.nio.file.Files.readAllBytes(petApi.toPath()));
3968+
3969+
// Check for delegate call pattern with pageable
3970+
if (content.contains("getDelegate().findPetsByStatus")) {
3971+
assertFileContains(petApi.toPath(), "getDelegate().findPetsByStatus(");
3972+
assertFileContains(petApi.toPath(), "pageable)");
3973+
}
3974+
}
3975+
38273976
private Map<String, File> generateFromContract(String url) throws IOException {
38283977
return generateFromContract(url, new HashMap<>(), new HashMap<>());
38293978
}

modules/openapi-generator/src/test/resources/3_0/spring/petstore-with-spring-pageable.yaml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,58 @@ paths:
191191
- write:pets
192192
- read:pets
193193
x-spring-paginated: true
194+
/pet/all:
195+
get:
196+
tags:
197+
- pet
198+
summary: List all pets
199+
description: Returns all pets with pagination support
200+
operationId: listAllPets
201+
parameters:
202+
- name: page
203+
in: query
204+
description: "The page number to return. Test QueryParam for issue #8315 - must be removed when x-spring-paginated:true is used."
205+
required: false
206+
schema:
207+
type: integer
208+
format: int32
209+
default: 0
210+
- name: size
211+
in: query
212+
description: "The number of items to return per page. Test QueryParam for issue #8315 - must be removed when x-spring-paginated:true is used."
213+
required: false
214+
schema:
215+
type: integer
216+
format: int32
217+
default: 20
218+
- name: sort
219+
in: query
220+
description: "The sort order. Test QueryParam for issue #8315 - must be removed when x-spring-paginated:true is used."
221+
required: false
222+
schema:
223+
type: string
224+
responses:
225+
200:
226+
description: successful operation
227+
content:
228+
application/json:
229+
schema:
230+
type: array
231+
items:
232+
$ref: '#/components/schemas/Pet'
233+
application/xml:
234+
schema:
235+
type: array
236+
items:
237+
$ref: '#/components/schemas/Pet'
238+
400:
239+
description: Invalid status value
240+
content: {}
241+
security:
242+
- petstore_auth:
243+
- write:pets
244+
- read:pets
245+
x-spring-paginated: true
194246
/pet/{petId}:
195247
get:
196248
tags:

0 commit comments

Comments
 (0)