Skip to content

Commit

Permalink
[Spring] Pageable & ApiIgnore imports depend on config
Browse files Browse the repository at this point in the history
  • Loading branch information
morphlne committed Mar 10, 2021
1 parent 2f047fe commit 0d1c88f
Show file tree
Hide file tree
Showing 75 changed files with 8,881 additions and 2 deletions.
8 changes: 8 additions & 0 deletions bin/configs/spring-boot-no-pagination.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
generatorName: spring
outputDir: samples/server/petstore/springboot-no-pagination
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
additionalProperties:
artifactId: springboot-no-pagination
hideGenerationTimestamp: "true"
usePagination: "false"
1 change: 1 addition & 0 deletions docs/generators/spring.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|unhandledException|Declare operation methods to throw a generic exception and allow unhandled exceptions (useful for Spring `@ControllerAdvice` directives).| |false|
|useBeanValidation|Use BeanValidation API annotations| |true|
|useOptional|Use Optional container for optional parameters| |false|
|usePagination|Import Spring Data Pageable & Springfox ApiIgnore annotations| |true|
|useTags|use tags for creating interface and controller classnames| |false|
|virtualService|Generates the virtual service. For more details refer - https://github.com/virtualansoftware/virtualan/wiki| |false|
|withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class SpringCodegen extends AbstractJavaCodegen
public static final String SINGLE_CONTENT_TYPES = "singleContentTypes";
public static final String VIRTUAL_SERVICE = "virtualService";
public static final String SKIP_DEFAULT_INTERFACE = "skipDefaultInterface";
public static final String USE_PAGINATION = "usePagination";

public static final String JAVA_8 = "java8";
public static final String ASYNC = "async";
Expand Down Expand Up @@ -98,6 +99,7 @@ public class SpringCodegen extends AbstractJavaCodegen
protected boolean hateoas = false;
protected boolean returnSuccessCode = false;
protected boolean unhandledException = false;
protected boolean usePagination = true;

