Creating a Swagger-specification JSON file
If you need to create a new Swagger specification JSON file, you can start with the template provided here.
About this task
The template includes boilerplate elements:
Field name | Type | |
---|---|---|
openapi | string | REQUIRED. This string must be the semantic version number of the OpenAPI Specification version that the OpenAPl document uses. The field should be used by tooling specifications and clients to interpret the OpenAPI document. This is not related to the API string. |
info | Info Object | REQUIRED. Provides metadata about the API. The metadata MAY be used by tooling as required. |
servers | Server Object | An array of Server Objects, which provide connectivity information to a target server. If the property is not provided, or is an empty array, the default value is a Server Object with a URL value of /. |
paths | Paths Object | REQUIRED. The available paths and operations for the API. |
components | Components Object | An element to hold various schemas for the specification. |
security | Security Requirements Object | A declaration of which security mechanisms can be used across the API. The list of values includes alternative security requirement Objects that can be used. Only one of the security requirement Objects need to be satisfied to authorize a request. Individual operations can override this definition. To make security optional, an empty security requirement can be included in the array. |
tags | Tag Object | A list of tags used by the specification with additional metadata. The order of the tags can be used to reflect on their order by the parsing tools, Not all tags that are used by the Operation Object must be declared. The tags that are not declared may be organized randomly or based on the tools' logic. Each tag name in the list must be unique. |
externalDocs | External Documentation Object | Additional external documentation. |
You need to determine several things before creating a new JSON file.
- What are the parameters you will use (described here: http://swagger.io/specification/#parameterObject)?
- What does the method you creating produce? Draw up a list of data types that can be returned from the operation.
- What does the method consume? Draw up a list of data types that are consumed by the operation.
- What are the valid responses (described here: http://swagger.io/specification/#responseObject)?
To add a new schema, add a schema object to the components object.
Key points to remember
- The schema name.
- The structure of the schema object. For guidance, see the Schema Object documentation in the Swagger specification. Note that a schema object can contain another schema object, by way of reference to it. For example, see the address field. Consider this simple model as a further example of a schema object
Adding headers to responses
Headers that are returned with responses can be described by adding a headers property for each responses object.
For example, adding a header named Location to the response of a POST request where a resource is created:
For more information, see the OpenAPI specification.
Procedure
- Navigate to the Swagger editor.
- Select the Insert drop-down menu and select Add Path Items to add the path.
- Add an operation by selecting Add Operation.
- Add a response by selecting Add Example Response.