Basic definition of RESTful Web Service API specs using OpenAPI Specification

I showed you how to define RESTful Web Service API specs using RAML. Another way to do this is to use the OpenAPI Specification. How is it in detail? In this tutorial, we will learn together how to define RESTful Web Service API specs using OpenAPI Specification.

We will use YAML or JSON file to define API specs with OpenAPI Specification. Below is the content of the YAML file that defines the API specs in the tutorial Introducing about RAML using the OpenAPI Specification:

As you can see, similar to defining API specs with RAML, at the top of this YAML file we will define some overview information about our API specs. We use the openapi field to define the OpenAPI Specification version we will use to define the API specs, information about the API specs including its purpose (field info.title), the version of this API specs (info.version). The base URL will be defined using the servers.url field.

Request URLs will be defined with section paths.

We cannot define request URLs with the same starting value in the extends type like in RAML. We can only aggregate requests with different HTTP methods but the same request URL. In the above example, request URL “/students/{id}” is defined with GET and DELETE method, we can merge these 2 request URLs together.

We can use the field operationId to identify each request URL. The value of this field is unique within API specs for each request URL.

Similar to RAML, we can also define response for each response status code, each response can have a different data type. In my example, it is application/json.

To define parameters for URL requests, we will use the parameters section in each request. The parameters can be path parameters, query parameters or in headers, cookies. We use the in field of each parameter to declare this.

OpenAPI also supports various data types such as RAML. You can check out more here! In the above example, we can specify the response body data type for request URLs with the Schema object by declaring the components.schemas section as follows:

then declare these Schema objects in the response body of each request as follows:

As you can see, we use the keyword “$ref” with the path to the object we want to use, usually starting with “components.schemas”, to declare the data type of the object type.

Defining the API specs before starting to implement the API will help us agree on the API contract, the parties involved only need to follow this API contract to use it, minimizing a lot of possible risks. Think about it before you start implementing RESTful Web Service APIs!

Add Comment