OpenAPI & AsyncAPI $ref: Advanced Guide
Blog post from Bump
OpenAPI and AsyncAPI are powerful tools for defining APIs, but their complexity can lead to unwieldy documents filled with repetitive YAML and JSON, increasing the likelihood of merge conflicts. To manage this, developers can use the `$ref` feature to split documents into reusable components, thus reducing redundancy and conflicts. This approach is exemplified in an OpenAPI documentation for a Tic Tac Toe game, where components like schemas and parameters are stored separately, making the main document lighter and easier to manage. Developers can choose their own conventions for organizing these files, but emerging best practices suggest using separate directories for different types of components. While this method can reduce conflicts and simplify document management, it can also complicate tracking API changes, especially if a schema change affects multiple endpoints. Tools like Bump.sh and Optic can help by detecting and reporting breaking changes. Additionally, `$ref` can be used with URLs to share components across multiple APIs, though this requires careful management to avoid issues with authentication and tool compatibility. Despite some challenges, such as the need for public hosting of shared components and the potential for changes to propagate unexpectedly, using `$ref` effectively can greatly enhance the manageability and scalability of API documentation.