Create complex types (definitions) in Swagger

Computered picture Computered · Sep 10, 2015 · Viewed 10.3k times · Source

I created a definition called Product and another called Text (see code).

On parameters of paths I can not use the type Text created in definitions. On the definition Product I have a property called message and I want that property to be the type Text too.

(...)

paths:
  /products:
    get:
      summary: Product Types
      description: |
        Description text
      parameters:
        - name: latitude
          in: query
          description: Latitude component of location.
          required: true
          ### The type Text was not found here
          type: Text ### The type Text was not found here
(...)

definitions:

  Product:
    properties:
      message:
        ### The type Text was not found here
        type: Text ### Compilation Error in this line ####
      name:
        type: string
        description: Data description.


  Text:
    properties:
      code:
        type: string

But this error occurs:

Swagger Error: Data does not match any schemas from 'anyOf'.

How can I reference the type Text on the type Product?

Answer

William Cheng picture William Cheng · Sep 11, 2015

Please use $ref instead. Here is an example

type: object
required:
- name
properties:
  name:
    type: string
  address:
    $ref: '#/definitions/Address'
  age:
    type: integer
    format: int32
    minimum: 0

Ref: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#simple-model