How to make a field in a definition required for some operations and not others

前端 未结 3 1448
执笔经年
执笔经年 2020-12-10 10:57

I\'m writing my swagger definition in yaml. Say I have a definition that looks something like this.

paths:
  /payloads:
    post:
      summary: create a pay         


        
3条回答
  •  猫巷女王i
    2020-12-10 11:26

    I'm in the same problem rigth now. Im my case, I tried to override requerid block of model. But didn't work. =[ Then, I remembered we can add new properties of a $ref. So it works if you define the requeried fields on schema for each method. Some thing like this (Focus on required block for each ref):

    swagger: '2.0'
    info:
      title: API
      version: 0.0.1
    host: api.help.v1
    basePath: /help
    schemes:
    - https
    definitions:
      MyModel:
        description: 'Exemplo'
        type: object
        properties:
          field_1:
            type: string
          field_2:
            type: string
          field_3:
            type: string
    paths:
      '/helps':
        post:
          description: ''
          summary: ''
          parameters:
            - in: body
              name: payload
              schema:
                type: object
                allOf:
                - $ref: '#/definitions/MyModel'
                required: 
                - field_1
          responses:
            '200':
              description: OK
            '400':
              description: N_OK
        put:
          description: ''
          summary: ''
          parameters:
            - in: body
              name: payload
              schema:
                type: object
                allOf: 
                - $ref: '#/definitions/MyModel'
                required: 
                - field_2
          responses:
            '200':
              description: OK
            '400':
              description: N_OK
    
    externalDocs:
      description: Find out more about Swagger
      url: 'http://swagger.io'
    

    Oh! On swagger editor show that only by model view:

    I didn't try yet. But should be possible to define Model like that.

提交回复
热议问题