Write a document with a charge that consumes several types of content, for example. application / json AND application / x-www-form-urlencoded (no duplication)

I am looking for an elegant way to define api that can consume JSON data as well as form data. The following snippet works, but it's not elegant and requires all the ugly code in the backend. Is there a better way to define this?

What works right now:

paths:
  /pets:
    post:
      consumes:
      - application/x-www-form-urlencoded
      - application/json
      parameters:
      - name: nameFormData
        in: formData
        description: Updated name of the pet
        required: false
        type: string
      - name: nameJSON
        in: body
        description: Updated name of the pet
        required: false
        type: string

The basic idea of โ€‹โ€‹how I want to work:

paths:
  /pets:
    post:
      consumes:
      - application/x-www-form-urlencoded
      - application/json
      parameters:
      - name: name
        in: 
        - formData
        - body
        description: Updated name of the pet
        required: true
        type: string

But this does not work, because the value inshould be a string, not an array.

Any good ideas?

+4
source share
1 answer

Openapi 2.0

OpenAPI 2.0 . , , JSON, . - - JSON - .

OpenAPI 3.0

OpenAPI 3.0. requestBody.content.<media-type> , , application/json application/x-www-form-urlencoded, . .

openapi: 3.0.0
...
paths:
  /pets:
    post:
      requestBody:
        required: true
        content:

          application/json:
            schema:
              $ref: '#/components/schemas/Pet'

          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/Pet'

       responses:
         '200':
            description: OK

components:
  schemas:
    Pet:
      type: object
      properties:
        name:
          type: string
          description: Updated name of the pet
      required:
        - name

:

+4

Source: https://habr.com/ru/post/1670004/


All Articles