Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I define a property type as being a list (list, set, array, collection) of string in my YAML Swagger definition

I am writing a swagger definition file for an API. The API is a for a GET request

/path/to/my/api:
  get:
    summary: My Custom API
    description: |
      Gets a List of FooBar IDs
    produces:
      - application/json
    tags:
      - FooBar
    responses:
      "200":
        description: successful operation
        schema:
          $ref: "#/definitions/MyCustomType"         

...

MyCustomType:
  type: object
  properties: 
    myCustomObject
      type: ??? # list of string?
like image 512
Vihung Avatar asked Jul 24 '16 15:07

Vihung


2 Answers

For a list of strings, you can describe as follows:

      type: array
      items:
        type: string

Ref: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#schemaObject

Example:

  • https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml#L93-L100 (OpenAPI v2)
  • https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml#L71-L78 (OpenAPI v3)
like image 77
William Cheng Avatar answered Sep 30 '22 19:09

William Cheng


None of these comments really seem to answer the question -- How do you define a SET of items in OpenAPI/Swagger?

  • An array of strings is not the same as a SET
  • An array of unique strings is not the same as SET

A set is essentially an array of unrepeated enum values.

Given possible string values of facebook, pinterest, twitter, a field named share_type could have values containing one or more of them, ex:

Valid

facebook twitter facebook, twitter facebook, twitter, pinterest

Invalid Items

instagram facebook, instagram pinterest, pinterest

Another important distinction is that the values cannot be repeated. This is where the uniqueItems directive can be helpful.

So given the example above, here is what that OpenAPI spec might look like:

share_type:
  type: array
  uniqueItems: true
  items:
    type: string
    enum:
      - facebook
      - pinterest
      - twitter
like image 22
Nick Poulos Avatar answered Sep 30 '22 20:09

Nick Poulos