Skip to content

@RequestBody not working for @GetMapping endpoints #2775

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
ML-Marco opened this issue Nov 7, 2024 · 5 comments
Closed

@RequestBody not working for @GetMapping endpoints #2775

ML-Marco opened this issue Nov 7, 2024 · 5 comments
Labels
question Further information is requested

Comments

@ML-Marco
Copy link
Contributor

ML-Marco commented Nov 7, 2024

@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 🙏.

@ML-Marco ML-Marco changed the title @RequestBody not working for @GetMapping requests @RequestBody not working for @GetMapping endpoints Nov 7, 2024
@bnasslahsen
Copy link
Collaborator

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: [email protected]

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

@bnasslahsen bnasslahsen added the question Further information is requested label Nov 7, 2024
@ML-Marco
Copy link
Contributor Author

ML-Marco commented Nov 7, 2024

@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
Copy link
Collaborator

bnasslahsen commented Nov 7, 2024

@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
Copy link
Contributor Author

ML-Marco commented Nov 7, 2024

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
Copy link
Collaborator

bnasslahsen commented Nov 7, 2024

@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.

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

No branches or pull requests

2 participants