Search code examples
arraysswaggerdocumentationswagger-ui

Swagger array of strings without name


Currently I am trying to create a swagger file for my software. Now I would like to create a definition for a timeRange. My problem is that this array looks like this:

timeRange: {
  "2016-01-15T09:00:00.000Z", // this is the start date
  "2017-01-15T09:00:00.000Z" // this is the end date
}

How can I create an example value that works out of the box? It is an "array of strings" with a minimum of two.

"timeRange": {
  "type": "array",
  "items": {
    "type": "string",
    "example": "2017-01-15T09:00:00.000Z,2017-01-15T09:00:00.000Z"
  }
}

This generates an example like this:

"timeRange": [
  "2017-01-15T09:00:00.000Z,2017-01-15T09:00:00.000Z"
]

This example does not work, because it is an array and not an object. All together: How can I realize an example value that exists out of two different strings (without a name).

Hope you can help me!


Solution

  • timeRange: {
      "2016-01-15T09:00:00.000Z", // this is the start date
      "2017-01-15T09:00:00.000Z" // this is the end date
    }
    

    is not valid JSON – "timeRange" needs to be enclosed in quotes, and the object/array syntax should be different.

    If using the object syntax {}, the values need to be named properties:

    "timeRange": {
      "start_date": "2016-01-15T09:00:00.000Z",
      "end_date": "2017-01-15T09:00:00.000Z"
    }
    

    Otherwise timeRange needs to be an [] array:

    "timeRange": [
      "2016-01-15T09:00:00.000Z",
      "2017-01-15T09:00:00.000Z"
    ]
    


    In the first example ({} object), your Swagger would look as follows, with a separate example for each named property:

    "timeRange": {
      "type": "object",
      "properties": {
        "start_date": {
          "type": "string",
          "format": "date-time",
          "example": "2016-01-15T09:00:00.000Z"
        },
        "end_date": {
          "type": "string",
          "format": "date-time",
          "example": "2017-01-15T09:00:00.000Z"
        }
      },
      "required": ["start_date", "end_date"]
    }
    

    In case of an [] array, you can specify an array-level example that is a multi-item array:

    "timeRange": {
      "type": "array",
      "items": {
        "type": "string",
        "format": "date-time"
      },
      "example": [
        "2016-01-15T09:00:00.000Z",
        "2017-01-15T09:00:00.000Z"
      ]
    }