paint-brush
OpenAPI Specification v. 3+ Introductionby@go-nikita-jos
267 reads

OpenAPI Specification v. 3+ Introduction

by Nikita JosAugust 20th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The OpenAPI Specification (OAS) defines a standard, language-agnostic interface to RESTful APIs. This allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection. The solution is mentioned both in official Swagger and Spring doc link https://springdoc.org/faq.com/swagger-v3-and-microservices-776df11c. This was solved using @CrossOriginin controller using @SecurityRequirement (name = “bearer-key”)

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - OpenAPI Specification v. 3+ Introduction
Nikita Jos HackerNoon profile picture

Mental Checklist

Background

In the heat of coding and meeting strict deadlines, most of us as developers forget to think about documentation. In my case my manager popped the question on the day of the demo. There was utter panic when he shot down our out of the box swagger UI. What followed was a learning experience for me. I do not consider myself an expert at coding, yet (wink wink). I love what I do and have too much passion for coding. This chiding incident led me to deep dive into documentation standards in the industry. Since we had already built the microservices functionality. Now, we had to engineer the swagger UI to look like a professional API document.

Introduction

The OpenAPI Specification (OAS) defines a standard, language-agnostic interface to RESTful APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection. When properly defined, a consumer can understand and interact with the remote service with a minimal amount of implementation logic.

An OpenAPI definition can then be used by documentation generation tools to display the API, code generation tools to generate servers and clients in various programming languages, testing tools, and many other use cases. (Reference)

A quick guide to swagger annotations

@Tag @Operation @ApiResponses @Schema

I would also encourage you to explore io.swagger.v3.oas.annotations source package.

How can I pass JWT token in swagger

What I learnt is that one cannot pass JWT token in the swagger UI header. <LOST?> This is a tricky matter. On trying to google it you will be lost in a vicious forums of GIT issues. Do not fret. The solution is mentioned both in official Swagger and Spring doc link https://springdoc.org/faq.html

You want to enable a Authorize button on your Swagger UI

Steps to enable Authorize button

Step 1 : At Controller add @SecurityRequirement

@Operation(summary = “Summary”, description = “<b>Implementation Notes</b> <br/>Long description”, tags = {“reports” }, security = { @SecurityRequirement(name = “bearer-key”) })

Step 2 : At Open API configuration

List<Server> serverList = new ArrayList<Server>();
Server qaServer = new Server();
qaServer.setDescription(“INT”);
qaServer.setUrl(“URL-int");
Server stgServer = new Server();
stgServer.setDescription(“STG”);
stgServer.setUrl(“URL-stg");
serverList.add(qaServer);
serverList.add(stgServer);
return new OpenAPI().servers(serverList).components(new Components().addSecuritySchemes(“bearer-key”,
new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme(“bearer”).bearerFormat(“JWT”))
).info(new Info().title(“Pro API”).description(
“Pro description”));

Enabling Cross-origin resource sharing (CORS)

This was solved using @CrossOriginin controller

MUST read —

How can I use GIT pages for swagger UI

Further reading- https://github.com/peter-evans/swagger-github-pages

Previously published at https://medium.com/@nikitajos/openapi-swagger-v3-and-microservices-776df1bda93c