scottie1984 / swagger-ui-express

Adds middleware to your express app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app.
MIT License
1.42k stars 225 forks source link

Question about yaml syntax [help!] #241

Closed pravosleva closed 3 years ago

pravosleva commented 3 years ago

Hello. How can I describe this case in yaml syntax? (The most interested color_choises field)

// express widdleware

const toClient = [
  {
    ok: false,
    code: 'imei_invalid',
    message: 'Неверный IMEI: Неверный формат IMEI',
    extra: null,
  },
  {
    ok: true,
    imei: '868030034494621',
    phone: {
      vendor: 'Xiaomi',
      model: 'MI 6',
      memory: '',
      color: '',
      memory_choices: ['128 GB', '64 GB'],
      color_choices: {
        '128 GB': ['white', 'ceramic_black', 'blue', 'black'],
        '64 GB': ['black', 'blue', 'ceramic_black', 'white'],
      },
      type: 'mobile_phone',
    },
    id: 777,
    photo: 'models/samsung/galaxy-s20-ultra/color_cosmic_black_01.jpg',
  },
]

module.exports = async (req, res) => {
  const toBeOrNotToBe = getRandomInteger(0, 1)

  res.status(200).send(toClient[toBeOrNotToBe])
}

I tried this, but does not work:

# swagger.yaml
# etc.

paths:
  /imei:
    post:
      consumes:
        - application/json
      parameters:
        - in: body
          required: true
          schema:
            $ref: '#/definitions/IMEIVerifyRequestParams'
      summary: 'Returns specific OT data'
      description: 'Проверить IMEI'
      produces:
        - application/json
      responses:
        200:
          description: 'OK'
          schema:
            info: 'Success response sample'
            type: object
            properties:
              ok: boolean
              imei:
                type: string
              phone:
                vendor: string
                model: string
                memory: string
                color: string
                memory_choices: array
                  items:
                    type: string
                color_choices:
                  $ref: '#/definitions/ColorChoices'
            example:
              ok: true
              imei: '868030034494621'
              phone:
                vendor: Xiaomi
                model: 'MI 6'
                memory: ''
                color: ''
                memory_choices:
                  - '128 GB'
                  - '64 GB'
                color_choices:
                  '128 GB':
                    - white
                    - ceramic_black
                    - blue
                    - black
                  '64 GB':
                    - white
                    - ceramic_black
                    - blue
                    - black
                type: mobile_phone
definitions:
  IMEIVerifyRequestParams:
    type: object
    properties:
      IMEI:
        required: true
        type: string
        description: 'Your device IMEI'
scottie1984 commented 3 years ago

I would look at some json to yaml converter to help perhaps. Not something I am too versed in unfortunately

pravosleva commented 3 years ago

@scottie1984 thanx for good idea.