Skip to content

Enhance SpringDoc documentation for file upload API configuration #77

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
YangSiJun528 opened this issue Apr 12, 2024 · 2 comments
Closed
Labels
documentation Improvements or additions to documentation

Comments

@YangSiJun528
Copy link

I'm using springdoc-openapi-starter-webmvc-ui:2.5.0 and wanted to test a file upload API using SpringDoc's Swagger UI functionality. However, I encountered an issue with the Swagger UI not displaying correctly.
(and I'm using Spring Boot 3.2.3, but I don't think that has anything to do with this issue.)

After examining the Spring WebMVC demo code, I discovered that specific annotations are necessary for it works. Unfortunately, this crucial information wasn't clearly outlined in the official SpringDoc documentation, making it challenging to resolve the issue.

The official documentation mentions that SpringDoc supports @RequestPart and MultipartFile, but lacks detailed guidance on configuring file upload endpoints.

I suggest improving the SpringDoc documentation to provide thorough guidelines for displaying file upload APIs correctly in Swagger UI:

  1. Clearly define parameters using either @RequestPart or @RequestParam.
  2. Ensure that the produces and consumes attributes of the @PostMapping annotation are properly configured to specify media types.

Below is an example demonstrating both working and non-working scenarios:

@RestController
@RequestMapping("/demo")
public class DemoController {

 private final FakeService fakeService;

 public DemoController(FakeService fakeService) {
 this.fakeService = fakeService;
 }

 @PostMapping(value = "/work-well", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
 public ResponseEntity<Void> workWell(@RequestPart MultipartFile file, @RequestParam String name) {
 // This case works correctly
 return ResponseEntity.created(this.fakeService.uploadImage(file, name)).build();
 }

 @PostMapping(value = "/bad-1", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
 public ResponseEntity<Void> bad1(MultipartFile file, String name) {
 // Swagger UI shows file as "string($binary)"
 // Sending a request via Swagger UI results in a "Value must be a string" validation error.
 return ResponseEntity.created(this.fakeService.uploadImage(file, name)).build();
 }

 @PostMapping(value = "/bad-2")
 public ResponseEntity<Void> bad2(@RequestPart MultipartFile file, @RequestParam String name) {
 // Swagger UI shows file as "application/json" type Request body
 return ResponseEntity.created(this.fakeService.uploadImage(file, name)).build();
 }

 @PostMapping(value = "/bad-3")
 public ResponseEntity<Void> bad3(MultipartFile file, String name) {
 // Same result as bad1
 return ResponseEntity.created(this.fakeService.uploadImage(file, name)).build();
 }
}

I hope this helps in effectively communicating the issue, and I encourage the SpringDoc team to consider enhancing the documentation to address this concern.

@bnasslahsen
Copy link
Contributor

@YangSiJun528,

Feel free to contribute to the documentation here: https://github.com/springdoc/springdoc.github.io

@bnasslahsen bnasslahsen added the documentation Improvements or additions to documentation label Jun 15, 2024
@bnasslahsen bnasslahsen transferred this issue from springdoc/springdoc-openapi Jun 15, 2024
bnasslahsen added a commit that referenced this issue Sep 27, 2024
Add documentation about file upload for springdoc 2
@bnasslahsen
Copy link
Contributor

docs updated

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

No branches or pull requests

2 participants