How to annotate a field as deprecated in OpenAPI (Swagger) 2.0?

The possibility to mark schemas and schema properties as deprecated was added in OpenAPI 3.0: openapi: 3.0.1 … components: schemas: Service: type: object properties: location: type: string description: Location of the service example: ‘400 Street name, City State postcode, Country’ deprecated: true # <——— If you use OpenAPI 2.0 (Swagger 2.0), the only thing you … Read more

Swagger; specify two responses with same code based on optional parameter

OpenAPI 2.0 OAS2 does not support multiple response schemas per status code. You can only have a single schema, for example, a free-form object (type: object without properties). OpenAPI 3.x In OAS3 you can use oneOf to define multiple possible request bodies or response bodies for the same operation: openapi: 3.0.0 … paths: /path: get: … Read more

How to post files in Swagger (OpenAPI)?

OpenAPI Specification 2.0 In Swagger 2.0 (OpenAPI Specification 2.0), use a form parameter (in: formData) with the type set to file. Additionally, the operation’s consumes must be multipart/form-data. consumes: – multipart/form-data parameters: – name: file in: formData # <—– description: The uploaded file data required: true type: file # <—– OpenAPI Specification 3.0 In OpenAPI … Read more

How to define an enum in OpenAPI (Swagger)?

“enum” works like this in OpenAPI 2.0: { “in”: “query”, “name”: “sample”, “description”: “a sample parameter with an enum value”, “type”: “string”, “enum”: [ “1”, “2”], “required”: true } and in OpenAPI 3.0: { “in”: “query”, “name”: “sample”, “description”: “a sample parameter with an enum value”, “schema”: { “type”: “string”, “enum”: [ “1”, “2”] }, … Read more

How to define UUID property in JSON Schema and Open API (OAS)

There’s no built-in type for UUID, but the OpenAPI Specification suggests using type: string format: uuid From the Data Types section (emphasis mine): Primitives have an optional modifier property: format. OAS uses several known formats to define in fine detail the data type being used. However, to support documentation needs, the format property is an … Read more

Swagger/OpenAPI mock server

An easy way to create simple mock from an OpenAPI (fka Swagger) spec without code is to use a tool call prism available at http://github.com/stoplightio/prism written in Typescript. This command line is all you need: ./prism run –mock –list –spec <your swagger spec file> The mock server will return a dynamic response based on the … Read more

Swagger/OpenAPI – use $ref to pass a reusable defined parameter

This feature already exists in Swagger 2.0. The linked ticket talks about some specific mechanics of it which doesn’t affect the functionality of this feature. At the top level object (referred to as the Swagger Object), there’s a parameters property where you can define reusable parameters. You can give the parameter any name, and refer … Read more

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

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 … Read more