Synchronize your tests with your documentation
When you develop your API, a part of the job is to write the documentation. At Fabernovel, we used to write our documentation on a tool called Slate. However, by lack of time, clear process or experience, it happened that API were modified without the documentation. The longer our API lived, the more outdated or incorrect the documentation was.
Our documentation tool
To solve this problem, we decided to automate the detection of outdated documentation by developing our own tool called Pericles. One of the main feature of Pericles is a proxy. You can plug Pericles between your client and your server. Because Pericles acts as middle man, it can analyse the requests and responses and check if they are conformed to the specifications. It’s very useful to detect typo errors, wrong types or evolutions that were not specified. If you want to learn more about why and how we develop this tool you can check this awesome presentation made by Hugo Hache and Julie Rollin: French / English
But is this proxy feature enough to solve our problem of outdated or incorrect documentation? Well, the answer is no. If we only rely on this proxy, we need to wait that a request is performed to be analysed. So, we need to wait for our API to be deployed somewhere and for someone to use it. And to allow someone to use it, the documentation must be up to date.
In order to have earlier feedbacks, we can use automated tests to check that our API is correct according to the documentation.
Automated tests
To analyse the response and the request, Pericles uses json schemas, we can do the same in our tests.
The first step is to add the json-schema gem in our Gemfile
Then we add the json schemas of our requests and responses in our tests folders. For example, if we have a Product resource in our API our tests folder will look like this:
Then in our test_helper.rb
, we add two new helpers to validate the response and the request.
Finally in our controller tests, we can use those helpers to validate that the generated response and the sent request are correct according to the specification.
In order to
detect extra fields in the json that are not allowed, we set additionalProperties
to false in our json schemas.
You can learn more about it here.
There is one more step to do: automate the synchronization between our documentation and our json schemas folder. Luckily, Pericles provides a route to download all the json-schemas so we can create a rake task to automate it.
And voilà! By running this task when the documentation is updated, we can guarantee that our code is correctly implementing the specifications.
Conclusion
By writing those tests, we can detect many common mistakes that are made when we document our API:
- Numbers are serialized as string
- Fields are nullable but marked as non-nullable
- Update requests should have optional arguments but are marked as required.
We got a feedback early and solves the issues before deploying our API.
When we update our serializers, we can do it without fear of breaking anything. If it were used somewhere we were not aware of or we did not update the documentation, a test will fail. Furthermore if we update the documentation, for example by adding a new required attribute, we can rely on our tests to tell us where our serializers should be updated.
Before, we used to write code, push the code to our version control system and then write the documentation. Now the CI will prevent the code to be pushed if the documentation is not updated. We can no longer forget this step.
However, this solution is not perfect. Pericles does not handle versioning. If we have a long feature branch or several developers working at the same time, the automated synchronization can be painful to use as it will download all modifications made.
If you are using OpenAPI and not Pericles for your documentation, you can still use this technique because OpenAPI also relies on json schemas.