OpenAPi 3.0.3 - can I use two POST methods for the same path - each with its own request body?

Originally posted by Nixol on November 15, 2020

can I use two POST methods for the same path with each operation having its own request body? If it is not possible, how cab I achieve this? I use OpenApi 3.0.3

Originally posted by HKosova on November 16, 2020

OpenAPI only allows one POST/GET/etc. operation per path, but you can define multiple possible request bodies by using oneOf:

paths:
  /something:
    post:
      operationId: createSomething
      requestBody:
        required: true
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/Body1'
                - $ref: '#/components/schemas/Body2'

Originally posted by Nixol on November 16, 2020

Thanks HKosova for your explanation.

Could you also assist to answer the following followup questions:

  1. can each of the requests have a different tag? eg. Body1 has “TagA” and body2 has “TagB”
  2. can each of the requests have a corresponding response? e.g. if Body1 is used, then the response will be Response1, if Body2 is used then the response will be Response2.

Originally posted by HKosova on November 16, 2020

  1. No.

  2. No. You can use oneOf to define the possible response schemas, but OpenAPI doesn’t support request/response correlation.

Here’s a related feature request you can track:

Support an operation to have multiple specifications per path (e.g. multiple POST operation per path)

Originally posted by Nixol on February 14, 2021

Is there any plans to include the previously mentioned scenarios in future releases? It will be great if this can be done. Thanks

Originally posted by HKosova on February 14, 2021

Hi Nixol,

I agree this can be useful. The best way to show your interest is to upvote the existing feature request in the OpenAPI Specification repository and/or add a comment there with your use case:

https://github.com/OAI/OpenAPI-Specification/issues/182