Automatically Generating OpenAPI Docs for Mastodon
Tags: mastodon, api, rails, ruby
Published: 2022-12-14 20:26:15

As the Mastodon open source community grows, many developers are coming into the project for the first time and learning more by viewing the documentation.  One of the common pieces of documentation that users in both the [Mastodon Discourse](https://discourse.joinmastodon.org/t/openapi-documentation-of-the-api/3929) and [GitHub issues](https://github.com/mastodon/mastodon/issues/20572) are looking for is an up-to-date version of API documentation. 

One user proposes writing rswag specs to allow for automatically documenting the APIs and generating the OpenAPI schema. The downside to this approach is the additional engineering time it takes for adding this net new code to the project. 

The OpenAPI specification, which is formerly known as the Swagger Specification, is a [community-driven open standard]((https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md)) for documenting APIs. OpenAPI allows both humans and computers to discover and understand the capabilities of a service without requiring access to the service’s source code. 

In the previous parts of this series, we showed you:
1. [How you can install AppMap into Mastodon](https://dev.to/appmap/installing-appmap-in-mastodon-with-vs-code-167d)
2. [How to visualize Mastodon code internals](https://dev.to/appmap/visualizing-mastodon-internals-1198)
3. [How to run the Mastodon rspec test suite](https://dev.to/appmap/the-fastest-way-to-run-mastodon-tests-5g03). 

By now you’ll now have AppMaps recorded from the Mastodon tests. The [AppMap data specification](https://github.com/getappmap/appmap) includes all of the API calls processed including the schema of each request and response. Using the AppMap CLI (or the [VS Code](https://marketplace.visualstudio.com/items?itemName=appland.appmap) or [JetBrains](https://plugins.jetbrains.com/plugin/16701-appmap) extension) developers can automatically output OpenAPI specifications for Mastodon in just 3 steps.

* Add AppMap to Mastodon
* Run the Mastodon Test suite
* Generate the OpenAPI Spec


##Add AppMap to Mastodon
[Check out part 1 of this series](https://dev.to/appmap/installing-appmap-in-mastodon-with-vs-code-167d) to install AppMap libraries into the Mastodon code base, and to install the [VS Code](https://marketplace.visualstudio.com/items?itemName=appland.appmap) or [JetBrains](https://plugins.jetbrains.com/plugin/16701-appmap) extension. 

If you are not using VS Code or Jetbrains, or if you want to incorporate OpenAPI generation into your CI/CD system, you can install the AppMap CLI and generate OpenAPI docs on the command line. 
##Run the Mastodon Test suite
[In Part 3 of this series](https://dev.to/appmap/the-fastest-way-to-run-mastodon-tests-5g03) you learned how to run the entire Mastodon rspec test suite.  After running the full test suite you’ll have a collection of code-accurate AppMaps with all the up-to-date API request information within. 
##Generate the OpenAPI Spec
Now that AppMaps exist with our API requests, you can generate the OpenAPI v3 supported specification file via either VS Code or using the AppMap CLI. From there you can import this into [Postman](https://www.postman.com/), other API management tools, or share with your team.

###Generate in VS Code

After the Mastodon test cases complete - click the “Generate OpenAPI” button on the left column. 

![openapi-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdsp8ua5lcwkf57hc0mf.png)

This will open the OpenAPI introduction page. Then click the “Generate OpenAPI Definitions” button and your OpenAPI specification will be displayed in the editor.  Save this data anywhere you need it for later usage or distribution. 

![openapi-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5cwac64m57cqxe0s5ajs.png)

For example: I loaded this API Specification into [Postman](https://blog.postman.com/new-postman-integration-with-appmap-create-and-manage-always-accurate-collections/) as a new collection.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eepu7gasn2bu9gfn24a5.png)

###Generate with AppMap CLI
If you are not a VS Code user, or if you want to incorporate OpenAPI generation as part of your CI/CD build process, you can run the AppMap CLI command after the `rspec` tests have completed to similarly output the API documentation to a user-defined output file.  

The AppMap CLI is written in JavaScript which makes it easy to run across a variety of platforms. You’ll need [Node.js](https://nodejs.org/en/) installed or alternatively you can grab pre-built binaries for [@appland/appmap on Github.](https://github.com/getappmap/appmap-js/releases)

```
npx @appland/appmap@latest openapi --output-file openapi.yml
```

Now you can import this OpenAPI documentation into other tools to more easily interact with your APIs, test new or updated features, and finally export these OpenAPI specifications to your users so that they will know how to interact with your application as well.  

You can learn more about other features of AppMap by reviewing our [documentation](https://appmap.io/docs/appmap-overview.html) or you can join the developers in [our community Slack.](https://appmap.io/slack)