Skip to content

Commit

Permalink
[Java][Spring] Add Spring pagination with vendor extension x-spring-p…
Browse files Browse the repository at this point in the history
…aginated (#5022)

Co-authored-by: Mathias Hinterndorfer <[email protected]>
Co-authored-by: Mathias Hinterndorfer <[email protected]>
Co-authored-by: Jim Schubert <[email protected]>
  • Loading branch information
4 people authored Oct 2, 2020
1 parent 4fe76fc commit 206f3f7
Show file tree
Hide file tree
Showing 523 changed files with 50,884 additions and 7 deletions.
8 changes: 8 additions & 0 deletions bin/configs/spring-cloud-petstore-feign-spring-pageable.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
generatorName: spring
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore-with-spring-pageable.yaml
outputDir: samples/client/petstore/spring-cloud-spring-pageable
artifactId: spring-cloud-spring-pageable
library: spring-cloud
additionalProperties:
hideGenerationTimestamp: 'true'
8 changes: 8 additions & 0 deletions bin/configs/spring-mvc-petstore-server-spring-pageable.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
generatorName: spring
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing-with-spring-pageable.yaml
outputDir: samples/server/petstore/spring-mvc-spring-pageable
artifactId: spring-mvc-spring-pageable
library: spring-mvc
additionalProperties:
hideGenerationTimestamp: 'true'
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
generatorName: spring
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing-with-spring-pageable.yaml
outputDir: samples/server/petstore/springboot-spring-pageable-delegatePattern-without-j8
artifactId: springboot-spring-pageable-delegatePattern-without-j8
library: spring-boot
delegatePattern: true
java8: false
additionalProperties:
hideGenerationTimestamp: 'true'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
generatorName: spring
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing-with-spring-pageable.yaml
outputDir: samples/server/petstore/springboot-spring-pageable-delegatePattern
artifactId: springboot-spring-pageable-delegatePattern
library: spring-boot
delegatePattern: true
additionalProperties:
hideGenerationTimestamp: 'true'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
generatorName: spring
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing-with-spring-pageable.yaml
outputDir: samples/server/petstore/springboot-spring-pageable-without-j8
artifactId: springboot-spring-pageable-withoutj8
library: spring-boot
java8: false
additionalProperties:
hideGenerationTimestamp: 'true'
8 changes: 8 additions & 0 deletions bin/configs/springboot-petstore-server-spring-pageable.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
generatorName: spring
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing-with-spring-pageable.yaml
outputDir: samples/server/petstore/springboot-spring-pageable
artifactId: springboot-spring-pageable
library: spring-boot
additionalProperties:
hideGenerationTimestamp: 'true'
5 changes: 5 additions & 0 deletions modules/openapi-generator-maven-plugin/examples/spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<!--SpringFox dependencies -->
<dependency>
<groupId>io.springfox</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.springframework.http.ResponseEntity;
{{#useBeanValidation}}
import org.springframework.validation.annotation.Validated;
{{/useBeanValidation}}
import org.springframework.data.domain.Pageable;

This comment has been minimized.

Copy link
@Andreas-Schaf

Andreas-Schaf Feb 9, 2021

These imports are only necessary if a Pagable is actually used (line 37 as well). Can they be made optional, e.g. by using {{vendorExtensions.x-spring-paginated}}? They require spring data as a dependency which we do not use.

import org.springframework.web.bind.annotation.*;
{{#jdk8-no-delegate}}
{{^reactive}}
Expand All @@ -33,6 +34,7 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.http.codec.multipart.Part;
{{/reactive}}
import springfox.documentation.annotations.ApiIgnore;

{{#useBeanValidation}}
import javax.validation.Valid;
Expand Down Expand Up @@ -124,19 +126,19 @@ public interface {{classname}} {
produces = { {{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }{{/hasProduces}}{{#hasConsumes}},
consumes = { {{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }{{/hasConsumes}}{{/singleContentTypes}}
)
{{#jdk8-default-interface}}default {{/jdk8-default-interface}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{#delegate-method}}_{{/delegate-method}}{{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{#hasMore}},{{/hasMore}}{{^hasMore}}{{#reactive}}, {{/reactive}}{{/hasMore}}{{/allParams}}{{#reactive}}ServerWebExchange exchange{{/reactive}}){{^jdk8-default-interface}};{{/jdk8-default-interface}}{{#jdk8-default-interface}}{{#unhandledException}} throws Exception{{/unhandledException}} {
{{#jdk8-default-interface}}default {{/jdk8-default-interface}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{#delegate-method}}_{{/delegate-method}}{{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{#hasMore}},{{/hasMore}}{{^hasMore}}{{#reactive}}, {{/reactive}}{{/hasMore}}{{/allParams}}{{#reactive}}ServerWebExchange exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}, @ApiIgnore final Pageable pageable{{/vendorExtensions.x-spring-paginated}}){{^jdk8-default-interface}};{{/jdk8-default-interface}}{{#jdk8-default-interface}}{{#unhandledException}} throws Exception{{/unhandledException}} {
{{#delegate-method}}
return {{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}exchange{{/reactive}});
return {{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}, pageable{{/vendorExtensions.x-spring-paginated}});
}

// Override this method
{{#jdk8-default-interface}}default {{/jdk8-default-interface}} {{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{^isFile}}{{^isBodyParam}}{{>optionalDataType}}{{/isBodyParam}}{{#isBodyParam}}{{^reactive}}{{{dataType}}}{{/reactive}}{{#reactive}}{{^isListContainer}}Mono{{/isListContainer}}{{#isListContainer}}Flux{{/isListContainer}}<{{{baseType}}}>{{/reactive}}{{/isBodyParam}}{{/isFile}}{{#isFile}}{{#reactive}}Flux<Part>{{/reactive}}{{^reactive}}MultipartFile{{/reactive}}{{/isFile}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}ServerWebExchange exchange{{/reactive}}){{#unhandledException}} throws Exception{{/unhandledException}} {
{{#jdk8-default-interface}}default {{/jdk8-default-interface}} {{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{^isFile}}{{^isBodyParam}}{{>optionalDataType}}{{/isBodyParam}}{{#isBodyParam}}{{^reactive}}{{{dataType}}}{{/reactive}}{{#reactive}}{{^isListContainer}}Mono{{/isListContainer}}{{#isListContainer}}Flux{{/isListContainer}}<{{{baseType}}}>{{/reactive}}{{/isBodyParam}}{{/isFile}}{{#isFile}}{{#reactive}}Flux<Part>{{/reactive}}{{^reactive}}MultipartFile{{/reactive}}{{/isFile}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}ServerWebExchange exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}, @ApiIgnore final Pageable pageable{{/vendorExtensions.x-spring-paginated}}){{#unhandledException}} throws Exception{{/unhandledException}} {
{{/delegate-method}}
{{^isDelegate}}
{{>methodBody}}
{{/isDelegate}}
{{#isDelegate}}
return getDelegate().{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}exchange{{/reactive}});
return getDelegate().{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}, pageable{{/vendorExtensions.x-spring-paginated}});
{{/isDelegate}}
}{{/jdk8-default-interface}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import org.springframework.web.context.request.NativeWebRequest;
{{/isDelegate}}
{{^jdk8}}
import org.springframework.web.multipart.MultipartFile;
import org.springframework.data.domain.Pageable;
import springfox.documentation.annotations.ApiIgnore;

{{#useBeanValidation}}
import javax.validation.constraints.*;
Expand Down Expand Up @@ -110,7 +112,7 @@ public class {{classname}}Controller implements {{classname}} {
{{/externalDocs}}
* @see {{classname}}#{{operationId}}
*/
public {{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{#hasMore}},{{/hasMore}}{{/allParams}}) {
public {{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{#hasMore}},{{/hasMore}}{{/allParams}}{{#vendorExtensions.x-spring-paginated}}, @ApiIgnore final Pageable pageable{{/vendorExtensions.x-spring-paginated}}) {
{{^isDelegate}}
{{^async}}
{{>methodBody}}
Expand All @@ -125,7 +127,7 @@ public class {{classname}}Controller implements {{classname}} {
{{/async}}
{{/isDelegate}}
{{#isDelegate}}
return delegate.{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
return delegate.{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#vendorExtensions.x-spring-paginated}}, pageable{{/vendorExtensions.x-spring-paginated}});
{{/isDelegate}}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package {{package}};
{{#imports}}import {{import}};
{{/imports}}
import io.swagger.annotations.*;
import org.springframework.data.domain.Pageable;
{{#jdk8}}
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -70,7 +71,7 @@ public interface {{classname}}Delegate {
*/
{{#jdk8-default-interface}}default {{/jdk8-default-interface}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{^isFile}}{{^isBodyParam}}{{>optionalDataType}}{{/isBodyParam}}{{#isBodyParam}}{{^reactive}}{{{dataType}}}{{/reactive}}{{#reactive}}{{^isListContainer}}Mono{{/isListContainer}}{{#isListContainer}}Flux{{/isListContainer}}<{{{baseType}}}>{{/reactive}}{{/isBodyParam}}{{/isFile}}{{#isFile}}{{#isListContainer}}List<{{/isListContainer}}{{#reactive}}Flux<Part>{{/reactive}}{{^reactive}}MultipartFile{{/reactive}}{{#isListContainer}}>{{/isListContainer}}{{/isFile}} {{paramName}}{{#hasMore}},
{{/hasMore}}{{/allParams}}{{#reactive}}{{#hasParams}},
{{/hasParams}}ServerWebExchange exchange{{/reactive}}){{#unhandledException}} throws Exception{{/unhandledException}}{{^jdk8-default-interface}};{{/jdk8-default-interface}}{{#jdk8-default-interface}} {
{{/hasParams}}ServerWebExchange exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}, final Pageable pageable{{/vendorExtensions.x-spring-paginated}}){{#unhandledException}} throws Exception{{/unhandledException}}{{^jdk8-default-interface}};{{/jdk8-default-interface}}{{#jdk8-default-interface}} {
{{>methodBody}}
}{{/jdk8-default-interface}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
{{#useSpringfox}}
<springfox-version>2.9.2</springfox-version>
{{/useSpringfox}}
<jackson-databind-version>2.9.8</jackson-databind-version>
</properties>
{{#parentOverridden}}
<parent>
Expand Down Expand Up @@ -82,6 +83,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web{{#reactive}}flux{{/reactive}}</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
{{#useSpringfox}}
<!--SpringFox dependencies -->
<dependency>
Expand All @@ -101,6 +106,11 @@
</dependency>
{{/useSpringfox}}
{{^useSpringfox}}
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
Expand Down Expand Up @@ -189,5 +199,10 @@
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
{{/hateoas}}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind-version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,14 @@
{{/parentOverridden}}
</dependency>
{{/useBeanValidation}}
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import org.threeten.bp.OffsetDateTime;
import org.threeten.bp.ZonedDateTime;
{{/threetenbp}}

import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;

import java.util.List;

{{>generatedAnnotation}}
Expand Down Expand Up @@ -63,6 +66,12 @@ public class OpenAPIUiConfiguration extends WebMvcConfigurerAdapter {
}
}

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new PageableHandlerMethodArgumentResolver());
super.addArgumentResolvers(argumentResolvers);
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!registry.hasMappingForPattern("/webjars/**")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@
</dependency>
{{/useSpringfox}}
{{^useSpringfox}}
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-version}</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
Expand Down Expand Up @@ -253,6 +264,16 @@
<version>1.0.1.RELEASE</version>
</dependency>
{{/hateoas}}
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind-version}</version>
</dependency>
</dependencies>
<properties>
<java.version>{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}</java.version>
Expand All @@ -272,5 +293,6 @@
{{#openApiNullable}}
<jackson-databind-nullable-version>0.2.1</jackson-databind-nullable-version>
{{/openApiNullable}}
<jackson-databind-version>2.9.8</jackson-databind-version>
</properties>
</project>
Loading

0 comments on commit 206f3f7

Please sign in to comment.