-
Notifications
You must be signed in to change notification settings - Fork 108
Reorganization of best practices and addition of overlays for globals #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
95994f9
33657bc
8fbd3e8
102f1c8
79d0df9
142cf2c
faa12bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| --- | ||
| layout: default | ||
| title: Add OpenAPI Descriptions to Source Control | ||
| parent: Best Practices | ||
| nav_order: 3 | ||
| --- | ||
|
|
||
| # Add OpenAPI Descriptions to Source Control | ||
|
|
||
| OpenAPI Descriptions are **not** just a documentation artifact: they are **first-class source files** which can drive a great number of automated processes, including boilerplate generation, unit testing and documentation rendering. | ||
|
|
||
| As such, OADs should be committed to source control, and, in fact, they should be among the first files to be committed. From there, they should also participate in Continuous Integration processes. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| --- | ||
| layout: default | ||
| title: Describing Large APIs | ||
| parent: Best Practices | ||
| nav_order: 6 | ||
| --- | ||
|
|
||
| # Describing Large APIs | ||
|
|
||
| This is a collection of small hints related to working with large OADs. | ||
|
|
||
| - **Do not repeat yourself** (The DRY principle). If the same piece of YAML or JSON appears more than once in the document, it's time to move it to the `components` section and reference it from other places using `$ref` (See [Reusing Descriptions](../specification/components). Not only will the resulting document be smaller but it will also be much easier to maintain). | ||
|
|
||
| Components can be referenced from other documents, so you can even reuse them across different API descriptions! | ||
|
|
||
| - **Split the description into several documents**: Smaller files are easier to navigate, but too many of them are equally taxing. The key lies somewhere in the middle. | ||
|
|
||
| A good rule of thumb is to use the natural hierarchy present in URLs to build your directory structure. For example, put all routes starting with `/users` (like `/users` and `/users/{id}`) in the same file (think of it as a "sub-API"). | ||
|
|
||
| Bear in mind that some tools might have issues with large files, whereas some other tools might not handle too many files gracefully. The solution will have to take your toolkit into account. | ||
|
|
||
| - **Use tags to keep things organized**: [Tags](https://spec.openapis.org/oas/v3.1.0#oasTags) have not been described in the Specification chapter, but they can help you arrange your operations and find them faster. A tag is simply a piece of metadata (a unique name and an optional description) that you can attach to [operations](../specification/paths). Tools, specially [GUI editors](https://tools.openapis.org/categories/gui-editors), can then sort all your API's operation by their tags to help you keep them organized. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| --- | ||
| layout: default | ||
| title: Best Practices | ||
| nav_order: 5 | ||
| has_children: true | ||
| has_toc: false | ||
| --- | ||
|
|
||
| # Best Practices | ||
|
baywet marked this conversation as resolved.
|
||
|
|
||
| This page contains general pieces of advice which do not strictly belong to the [Specification Explained](../specification) chapter because they are not directly tied to the OpenAPI Specification (OAS). | ||
|
baywet marked this conversation as resolved.
|
||
|
|
||
| However, they greatly simplify creating and maintaining OpenAPI Descriptions (OADs), so they are worth keeping in mind. | ||
|
baywet marked this conversation as resolved.
|
||
|
|
||
| This section is split into topic-specific pages: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we going to correctly maintain this list every time we add or change content? Even if we've done this somewhere else, I'd be tempted to just lean on "check the pages in this section" or something
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for the feedback, updated to use jekyll list instead of manually maintained one. |
||
|
|
||
| - [Use a Design-First Approach](use-a-design-first-approach) | ||
| - [Keep a Single Source of Truth](keep-a-single-source-of-truth) | ||
| - [Add OpenAPI Descriptions to Source Control](add-openapi-descriptions-to-source-control) | ||
| - [Make the OpenAPI Descriptions Available to the Users](make-the-openapi-descriptions-available-to-the-users) | ||
| - [There is Seldom Need to Write OpenAPI Descriptions by Hand](there-is-seldom-need-to-write-openapi-descriptions-by-hand) | ||
| - [Describing Large APIs](describing-large-apis) | ||
| - [Use Overlays for Global Information](use-overlays-for-global-information) | ||
| - [Links to External Best Practices](links-to-external-best-practices) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| --- | ||
| layout: default | ||
| title: Keep a Single Source of Truth | ||
| parent: Best Practices | ||
| nav_order: 2 | ||
| --- | ||
|
|
||
| # Keep a Single Source of Truth | ||
|
|
||
| Regardless of your design approach (design-first or code-first) always keep a single source of truth, i.e., information should **not** be duplicated in different places. It is really the same concept used in programming, where repeated code should be moved to a common function. | ||
|
baywet marked this conversation as resolved.
|
||
|
|
||
| Otherwise, eventually one of the places will be updated while the other won't, leading to headaches... in the best of cases. | ||
|
baywet marked this conversation as resolved.
|
||
|
|
||
| For instance, it is also commonplace to use code annotations to generate an OpenAPI description and then commit the latter to source control while the former still lingers in the code. As a result, newcomers to the project will not know which one is actually in use and mistakes will be made. | ||
|
|
||
| Alternatively, you can use a Continuous Integration test to ensure that the two sources stay consistent. | ||
|
baywet marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| layout: default | ||
| title: Links to External Best Practices | ||
| parent: Best Practices | ||
| nav_order: 8 | ||
| --- | ||
|
|
||
| # Links to External Best Practices | ||
|
|
||
| There's quite a bit of literature about how to organize your API more efficiently. Make sure you check out how other people solved the same issues you are facing now! | ||
|
|
||
| For example: | ||
|
|
||
| - The [API Stylebook](http://apistylebook.com/design/guidelines/) contains internal API Design Guidelines shared with the community by some well known companies and government agencies. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| --- | ||
| layout: default | ||
| title: Make the OpenAPI Descriptions Available to the Users | ||
| parent: Best Practices | ||
| nav_order: 4 | ||
| --- | ||
|
|
||
| # Make the OpenAPI Descriptions Available to the Users | ||
|
baywet marked this conversation as resolved.
|
||
|
|
||
| Beautifully-rendered documentation can be very useful for the users of an API, but sometimes they might want to access the source OAD. For instance, to use tools to generate client code for them, or to build automated bindings for some language. | ||
|
|
||
| Therefore, making the OAD available to the users is an added bonus for them. The documents that make up the OAD can even be made available through the same API to allow runtime discovery. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| --- | ||
| layout: default | ||
| title: There is Seldom Need to Write OpenAPI Descriptions by Hand | ||
| parent: Best Practices | ||
| nav_order: 5 | ||
| --- | ||
|
|
||
| # There is Seldom Need to Write OpenAPI Descriptions by Hand | ||
|
baywet marked this conversation as resolved.
|
||
|
|
||
| Since OADs are plain text documents, in an easy-to-read format (be it JSON or YAML), API designers are usually tempted to write them by hand. | ||
|
|
||
| While there is nothing stopping you from doing this, and, in fact, hand-written API descriptions are usually the most terse and efficient, approaching any big project by such method is highly impractical. | ||
|
|
||
| Instead, you should try the other existing creation methods and choose the one that better suits you and your team (No YAML or JSON knowledge needed!): | ||
|
baywet marked this conversation as resolved.
|
||
|
|
||
| - **OpenAPI Editors**: Be it [text editors](https://tools.openapis.org/categories/text-editors) or [GUI editors](https://tools.openapis.org/categories/gui-editors) they usually take care of repetitive tasks, allow you to keep a library of reusable components and provide real-time preview of the generated documentation. | ||
|
|
||
| - **Domain-Specific Languages**: As its name indicates, [DSL](https://tools.openapis.org/categories/dsl)'s are API description languages tailored to specific development fields. A tool is then used to produce the OpenAPI Description. A new language has to be learned, but, in return, extremely concise descriptions can be achieved. | ||
|
|
||
| - **Code Annotations**: Most programming languages allow you to _annotate_ the code, be it with specific syntax or with general code comments. These annotations, for example, can be used to extend a method signature with information regarding the API endpoint and HTTP method that lead to it. A tool can then parse the code annotations and generate OADs automatically. This method fits very nicely with the code-first approach, so keep in mind the advice in [Use a Design-First Approach](use-a-design-first-approach) when using it. | ||
|
baywet marked this conversation as resolved.
|
||
|
|
||
| - **A Mix of All the Above**: It's perfectly possible to create the bulk of an OpenAPI Description using an editor or DSL and then hand-tune the resulting file. Just be aware of [Keep a Single Source of Truth](keep-a-single-source-of-truth): once you modify a file **it becomes the source of truth** and the previous one should be discarded (maybe keep it as backup, but out of the sight and reach of children and newcomers to the project). | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| --- | ||
| layout: default | ||
| title: Use a Design-First Approach | ||
| parent: Best Practices | ||
| nav_order: 1 | ||
| --- | ||
|
|
||
| # Use a Design-First Approach | ||
|
|
||
| Traditionally, two main approaches exist when creating OADs: **Code-first** and **Design-first**. | ||
|
|
||
| - In the Code-first approach, **the API is first implemented in code**, and then its description is created from it, using code comments, code annotations or simply written from scratch. This approach does not require developers to learn another language so it is usually regarded as the easiest one. | ||
|
|
||
| - Conversely, in Design-first, **the API description is written first** and then the code follows. The first obvious advantages are that the code already has a skeleton upon which to build, and that some tools can provide boilerplate code automatically. | ||
|
|
||
| There have been a number of heated debates over the relative merits of these two approaches but, in the opinion of the OpenAPI Initiative (OAI), the importance of using **Design-first** cannot be stressed strongly enough. | ||
|
baywet marked this conversation as resolved.
|
||
|
|
||
| The reason is simple: **The number of APIs that can be created in code is far superior to what can be described in OpenAPI**. To emphasize: **OpenAPI is not capable of describing every possible HTTP API, it has limitations**. | ||
|
|
||
| Therefore, unless these descriptive limitations are perfectly known and taken into account when coding the API, they will rear their ugly head later on when trying to create an OpenAPI description for it. At that point, the right fix will be to change the code so that it uses an API which can be actually described with OpenAPI (or switch to Design-first altogether). | ||
|
|
||
| Sometimes, however, since it is late in the process, it will be preferred to twist the API description so that it matches *more or less* the actual API. It goes without saying that this leads to **unintuitive and incomplete descriptions**, that will rarely scale in the future. | ||
|
|
||
| Finally, there exist a number of [validation tools](https://tools.openapis.org/categories/data-validators) that can verify that the implemented code adheres to the OpenAPI description. Running these tools as part of a Continuous Integration process allows changing the OpenAPI Description with peace of mind, since deviations in the code behavior will be promptly detected. | ||
|
|
||
| > **Bottom line:** | ||
| > OpenAPI opens the door to a [wealth of automated tools](https://tools.openapis.org). Make sure you use them! | ||
Uh oh!
There was an error while loading. Please reload this page.