Reputation: 678
I am using swagger 2.0 and I have an endpoint which is using form-data as the payload for the request, I am using the same form-data body request in several places and I don't want to write it again and again (duplicate it) and I don't know how to model it inside my yaml file (I do know how to model object when it is part of the request body as payload).
This is the form-data payload I am trying to model:
I looked at swagger's documentation: https://swagger.io/docs/specification/2-0/file-upload/
and tried to do the same but I got errors as follow:
I don't want to duplicate the same values (if I do so it will work but the solution won't be elegant).
Upvotes: 1
Views: 4577
Reputation: 97540
In OpenAPI 2.0, you can only reuse the definitions of individual form fields/parameters. There's no way to reuse a set of fields/parameters.
swagger: '2.0'
parameters:
name:
in: formData
name: name
type: string
upfile1:
in: formData
name: upfile1
type: file
...
paths:
/foo:
post:
consumes:
- multipart/form-data
parameters:
- $ref: '#/parameters/name'
- $ref: '#/parameters/upfile1'
- ...
/bar:
post:
consumes:
- multipart/form-data
parameters:
- $ref: '#/parameters/name'
- $ref: '#/parameters/upfile1'
- ...
To reuse the entire request body definition, you need OpenAPI 3.0. In OAS 3, request bodies can be defined in the global components/requestBodies
section and referenced using $ref
:
openapi: 3.0.0
paths:
/foo:
post:
requestBody:
$ref: '#/components/requestBodies/statusMode'
responses:
...
/bar:
post:
requestBody:
$ref: '#/components/requestBodies/statusMode'
responses:
...
components:
requestBodies:
statusMode:
required: true
content:
multipart/form-data:
schema:
type: object
# The properties correspond to individual form fields
properties:
name:
type: string
upfile1:
type: string
format: binary
Upvotes: 2