springdoc / springdoc-openapi

Library for OpenAPI 3 with spring-boot
https://springdoc.org
Apache License 2.0
3.3k stars 500 forks source link

`@RequestBody` not working for `@GetMapping` endpoints #2775

Closed ML-Marco closed 1 week ago

ML-Marco commented 1 week ago
@GetMapping("test")
public void getWithRequestBody(@RequestBody MyClass myClass) {
}

This results in a definition where myClass parameter is defined to appear "in": "query". Correct behavior would be a requestBody block with $ref schema. Neither org.springframework.web.bind.annotation.RequestBody nor io.swagger.v3.oas.annotations.parameters.RequestBody work.

Actual

"/test": {
      "get": {
        "tags": [
          "test-controller"
        ],
        "operationId": "TestController.getWithRequestBody",
        "parameters": [
          {
            "name": "myClass",
            "in": "query",
            "required": true,
            "schema": {
              "$ref": "#/components/schemas/MyClass"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    }

Expected part

"requestBody": {
  "content": {
    "application/json": {
      "schema": {
        "$ref": "#/components/schemas/MyClass"
      }
    }
  },
  "required": true
}

It would be nice if a fix could be backported to the 1.8.x release 🙏.

bnasslahsen commented 1 week ago

springdoc-openapi v1.8.0 is the latest Open Source release supporting Spring Boot 2.x and 1.x. An extended support for springdoc-openapi v1 project is now available for organizations that need support beyond 2023. For more details, feel free to reach out: sales@springdoc.org

You should move to OpenAPI 3.1 to Allow requestBody creation for GET see #1814 Or move to 2.6.0, which has better support

ML-Marco commented 1 week ago

@bnasslahsen I also tested it with 2.6.0. Same problem with OpenAPI 3.0. Do you plan to support only OpenAPI 3.1 with upcoming releases or is this maybe an oversight?

bnasslahsen commented 1 week ago

@ML-Marco,

Both are supported. But the @RequestBody for GET operations, is only allowed in OpenAPI 3.1 and not OpenAPI 3.0. This comes from the spec.

ML-Marco commented 1 week ago

You might be right. I'm not absolutely sure of it. The OpenApi 3.0.3 spec references RFC7231 here in the description of requestBody. In that document, it says:

A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.

So, while discouraged, I'd say it's technically allowed. What do you think?

bnasslahsen commented 1 week ago

@ML-Marco,

I am not refering to the RFC. It's not allowed in OpenAPI 3.0 spec. If you need it, you have to move to OpenAPI 3.1 spec.