public SpringCodegen() {
super();
Expand Down Expand Up @@ -171,6 +173,7 @@ public SpringCodegen() {
cliOptions.add(CliOption.newBoolean(HATEOAS, "Use Spring HATEOAS library to allow adding HATEOAS links", hateoas));
cliOptions.add(CliOption.newBoolean(RETURN_SUCCESS_CODE, "Generated server returns 2xx code", returnSuccessCode));
cliOptions.add(CliOption.newBoolean(UNHANDLED_EXCEPTION_HANDLING, "Declare operation methods to throw a generic exception and allow unhandled exceptions (useful for Spring `@ControllerAdvice` directives).", unhandledException));
cliOptions.add(CliOption.newBoolean(USE_PAGINATION, "Import Spring Data Pageable & Springfox ApiIgnore annotations", this.usePagination));

supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application using the SpringFox integration.");
supportedLibraries.put(SPRING_MVC_LIBRARY, "Spring-MVC Server application using the SpringFox integration.");
Expand Down Expand Up @@ -336,6 +339,11 @@ public void processOpts() {
}
additionalProperties.put(UNHANDLED_EXCEPTION_HANDLING, this.isUnhandledException());

if (additionalProperties.containsKey(USE_PAGINATION)) {
this.setUsePagination(Boolean.parseBoolean(additionalProperties.get(USE_PAGINATION).toString()));
}
additionalProperties.put(USE_PAGINATION, usePagination);

typeMapping.put("file", "org.springframework.core.io.Resource");
importMapping.put("org.springframework.core.io.Resource", "org.springframework.core.io.Resource");

Expand Down Expand Up @@ -812,6 +820,10 @@ public void setUnhandledException(boolean unhandledException) {
this.unhandledException = unhandledException;
}

public void setUsePagination(final boolean usePagination) {
this.usePagination = usePagination;
}

@Override
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
super.postProcessModelProperty(model, property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import org.springframework.http.ResponseEntity;
{{#useBeanValidation}}
import org.springframework.validation.annotation.Validated;
{{/useBeanValidation}}
{{#usePagination}}
import org.springframework.data.domain.Pageable;
{{/usePagination}}
import org.springframework.web.bind.annotation.*;
{{#jdk8-no-delegate}}
{{^reactive}}
Expand All @@ -34,7 +36,9 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.http.codec.multipart.Part;
{{/reactive}}
{{#usePagination}}
import springfox.documentation.annotations.ApiIgnore;
{{/usePagination}}

{{#useBeanValidation}}
import javax.validation.Valid;
Expand Down Expand Up @@ -125,13 +129,13 @@ public interface {{classname}} {
produces = { {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} }{{/hasProduces}}{{#hasConsumes}},
consumes = { {{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/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}}{{^-last}},{{/-last}}{{#-last}}{{#reactive}}, {{/reactive}}{{/-last}}{{/allParams}}{{#reactive}}@ApiIgnore final 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}} {
{{#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}}{{^-last}},{{/-last}}{{#-last}}{{#reactive}}, {{/reactive}}{{/-last}}{{/allParams}}{{#reactive}}{{#usePagination}}@ApiIgnore{{/usePagination}} final 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}}{{^-last}}, {{/-last}}{{/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}}{{^isArray}}Mono<{{{dataType}}}>{{/isArray}}{{#isArray}}Flux<{{{baseType}}}>{{/isArray}}{{/reactive}}{{/isBodyParam}}{{/isFile}}{{#isFile}}{{#reactive}}Flux<Part>{{/reactive}}{{^reactive}}MultipartFile{{/reactive}}{{/isFile}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}@ApiIgnore final ServerWebExchange exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}, @ApiIgnore final Pageable pageable{{/vendorExtensions.x-spring-paginated}}){{#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}}{{^isArray}}Mono<{{{dataType}}}>{{/isArray}}{{#isArray}}Flux<{{{baseType}}}>{{/isArray}}{{/reactive}}{{/isBodyParam}}{{/isFile}}{{#isFile}}{{#reactive}}Flux<Part>{{/reactive}}{{^reactive}}MultipartFile{{/reactive}}{{/isFile}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{#reactive}}{{#hasParams}}, {{/hasParams}}}{{#usePagination}}@ApiIgnore {{/usePagination}}final ServerWebExchange exchange{{/reactive}}{{#vendorExtensions.x-spring-paginated}}, @ApiIgnore final Pageable pageable{{/vendorExtensions.x-spring-paginated}}){{#unhandledException}} throws Exception{{/unhandledException}} {
{{/delegate-method}}
{{^isDelegate}}
{{>methodBody}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import org.springframework.web.context.request.NativeWebRequest;
{{/isDelegate}}
{{^jdk8}}
import org.springframework.web.multipart.MultipartFile;
{{#usePagination}}
import org.springframework.data.domain.Pageable;
import springfox.documentation.annotations.ApiIgnore;
{{/usePagination}}

{{#useBeanValidation}}
import javax.validation.constraints.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package {{package}};
{{#imports}}import {{import}};
{{/imports}}
import io.swagger.annotations.*;
{{#usePagination}}
import org.springframework.data.domain.Pageable;
{{/usePagination}}
{{#jdk8}}
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
README.md
pom.xml
src/main/java/org/openapitools/OpenAPI2SpringBoot.java
src/main/java/org/openapitools/RFC3339DateFormat.java
src/main/java/org/openapitools/api/AnotherFakeApi.java
src/main/java/org/openapitools/api/AnotherFakeApiController.java
src/main/java/org/openapitools/api/ApiUtil.java
src/main/java/org/openapitools/api/FakeApi.java
src/main/java/org/openapitools/api/FakeApiController.java
src/main/java/org/openapitools/api/FakeClassnameTestApi.java
src/main/java/org/openapitools/api/FakeClassnameTestApiController.java
src/main/java/org/openapitools/api/PetApi.java
src/main/java/org/openapitools/api/PetApiController.java
src/main/java/org/openapitools/api/StoreApi.java
src/main/java/org/openapitools/api/StoreApiController.java
src/main/java/org/openapitools/api/UserApi.java
src/main/java/org/openapitools/api/UserApiController.java
src/main/java/org/openapitools/configuration/HomeController.java
src/main/java/org/openapitools/configuration/OpenAPIDocumentationConfig.java
src/main/java/org/openapitools/model/AdditionalPropertiesAnyType.java
src/main/java/org/openapitools/model/AdditionalPropertiesArray.java
src/main/java/org/openapitools/model/AdditionalPropertiesBoolean.java
src/main/java/org/openapitools/model/AdditionalPropertiesClass.java
src/main/java/org/openapitools/model/AdditionalPropertiesInteger.java
src/main/java/org/openapitools/model/AdditionalPropertiesNumber.java
src/main/java/org/openapitools/model/AdditionalPropertiesObject.java
src/main/java/org/openapitools/model/AdditionalPropertiesString.java
src/main/java/org/openapitools/model/Animal.java
src/main/java/org/openapitools/model/ArrayOfArrayOfNumberOnly.java
src/main/java/org/openapitools/model/ArrayOfNumberOnly.java
src/main/java/org/openapitools/model/ArrayTest.java
src/main/java/org/openapitools/model/BigCat.java
src/main/java/org/openapitools/model/BigCatAllOf.java
src/main/java/org/openapitools/model/Capitalization.java
src/main/java/org/openapitools/model/Cat.java
src/main/java/org/openapitools/model/CatAllOf.java
src/main/java/org/openapitools/model/Category.java
src/main/java/org/openapitools/model/ClassModel.java
src/main/java/org/openapitools/model/Client.java
src/main/java/org/openapitools/model/Dog.java
src/main/java/org/openapitools/model/DogAllOf.java
src/main/java/org/openapitools/model/EnumArrays.java
src/main/java/org/openapitools/model/EnumClass.java
src/main/java/org/openapitools/model/EnumTest.java
src/main/java/org/openapitools/model/FileSchemaTestClass.java
src/main/java/org/openapitools/model/FormatTest.java
src/main/java/org/openapitools/model/HasOnlyReadOnly.java
src/main/java/org/openapitools/model/MapTest.java
src/main/java/org/openapitools/model/MixedPropertiesAndAdditionalPropertiesClass.java
src/main/java/org/openapitools/model/Model200Response.java
src/main/java/org/openapitools/model/ModelApiResponse.java
src/main/java/org/openapitools/model/ModelReturn.java
src/main/java/org/openapitools/model/Name.java
src/main/java/org/openapitools/model/NumberOnly.java
src/main/java/org/openapitools/model/Order.java
src/main/java/org/openapitools/model/OuterComposite.java
src/main/java/org/openapitools/model/OuterEnum.java
src/main/java/org/openapitools/model/Pet.java
src/main/java/org/openapitools/model/ReadOnlyFirst.java
src/main/java/org/openapitools/model/SpecialModelName.java
src/main/java/org/openapitools/model/Tag.java
src/main/java/org/openapitools/model/TypeHolderDefault.java
src/main/java/org/openapitools/model/TypeHolderExample.java
src/main/java/org/openapitools/model/User.java
src/main/java/org/openapitools/model/XmlItem.java
src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5.1.0-SNAPSHOT
18 changes: 18 additions & 0 deletions samples/server/petstore/springboot-no-pagination/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# OpenAPI generated server

Spring Boot Server


## Overview
This server was generated by the [OpenAPI Generator](https://openapi-generator.tech) project.
By using the [OpenAPI-Spec](https://openapis.org), you can easily generate a server stub.
This is an example of building a OpenAPI-enabled server in Java using the SpringBoot framework.

The underlying library integrating OpenAPI to SpringBoot is [springfox](https://github.com/springfox/springfox)

Start your server as a simple java application

You can view the api documentation in swagger-ui by pointing to
http://localhost:80/

Change default port value in application.properties
79 changes: 79 additions & 0 deletions samples/server/petstore/springboot-no-pagination/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.openapitools</groupId>
<artifactId>springboot-no-pagination</artifactId>
<packaging>jar</packaging>
<name>springboot-no-pagination</name>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<springfox-version>2.9.2</springfox-version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
</parent>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<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>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-version}</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>0.2.1</version>
</dependency>
<!-- Bean Validation API support -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.openapitools;

import com.fasterxml.jackson.databind.Module;
import org.openapitools.jackson.nullable.JsonNullableModule;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootApplication
@ComponentScan(basePackages = {"org.openapitools", "org.openapitools.api" , "org.openapitools.configuration"})
public class OpenAPI2SpringBoot implements CommandLineRunner {

@Override
public void run(String... arg0) throws Exception {
if (arg0.length > 0 && arg0[0].equals("exitcode")) {
throw new ExitException();
}
}

public static void main(String[] args) throws Exception {
new SpringApplication(OpenAPI2SpringBoot.class).run(args);
}

static class ExitException extends RuntimeException implements ExitCodeGenerator {
private static final long serialVersionUID = 1L;

@Override
public int getExitCode() {
return 10;
}

}

@Bean
public WebMvcConfigurer webConfigurer() {
return new WebMvcConfigurer() {
/*@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("*")
.allowedHeaders("Content-Type");
}*/
};
}

@Bean
public Module jsonNullableModule() {
return new JsonNullableModule();
}

}
Loading

0 comments on commit 0d1c88f

Please sign in to comment.