Motivation
If you are developing a REST API to support websites or mobile applications, there comes a time when you have to communicate the signatures, and authorization schemes of new or existing endpoints to inform frontend development. Instead of communicating imprecisely with prose, it ia much better to communicate with structured documentation. It is even better if that structured documentation can be automatically generated and shared. Here we show you how we implemented automatic Swagger API documentation for our server project.
Frontend Integration
Giving the frontend team access to Swagger docs provides a clear reference for them to develop their client code and utilize the server resources. Also once you have Swagger documentation (json or yaml) it is possible to use it to generate client-side code That way you can load swagger.yaml as a git submodule, or add it to your frontend continous integration to always stay in sync with new updates in the REST API, and avoid writing boilerplate code.
QA
Testers can load automatic swagger documentation (json or yaml) into their REST API testing clients, like Postman or Insomnia. They can stay up to date with the latest changes, and know exactly the request parameters and expected responses. This can help backend engineers test their changes when they hit the pre-production environment, as well QA Engineers help in validation, and checking edge cases.
Third-party Integrations
If you plan on licensing access to your REST API, automatic Swagger documentation can help communicate with business partners exactly how to use and test your endpoints, and build using your API.
Implementation
Server code
We have a REST API defined in JAX-RS in our server. We didn’t want to have to write new Swagger annotations for every API defintion existing on our code base. So we looked for a Maven plugin that is able to interpret the JAX-RS annotations and produce the Swagger documentation. We found this one which was perfect for our use case.
We added the plugin to our pom.xml
Make sure to use a good value for operationIdFormat
like
{{className}}_{{methodName}}_{{httpMethod}}
Then we added some annotations to group endpoints under tags:
Finally when we compile our code, we Swagger maven plugin handles the rest and
produces a file in target/swagger/swagger.yaml
mvn -Dswagger.skip=false compile
Script to update swagger documentation
Finally approach we took was to run a script as part of our continuous deployment workflow in CircleCI, which committed the swagger.yaml file to another repository to be read and loaded by other members of the organization.
We run this script whenever new code updates are pushed to our pre-production environment, and everyone is kept in sync with the latest changes.