Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger best practices

Tags:

swagger

I am currently defining a Rest API and I intend to use Swagger to document it. I've already started to define my API specifications with the Open Api Specification in YAML into the Swagger-Editor.

Then, I understand that I will provide that file to the Swagger-codegen to generate a server implementation, and also to the Swagger-UI (whose statics files will be previously paste to my server) to expose the interactive documentation .

According to Swagger, this is the top-down approach.

But later, I will probably need to modify this API, and I want to do it via by this tediously YAML file previously defined, to keep the API easily modifiable by anyone (and Language-agnostic).

  1. Does the way to do this is to modify the definition file and then re-use Swagger-codegen ? By this approach, I guess so that I can't even lightly modify the API directly in the implementation server code without risking to have a out of date documentation.

And If I chose to do the bottom-up approach (via Swagger-core annotation), I will restrain all my further modifications to occur in the implementation server code, and my initial definition file will never be usable again.

  1. So another question would be : Is there a common way to deal with Swagger when we want to modify the API both via the specification file and via the implementation server code (I suppose that the file that Swagger-core can generate me from my code, will never looks like my initial one that I defined by hand).
like image 800
Victor Petit Avatar asked Jun 14 '16 14:06

Victor Petit


1 Answers

To maintain the API documentation, the best course of action that i can suggest is to follow a hybrid approach.

Initially, when you have to do bulk development, go for the top down approach. This will reduce the initial set up and coding effort. That's the basic idea behind any codegen.

Then, when it comes to maintaining the APIs, or adding a few new ones every day (or week), follow the bottom up approach. You will already have the previous code, the only thing you'll need to do is add some more annotations or API definitions.

Going for top-down iteratively defeats the purpose of code maintenance. Boiler plates and self generated code are there to give you a quick start, not for sustenance.

like image 157
Sampada Avatar answered Dec 25 '22 23:12

Sampada