At Drivy, we have an internal API to communicate with our native apps available on both iOS and Android. One of the main pain point we experienced is documentation.
As our product is constantly evolving, we need to have up-to-date documentation in order to help both mobile and backend developers stay aware of what each endpoint is expecting and returning.
Static documentation is known to be hard to maintain, it’s indeed easy to forget to update it from time to time. What is more, you quickly end up with differences between documentation and actual behaviour.
The solution we are going to see is something between a static and a live documentation.
Our backend is written in Rails and we use Rspec as part of our test suite. For our API, we use active_model_serializers to handle the view component of the MVC pattern. Here, views are called serializers. We tried other options like RABL, but felt that active_model_serializers was the best choice. For example, its DSL is inspired from Rails, so a Ruby developer does not need special training to learn how to use it, it’s also more simple to do unit testing.
We tend to have a serializer per action and some nested nodes are generated by shared ones.
Here’s an example of one of our serializer spec.
Under the hood, we generate the JSON output from the serializer, and compare it to our expected_json
variable. If any difference is spotted, the spec will fail.
What’s really interesting here is that this is an actual test. This means that any change to the serializer will break the spec, preventing a release to production and therefore forcing developers to actually update the test.
So what we see in this file will always be synchronised with the production generated content.
From a quick look, you can learn attribute names, possible values/types, and if some changes are tied to a specific app version, you can have different context, exposing those behaviors.
We’ve been using this solution for more than a year, with new developers joining the team, and it still fits our needs. Contexts and expected outputs are easy to read by a non-Ruby developer.
Of course, we have room for improvements, for example, it’s missing description of attributes, endpoints path, etc. But it’s a win-win solution, because we have all benefits of unit testing, and it acts as a documentation.
With some additional work, we could easily generate a standalone documentation on a regular basis regrouping all serializers output in a single page.
We’ve only been seeing a standalone piece of documentation for what a client can expect to receive from requesting our API. But what about inputs? What does a client need to send as parameters to properly receive those answers. In a future article, we’ll see how we managed to document request inputs.