Swagger Parameters and Complex Types

2019-07-15 08:55发布

问题:

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


标签: swagger