Skip to content

generic type information for response schema #3851

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
judos opened this issue Jan 7, 2021 · 2 comments
Closed

generic type information for response schema #3851

judos opened this issue Jan 7, 2021 · 2 comments

Comments

@judos
Copy link

judos commented Jan 7, 2021

How do I provide type information to a response schema?

When a service method has the following response type: ResponseEntity<PageDto<CustomerDto>> swagger correctly displays all type information I want to see.

However when I have multiple possible responses I would like to provide the same details via annotation and there I don't know how to provide the type information of the PageDto:

@ApiResponses(value = [
  ApiResponse(responseCode = "200", content = [Content(
    schema = Schema(implementation = PageDto::class))]),
  ApiResponse(responseCode = "400",
    content = [Content(schema = Schema(implementation = ErrorDto::class))])
])
fun getAll(): ResponseEntity<Any> {
  if (...) {
    return ResponseEntity.badRequest().body(ErrorDto("ERROR KEY"))
  }
  return ResponseEntity.ok(PageDto(result))
}

Swagger obviously doesnt know what type T the PageDto class contains. How do I provide this information such that the documentation is generated with better details?

Thanks for your help!

@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

@judos
Copy link
Author

judos commented Mar 11, 2022

@frantuma
In my opinion this can't work for this case. The specified return type is ResponseEntity<Any> and this is not helpful in the documentation. The return type also can't be different since the method actually returns either ErrorDto or PageDto.

Did I understand this wrong? useReturnTypeSchema can't magically find out what i'm doing inside the method also it wouldn't know which Type to use.

So again: How can I specify the generic Type of a Dto returned inside the Annotation (ApiResponse/Content/Schema)?

@judos judos changed the title type information for response schema generic type information for response schema Mar 11, 2022
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

2 participants