Skip to content

How to specify response schema for Page (org.springframework.data.domain.Page) of objects #3723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ArtemZubenko opened this issue Oct 27, 2020 · 4 comments

Comments

@ArtemZubenko
Copy link

Hello swagger team,

I have a simple method which returns Page (org.springframework.data.domain.Page) of user objects. How can I specify schema for such response? Both, @Schema(implementation = Page.class)) and @Schema(implementation = User.class)), produce incorrect result. Please refer to the example below.

@GetMapping("/users")
@Operation(
        description = "Get users",
        summary = "List all existing users",
        responses = {
                @ApiResponse(
                        responseCode = "200",
                        content = @Content(
                                mediaType = "application/json",
                                schema = @Schema(implementation = ???)) //<-- How to specify schema for Page<User>
                )
        }
)
public Page<User> getAll(@RequestParam Integer page, @RequestParam Integer size, @RequestParam String sortField, @RequestParam String sortDirection) {
    return userDelegate.getAll(page, size, sortField, sortDirection);
}

Thank you. Looking forward for your feedback.

Cheers,

Artem

@ArtemZubenko
Copy link
Author

Hi everyone

If you have the same issue with Page or in general with generics, the below workaround seems to work.


@RestController
public class UserController {

    private UserRepo userRepo;

    @GetMapping("/users")
    @Operation(
            description = "Get users",
            summary = "List all existing users",
            responses = {
                    @ApiResponse(
                            responseCode = "200",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(implementation = UserPage.class)) //set wrapper class as schema
                    )
            }
    )
    public UserPage getAll(@RequestParam Integer page, @RequestParam Integer size, @RequestParam String sortField, @RequestParam String sortDirection) {
        Pageable pageable = PageRequest.of(page, size, Sort.by(new Sort.Order(Sort.Direction.valueOf(sortDirection.toUpperCase()), sortField)));
        Page<User> aPage = this.userDelegate.getAll(pageable); // Returns page
        return new UserPage(aPage.getContent(), pageable, aPage.getContent().size()); // Creates and returns instance of the wrapper class
    }
}

//Wrapper class
class UserPage extends PageImpl<User> {
    public UserPage(List<User> content, Pageable pageable, long total) {
        super(content, pageable, total);
    }
}

Cheers,

Artem

@goncalvesfabio
Copy link

goncalvesfabio commented Feb 4, 2021

Thank you very much for sharing the solution!

@frantuma
Copy link
Member

#4129 addresses this by introducing field @ApiResponse.useReturnTypeSchema, which allows to mark a response to use the return type for its schema. Please see comment in PR for an example

@RomainBitard
Copy link

RomainBitard commented May 10, 2024

#4129 addresses this by introducing field @ApiResponse.useReturnTypeSchema, which allows to mark a response to use the
return type for its schema. Please see comment in PR for an example

How would you use this ? I tried on my endpoint without success but I may be doing something wrong
The answer above is working perfectly thought

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants