Song
Song

Reputation: 671

How to vary requestBody type based on a path parameter in OpenAPI 3.0?

In OpenAPI 2.0, in: body and in: formData parameters cannot exist together for the same operation, according to Swagger send body and formData parameter and OpenAPI 2.0 Specification. That makes sense.

But I'm using OpenAPI 3.0 and I wonder if there is a way to switch requestBody according to a path parameter? When store is path1, requestBody should use content with application/json; when store is path2, requestBody should use content with multipart/form-data.

  /customs/{store}:
    post:
      description: Customs server calls Nomad to receive the filing result of one
        order
      operationId: post_customs_callback
      parameters:
      - description: ID of the store.
        explode: true
        in: path
        name: store
        required: true
        schema:
          type: string
        style: simple
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              properties: # Request parts
                openReq:
                  type: string

#          application/json:
#            schema:
#              type: string
        description: Order details

Upvotes: 4

Views: 2269

Answers (1)

Helen
Helen

Reputation: 98102

OpenAPI Specification does not have a way to vary request/response body based on specific parameter values. However, in your scenario you can use two non-parameterized paths instead – /customs/path1 for JSON requests and /customs/path2 for multipart requests.

openapi: 3.0.0
...

paths:
  /customs/path1:
    post:
      ...
      requestBody:
        required: true
        content:
          application/json:
            schema:
              ...

  /customs/path2:
    post:
      ...
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              ...

Upvotes: 5

Related Questions