spring: fix spring pageable used without any config (#15113) (fix #13052)

* spring: fix spring pageable used without any config

Spring Pageable is imported whenever we have an API with
a Pageable Json schema, even if x-spring-paginated is not
set.

This commit imports Spring Pageable only if
x-spring-paginated is set to true.

* spring: add unit test for pageable fix

* spring: fix spring pageable feedback from review

remove unrelated example

---------

Co-authored-by: gonzalad <gonzalad@users.noreply.github.com>
This commit is contained in:
gonzalad
2023-05-30 07:42:33 +02:00
committed by GitHub
parent 8bad27e0ab
commit 8a0f374a45
3 changed files with 89 additions and 3 deletions

View File

@@ -530,7 +530,6 @@ public class SpringCodegen extends AbstractJavaCodegen
typeMapping.put("file", "org.springframework.core.io.Resource");
importMapping.put("org.springframework.core.io.Resource", "org.springframework.core.io.Resource");
importMapping.put("Pageable", "org.springframework.data.domain.Pageable");
importMapping.put("DateTimeFormat", "org.springframework.format.annotation.DateTimeFormat");
importMapping.put("ApiIgnore", "springfox.documentation.annotations.ApiIgnore");
importMapping.put("ParameterObject", "org.springdoc.api.annotations.ParameterObject");
@@ -1191,6 +1190,13 @@ public class SpringCodegen extends AbstractJavaCodegen
*/
@Override
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List<Server> servers) {
// add Pageable import only if x-spring-paginated explicitly used
// this allows to use a custom Pageable schema without importing Spring Pageable.
if (Boolean.TRUE.equals(operation.getExtensions().get("x-spring-paginated"))) {
importMapping.put("Pageable", "org.springframework.data.domain.Pageable");
}
CodegenOperation codegenOperation = super.fromOperation(path, httpMethod, operation, servers);
// add org.springframework.format.annotation.DateTimeFormat when needed

View File

@@ -1385,7 +1385,6 @@ public class SpringCodegenTest {
final SpringCodegen codegen = new SpringCodegen();
codegen.processOpts();
Assert.assertEquals(codegen.importMapping().get("org.springframework.core.io.Resource"), "org.springframework.core.io.Resource");
Assert.assertEquals(codegen.importMapping().get("Pageable"), "org.springframework.data.domain.Pageable");
Assert.assertEquals(codegen.importMapping().get("DateTimeFormat"), "org.springframework.format.annotation.DateTimeFormat");
Assert.assertEquals(codegen.importMapping().get("ApiIgnore"), "springfox.documentation.annotations.ApiIgnore");
Assert.assertEquals(codegen.importMapping().get("ParameterObject"), "org.springdoc.api.annotations.ParameterObject");
@@ -1781,13 +1780,31 @@ public class SpringCodegenTest {
files = generateFromContract("src/test/resources/2_0/petstore-with-spring-pageable.yaml", SPRING_BOOT, additionalProperties);
JavaFileAssert.assertThat(files.get("PetApi.java"))
.hasImports("org.springdoc.core.annotations.ParameterObject")
.hasImports("org.springdoc.core.annotations.ParameterObject", "org.springframework.data.domain.Pageable")
.assertMethod("findPetsByStatus")
.hasParameter("pageable").withType("Pageable")
.assertParameterAnnotations()
.containsWithName("ParameterObject");
}
@Test
public void paramPageableIsNotSpringPaginated_issue13052() throws Exception {
Map<String, Object> additionalProperties = new HashMap<>();
additionalProperties.put(SpringCodegen.USE_TAGS, "true");
additionalProperties.put(DOCUMENTATION_PROVIDER, "springdoc");
additionalProperties.put(SpringCodegen.INTERFACE_ONLY, "true");
additionalProperties.put(SpringCodegen.SKIP_DEFAULT_INTERFACE, "true");
additionalProperties.put(USE_SPRING_BOOT3, "true");
Map<String, File> files = generateFromContract("src/test/resources/bugs/issue_13052.yaml", SPRING_BOOT, additionalProperties);
JavaFileAssert.assertThat(files.get("PetApi.java"))
.hasImports("org.openapitools.model.Pageable")
.hasNoImports("org.springframework.data.domain.Pageable", "org.springdoc.core.annotations.ParameterObject")
.assertMethod("findPageable")
.hasParameter("pageable").withType("Pageable");
}
@Test
public void shouldSetDefaultValueForMultipleArrayItems() throws IOException {
Map<String, Object> additionalProperties = new HashMap<>();

View File

@@ -0,0 +1,63 @@
openapi: 3.0.1
info:
title: OpenAPI Petstore
description: This is a sample server Petstore server. For this sample, you can use
the api key `special-key` to test the authorization filters.
license:
name: Apache-2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
version: 1.0.0
servers:
- url: http://petstore.swagger.io/v2
tags:
- name: pet
description: Everything about your Pets
- name: store
description: Access to Petstore orders
- name: user
description: Operations about user
paths:
/pet/findPageable:
get:
tags:
- pet
summary: Finds Pets by status
description: Multiple status values can be provided with comma separated strings
operationId: findPageable
parameters:
- name: pageable
in: query
style: form
explode: true
schema:
$ref: "#/components/schemas/Pageable"
responses:
200:
description: successful operation
content:
application/xml:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
400:
description: Invalid status value
content: {}
components:
schemas:
Pageable:
type: object
properties:
page:
type: integer
description: page number
size:
type: integer
description: page size