Skip to content

Commit ecc0670

Browse files
authored
Add OpenAPI best practices (API Definitions) (#697)
1 parent 3116064 commit ecc0670

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

fern/products/api-def/openapi-pages/overview.mdx

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ title: What is an OpenAPI Specification?
33
description: OpenAPI is a standard for documenting REST APIs
44
---
55

6-
The OpenAPI Specification (OAS) is a framework used by developers to document REST APIs. The specification
6+
The OpenAPI Specification (OAS) is a framework used by developers to document REST APIs. The specification is
77
written in JSON or YAML and contains all of your endpoints, parameters, schemas, and authentication schemes.
88
Fern is compatible with the latest OAS release, which is currently [v3.1.1](https://spec.openapis.org/#openapi-specification).
99

1010
Below is an example of an OpenAPI file:
1111

12-
```yaml openapi.yml
12+
```yaml title="openapi.yml" maxLines=10
1313
openapi: 3.0.2
1414
info:
1515
title: Petstore - OpenAPI 3.0
@@ -101,8 +101,46 @@ components:
101101
name: api_key
102102
in: header
103103
```
104+
## Best practices
104105
105-
## Setup your fern folder
106+
Follow these best practices to ensure your OpenAPI specification generates high-quality SDKs and documentation:
107+
108+
- **Organize with proper project structure.** Follow the instructions at [Project structure](/api-definitions/overview/project-structure) to clearly organize the directories that contain your definition and other related files.
109+
- **Add `operationId` to endpoints.** Include a clear `operationId` for each endpoint to control the function names generated in your SDKs. (Or use [extensions to customize group and method names](/api-definitions/openapi/extensions/method-names).)
110+
- **Reference schemas instead of inlining.** Define reusable schemas in the `components/schemas` section and reference them with `$ref`. This promotes consistency, reduces duplication, and makes maintenance easier.
111+
<Accordion title="Example of referencing schemas">
112+
```yaml title="openapi.yml" {8, 14, 17-25}
113+
paths:
114+
/pets:
115+
post:
116+
requestBody:
117+
content:
118+
application/json:
119+
schema:
120+
$ref: '#/components/schemas/Pet' # Clean reference
121+
responses:
122+
'200':
123+
content:
124+
application/json:
125+
schema:
126+
$ref: '#/components/schemas/Pet' # Reused easily
127+
components:
128+
schemas:
129+
Pet: # Defined once, used everywhere
130+
type: object
131+
properties:
132+
name:
133+
type: string
134+
status:
135+
type: string
136+
enum: [available, pending, sold]
137+
```
138+
</Accordion>
139+
- **Use overrides and Fern extensions for customization.** Customize your specification using Fern [extensions](/api-definitions/openapi/extensions/overview) housed in an [overrides file](/api-definitions/overview/overrides). This lets you modify generation behavior without changing your core OpenAPI definition.
140+
141+
Once your OpenAPI spec follows these practices, you're ready to set up your fern folder.
142+
143+
## Set up your fern folder
106144

107145
<Info> Considering options to generate an OpenAPI spec? Get live support [here](https://fern-community.slack.com/join/shared_invite/zt-2dpftfmif-MuAegl8AfP_PK8s2tx350Q%EF%BB%BF#/shared-invite/email) </Info>
108146

0 commit comments

Comments
 (0)