Skip to content
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

Schema properties are ignored in method-level @RequestBody #1664

Closed
sgrimm opened this issue May 12, 2022 · 1 comment
Closed

Schema properties are ignored in method-level @RequestBody #1664

sgrimm opened this issue May 12, 2022 · 1 comment

Comments

@sgrimm
Copy link

sgrimm commented May 12, 2022

Describe the bug
Explicitly specifying request body properties by putting @Content(schemaProperties=...) in a method-level @RequestBody doesn't cause the properties to be included in the generated schema.

To Reproduce

  • Spring Boot 2.6.7
  • Springdoc-openapi 1.6.8
  • Modules: kotlin, security, ui (and their transitive dependencies)
@RestController
class ExampleController {
  @PostMapping("/upload", consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
  @RequestBody(content = [
        Content(
            schema = Schema(type = "object", requiredProperties = ["file"]),
            schemaProperties = [
                    SchemaProperty(name = "file", schema = Schema(type = "string", format = "binary"))],
            encoding = [Encoding(name = "file", contentType = "text/csv")])],
  )
  fun uploadCsv(request: HttpServletRequest): String {
    return "OK"
  }
}

Expected behavior
The above code should result in the following (leaving off the responses section for brevity):

  /upload:
    post:
      tags:
      - example-controller
      operationId: uploadCsv
      requestBody:
        content:
          multipart/form-data:
            schema:
              required:
              - file
              type: object
              properties:
                file:
                  type: string
                  format: binary
            encoding:
              file:
                contentType: text/csv

The actual output is missing the properties section of the schema:

  /upload:
    post:
      tags:
      - example-controller
      operationId: uploadCsv
      requestBody:
        content:
          multipart/form-data:
            schema:
              required:
              - file
              type: object
            encoding:
              file:
                contentType: text/csv

Additional context
I'm not using a FilePart or a RequestPart parameter here because the uploaded files are machine-generated output and can be multiple gigabytes in size. Spring (or Jetty) reads the entire request body from the client before calling the controller method if you declare one of those parameters. Instead I'm using Apache FileUpload to stream the contents of the file into my application code as it arrives from the client, but I want the API schema to be identical to what it would look like if I were using FilePart.

@Frettman
Copy link

Came here for the same thing, only that my media type is application/x-www-form-urlencoded.

I'm basically trying to migrate from JAX-WS to Spring and need to translate a @FormParam annotation. While I don't care that @RequestParam also accepts query parameters, I would like it to be a form parameter in the OpenAPI doc and Swagger UI.

It seems unnecessarily verbose, but this is how I'm trying to replace it:

  @Operation(
      requestBody = @RequestBody(
          content = @Content(
              mediaType = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
              schema = @Schema(type = "object", requiredProperties = {"formParam"}),
              schemaProperties = @SchemaProperty(
                  name = "formParam",
                  schema = @Schema(type = "string")))))
  public ResponseEntity<String> postUrl(
      @RequestParam(name = "formParam", required = false) String formParam)
  { ... }

Although I can't really be sure this would work as I hope it will, even when schemaProperties is supported.

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