swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.93k stars 6.03k forks source link

[Spring] generate paginated resources #3353

Open cbornet opened 8 years ago

cbornet commented 8 years ago

To follow the discussion from #3133 The idea is to generate Page/Pageable if the operation has a "x-spring-paginated" vendor-extension.

I just tried

    public ResponseEntity<List<Pet>> findPetsByTags(@ApiParam(value = "Tags to filter by", required = true) @RequestParam(value = "tags", required = true) List<String> tags, Pageable pageable) {
        ArrayList<Pet> petList = new ArrayList<Pet>();
        Page<Pet> page = new PageImpl(petList, pageable, 0);
        // do some magic!
        return new ResponseEntity<List<Pet>>(page.getContent(), HttpStatus.OK);
    }

in spring-boot after including spring-data-commons and didn't have any issue. I didn't have to configure any datasource.

cbornet commented 8 years ago

@diyfr

diyfr commented 8 years ago

Generate code works [https://github.com/diyfr/swagger-codegen/commit/e908589f2b8cda673e3c85c42ac309916ab41a6f](first commit x-spring-pageable). What dependency used for your test ? I used spring-boot-starter-data-jpa , perhaps this is the problem?

EDIT 2: add to application propeties

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration

instead of

@SpringBootApplication(exclude = { org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class,
        org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class})
diyfr commented 8 years ago

https://github.com/swagger-api/swagger-codegen/pull/3357

cbornet commented 8 years ago

@diyfr Don't use spring-boot-starter-data-jpa, use only spring-data-commons

cbornet commented 8 years ago

As for naming I prefer "x-spring-paginated" than "x-spring-pageable"

cbornet commented 8 years ago

Also I tested the the plugin for Pageable parameters and it worked great. I find it cleaner than @ApiImplicitParam everywhere in the code.

diyfr commented 8 years ago

@cbornet i use x-spring-pageable because is the know spring component. And plugin need to rebuild springfox or can it in springboot project ?

cbornet commented 8 years ago

No you can just include the class in your project and a @Bean in a @Configuration to activate it.

cbornet commented 8 years ago

Yes Pageable is the name of the spring-data component used to implement pagination (you also need Page) and it doesn't describe the nature of the operation which is that it is "paginated using spring pagination"

diyfr commented 8 years ago

@cbornet ok for x-spring-paginated. I do not know if I'll have time for the plug-in today