Automating OpenAPI Documentation Generation

Maintaining up-to-date API documentation can be a significant challenge in fast-paced development environments, particularly for staging environments. Outdated or inaccurate API documentation can lead to confusion, integration issues, and increased development/testing time for API consumers. Recognizing this pain point, an automated workflow for generating and publishing OpenAPI documentation we implemented, significantly improving the reliability and timeliness of our API information. This blog details the previous manual process and the enhanced automated system.

Manual API Documentation Process

Our initial approach to API documentation was largely manual and prone to inconsistencies. Developers were responsible for updating the OpenAPI specification whenever changes were made to the codebase. This often involved:

  1. Manual Updates: Developers would modify the OpenAPI specification files (typically YAML or JSON) and commit them to reflect API changes.
  2. Lack of Validation: There was no documentation validation in place, which meant developers had no way of knowing if their commits would break the API documentation build.
  3. Language Review: Writers manually reviewed the summaries and descriptions added to the API specification in the source YAML files.
  4. Manual Publishing: Once a release branch was finalized, writers would manually upload the source files to the documentation repository and deploy them to the API documentation portal.

This process was time-consuming, error-prone, and often resulted in documentation lagging behind the latest API versions. The lack of automation meant that keeping the documentation current required constant follow-ups and efforts.

Visual representation of the manual process:

%%{init: {'theme':'base', 'flowchart': {'layout': 'elk'}}}%% flowchart LR A["Update OpenAPI Spec in Source Code"] A --> C["Commit & Push YAML/JSON Files"] C --> D["Writer Review"] D --> F["Manual Upload to Docs Repo"] F --> G{"Render HTMLs (Redocly)"} G -- "Fail" --> H["Troubleshoot Errors"] G -- "Pass" --> I["Deploy API Documentation"] H --> I

Automated OpenAPI Documentation Process

To address the shortcomings of the manual process, I designed an automated workflow leveraging Redocly, python scripts, and CICD. This system automatically fetches, validates, and publishes OpenAPI documentation directly from the source code. For the development branch, this is implemented on a 12 hour cycle. The production workflow depends on the release branch and is triggered as needed rather than on a schedule. The key components of this new workflow include:

  1. Scheduled Fetching: A process is scheduled to run every 12 hours, automatically fetching the latest OpenAPI specification from the designated source repository.
  2. Upstream Validation: The API specification undergoes validation using Redocly. An optional validation step is introduced in the source repository where every time an API spec is updated, the commit triggers a Redocly build command to convert all the YAML files to HTML files. Any errors reported here are automatically emailed to the writing team, indicating which commit caused a specific error in a particular YAML file. This has significantly reduced the turnaround time for troubleshooting API documentation generation errors.
  3. Automated Publishing: The source files are fetched into the documentation repository, and a CI/CD pipeline is configured to automatically render the HTML files generated by Redocly. The updated OpenAPI documentation is then automatically published to our documentation portal.
  4. Error Reporting: In case of validation failures or any other errors during the process, detailed error reports are generated.
  5. Email Notifications: The writing team receives automated email notifications about the success or failure of each documentation update, ensuring we are promptly informed of any issues.

This automated approach ensures that our API documentation remains consistently in sync with the latest changes in the source code, without requiring manual intervention.

Visual representation of automated process:

%%{init: {'theme':'base', 'flowchart': {'layout': 'elk'}}}%% flowchart LR A["API Changes in Source Code (Dev Branch)"] A --> B{"Optional Validation in Source Repo</br>(Redocly Build on Every Commit)"} B --> C["Scheduled Job (Every 12 Hours)"] B --> J["Email Notification to Writing Team (Commit Errors)"] C --> D["Fetch OpenAPI Spec into Docs Repo"] D --> E{"Render HTMLs (Redocly)"} E -- Pass --> F["Publish Docs to Portal"] F --> G["Email Notification (Success)"] E -- Fail --> H["Generate Error Report"] H --> I["Email Notification (Failure)"]

Benefits of the Automated Workflow

Implementing this automated OpenAPI documentation generation workflow has yielded significant benefits:

Keywords

API Documentation OpenAPI Redocly Swagger Postman Automation CI/CD Git Python YAML Technical Writing Docs-as-Code


Schedule a meeting