Swagger Parameters and Complex Types

你说的曾经没有我的故事 提交于 2019-12-24 00:45:35

问题


In the following Swagger definition, I need the parameter labelValue to be of type LabelValueObject, so that it will be validated and correctly deserialized. However, I can't figure out the syntax! How can that be done?

swagger: "2.0"

paths:
  /competition:
    post:
      parameters:
        - name: labelValue
          in: formData
          type: array
          items:
            type: string       ### this has to be a LabelValueObject ###
      responses:
        default:
          description: Error
          schema:
            $ref: "#/definitions/AnyResponse"

definitions:
  AnyResponse:
    properties:
      any:
        type: string
  LabelValueObject:
    properties:
      label:
        type: string
      value:
        type: string
    required:
      - label
      - value

回答1:


The only way to pass an object as a parameter is to put it in the body (in: body) and then define this object in schema (inline definition or reference to an predefined object with $ref). Here's a full example:

swagger: "2.0"

info:
  title: A dummy title
  version: 1.0.0

paths:
  /competition:
    post:
      parameters:
        - name: labelValue
          in: body
          schema:
            $ref: '#/definitions/LabelValueObject'
      responses:
        default:
          description: Error
          schema:
            $ref: "#/definitions/AnyResponse"

definitions:
  AnyResponse:
    properties:
      any:
        type: string
  LabelValueObject:
    properties:
      label:
        type: string
      value:
        type: string
    required:
      - label
      - value


来源:https://stackoverflow.com/questions/39170423/swagger-parameters-and-complex-types

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!