Search code examples
swaggerswagger-2.0swagger-editor

Create my own type in Swagger


I Have this YAML code of swagger and I need to create my own type (named MyOwnType).

If I use "MyOwnType" a compilation error occurs.

paths:
  /in/total:
    get:
      summary: My summary.
      description: My description.

      parameters:
        - name: total
          in: query
          description: Total.
          required: true
          type: MyOwnType # -- THIS LINE OCCURS ERROR --
          format: MyOwnType
      responses:
        201:
          description: Respose
          schema:
            $ref: '#/definitions/MyOwnType'

definitions:
  MyOwnType:
    properties:
      var:
        type: string
        description: data.

I created a definition "MyOwnType" and I can use like that: "$ref: '#/definitions/MyOwnType'" in schema.

But how can I use the "MyOwnType" definition on a parameter type?


Solution

  • A query parameter can not have a JSON Schema. If you want to have schema for your parameter you should change in of your parameter to body or formData and use schema key:

    swagger: '2.0'
    info:
      version: 0.0.0
      title: '<enter your title>'
    paths:
      /in/total:
        get:
          summary: My summary.
          description: My description.
    
          parameters:
            - name: total
              in: body
              description: Total.
              required: true
              schema:
                $ref: '#/definitions/MyOwnType'
          responses:
            201:
              description: Respose
              schema:
                $ref: '#/definitions/MyOwnType'
    
    definitions:
      MyOwnType:
        properties:
          var:
            type: string
            description: data.