Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to declare a date in an OpenAPI / Swagger-file?

What is the correct way to declare a date in a swagger-file object? I would think it is:

  startDate:     type: string     description: Start date     example: "2017-01-01"     format: date 

But I see a lot of declarations like these:

  startDate:     type: string     description: Start date     example: "2017-01-01"     format: date     pattern: "YYYY-MM-DD"     minLength: 0     maxLength: 10 

Thanks.

like image 308
Patrick Savalle Avatar asked Mar 20 '18 08:03

Patrick Savalle


People also ask

How do you write the date in Swagger?

String Formatsdate – full-date notation as defined by RFC 3339, section 5.6, for example, 2017-07-21. date-time – the date-time notation as defined by RFC 3339, section 5.6, for example, 2017-07-21T17:32:28Z.

How do you write Swagger files?

Head over to Swagger Inspector, and insert the end point of the resource you want to have documented. You can then navigate to the right panel from the History section of Swagger Inspector, and click "Create API definition" to create the OAS definition.

What is format in Swagger?

OpenAPI Specification (formerly Swagger Specification) is an API description format for REST APIs. An OpenAPI file allows you to describe your entire API, including: Available endpoints ( /users ) and operations on each endpoint ( GET /users , POST /users ) Operation parameters Input and output for each operation.

What are the formats supported by OpenAPI Swagger for their definitions?

Format. An OpenAPI document that conforms to the OpenAPI Specification is itself a JSON object, which may be represented either in JSON or YAML format.


2 Answers

The OpenAPI Specification says that you must use:

type: string format: date # or date-time

The patterns supported are defined in RFC 3339, section 5.6 (effectively ISO 8601) and examples are provided in section 5.8. So for date values should look like "2018-03-20" and for date-time, "2018-03-20T09:12:28Z". As such, when using date or date-time, the pattern is unnecessary and in fact, should be omitted.

If you need to support dates/times formatted in a way that differs to RFC 3339, you are not allowed to specify your parameter as format: date or format: date-time. Instead, you should specify format: string with an appropriate pattern.

Finally, note that a pattern of "YYYY-MM-DD" is invalid according to the specification: pattern must be a regular expression, not a placeholder or format string.

If you are violating any of the above rules to work around bugs in tools that generate UIs from OpenAPI specifications, you should strongly consider raising these bugs with that tool, rather than generating an invalid OpenAPI spec to work around this.

like image 188
Pascal Avatar answered Oct 14 '22 13:10

Pascal


A correct example of declaring date in an Open API swagger file:

    properties:       releaseDate:         type: date         pattern: /([0-9]{4})-(?:[0-9]{2})-([0-9]{2})/         example: "2019-05-17" 
like image 36
Filipe Bezerra de Sousa Avatar answered Oct 14 '22 12:10

Filipe Bezerra de Sousa