OpenAPI: How to Handle File Management
As more organizations adopt OpenAPI, it's good to get up to speed on managing the files you create and consume.
Apr 8th, 2025 9:00am by
Image by Nils Schirmer from Unsplash.
Design-First APIs
Design-first is emerging as an efficient and effective approach to building APIs. In design-first APIs, the OpenAPI description is updated to describe the proposed API changes. Documentation and mock servers may also be produced at this stage, while feedback is collected from stakeholders. Once the changes are approved and accepted, the API is implemented. Working in a design-first approach means that changes to API design are easy to achieve at an early stage, where only the OpenAPI description needs to be updated and no code has been written yet. The downside is that many organizations find the OpenAPI format difficult to work with; often the API description is a single file and it may run to hundreds of thousands of lines of YAML or JSON. While having a good editor with OpenAPI support really helps, the structure of the OpenAPI description itself can also make a big difference to your API authoring experience.Use OpenAPI $ref Syntax
One reason the files get so long is that the OpenAPI format is, by necessity, verbose and because the same elements are often repeated in multiple places. For example, a newsletter microservice probably has an email field and a list of subscriptions that are the same in more than one endpoint. OpenAPI makes this repetition easy with its `$ref` syntax. Define the field once in the `components` section of the OpenAPI description and then refer to it with `$ref` in every location where it is needed. Here’s a snippet of the newsletter microservice example: Using `$ref` not only makes the files more manageable but also encourages consistency by using the same schema definitions throughout an API description. Consistency is key for API experience and so the `$ref` syntax serves both producer and consumer when it is used. The `$ref` syntax can also refer to content in other files, which gives users more ways to make their OpenAPI files easier to work with.Separate Components
Since OpenAPI 3.1 (released February 2021), an OpenAPI description can be valid with any one or more of `paths`, `webhooks` and `components` declared. What this means in practical terms is that if your API is purely webhooks or you have a single set of components that are used by multiple APIs, you can publish those API descriptions without needing an empty `paths: {}` object. Especially for microservices architectures, common components used to give a consistent experience across modules is a popular and helpful pattern. For example, for a shopping application that has separate “account” and “order” services, it might make sense to separate the components into one OpenAPI file, and refer to it from the individual API descriptions for the separate services. The directory structure would look something like this:
├── accounts.openapi.yaml
├── components.openapi.yaml
└── orders.openapi.yaml
$ref: 'components.openapi.yaml#/components/schemas/customer_name'
Radical OpenAPI File Structures
Some organizations split up their OpenAPI files more thoroughly, holding one file per operation and another for each element in the `components` section. With the `$ref` syntax, really anything is possible! At its most radical, the newsletter example from earlier ends up looking something like this: Whether you design and build your API structure this way, or use a tool like Redocly CLI’s `split` command to restructure an existing API description, working with many smaller files has advantages:- Each file is small enough to easily open and view
- You can view multiple files at once without losing your place in a long file.
- Proposed changes are much easier to review because you can see which areas are affected.
What’s Your Goldilocks Granularity?
There is no “right” answer for how to structure your OpenAPI files. Some teams are very happy with a single file and see no problem to solve. Others are fully committed to a very granular approach and wouldn’t consider changing. The right answer for your team might lie somewhere between the extremes, but by being aware of the options and open to change, you can find an approach that fits your needs and leads to more efficient and happier API experiences.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.