Can Swagger autogenerate its yaml based on existing express routes?

Solution 1:

I have experience in BOTH auto-generating the Swagger json and manually writing it out for an API that I helped build. Here are the pros/cons of both based on my experience.

Swagger AUTOMATIC Documentation Generation:

We used the swagger-node-express module in combination with swagger-ui. https://www.npmjs.com/package/swagger-node-express
https://github.com/swagger-api/swagger-ui

Pros

Super easy to document. Just throw a few lines above the resource definition and the documentation (json) is automatically generated by the module.

Cons

You are no longer using straight up Express when you use this package. Your route definitions have to be defined through the Swagger module and this pulls you away from vanilla Express.

Swagger MANUAL Documentation Generation:

We just pulled swagger-ui into the project and wrote the documentation manually.
https://github.com/swagger-api/swagger-ui

Pros

This approach decouples the documentation from the Express framework. Express endpoints are written as they normally would be written and the Swagger documentation is defined separate from the Express framework. Allows you to write pure express.

Cons

Documentation changes become a little more tedious due to the fact that you are manually writing and changing the yaml or json yourself. It's a little bit harder than just updating a few lines of code above a resource. This approach is also a little more prone to documentation typos and errors due to the fact it is entirely manually typed.

If you are planning to manually write your swagger documentation use the swagger editor below to validate your manual docs.
http://editor.swagger.io/#/

Conclusion

For this API project, we started out by auto-generating the documentation using the swagger-node-express package. However, we realized that decoupling the swagger documentation from the express library was important to enable us to use all the features and functionality of Express. I recommend manually writing the docs to have full control over both the Swagger documentation and the Express web framework that your app will use.

Solution 2:

There is an option: you can embed middleware that will analyse all requests and responses and generate specification for you: https://github.com/mpashkovskiy/express-oas-generator

Then you can use it through your's app Swagger UI like http://host:port/api-docs

Solution 3:

Yes !!!. You can use this awesome project typescript-test. Here is sample app. Clone it, run npm i,npm run swagger and go to /dist/swagger.json. Done. Swagger yaml and json is generated based on express routes !

Solution 4:

Have a look to swagger-jsdoc. It's a different approach.

The docs stick to the code, and also lets the express code to remain pure.

Guides:

  • https://dev.to/acanimal/express-api-with-autogenerated-openapi-doc-through-swagger-7na

  • https://dev.to/akshendra/generating-documentation-on-the-fly-in-express-2652