|
| 1 | +# Best Practices for OpenAPI Documentation |
| 2 | + |
| 3 | +Writing clear and comprehensive OpenAPI documentation is critical for developer adoption and API longevity. Below are best practices for versioning, model management, and endpoint documentation. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 1. Model Management |
| 8 | + |
| 9 | +Models (schemas) define the structure of request/response data. Consistency here reduces confusion. |
| 10 | + |
| 11 | +### Best Practices: |
| 12 | +- **Leverage JSON Schema**: Use OpenAPI's schema syntax to enforce validation. |
| 13 | + |
| 14 | + ```yaml |
| 15 | + components: |
| 16 | + schemas: |
| 17 | + User: |
| 18 | + type: object |
| 19 | + required: |
| 20 | + - id |
| 21 | + - name |
| 22 | + properties: |
| 23 | + id: |
| 24 | + type: integer |
| 25 | + format: int64 |
| 26 | + name: |
| 27 | + type: string |
| 28 | + example: "Jane Doe" |
| 29 | + ``` |
| 30 | +- **Reuse Components**: Define reusable schemas, parameters, and responses under `components` to avoid duplication. |
| 31 | +- **Provide Examples**: Include sample data for clarity. |
| 32 | + |
| 33 | + ```yaml |
| 34 | + examples: |
| 35 | + UserExample: |
| 36 | + value: |
| 37 | + id: 1 |
| 38 | + name: "Jane Doe" |
| 39 | + ``` |
| 40 | +- **Document Enums and Constraints**: Specify allowed values, formats, and limits. |
| 41 | + |
| 42 | + ```yaml |
| 43 | + status: |
| 44 | + type: string |
| 45 | + enum: [active, inactive, pending] |
| 46 | + minLength: 4 |
| 47 | + maxLength: 8 |
| 48 | + ``` |
| 49 | +- **Version Models**: Introduce new schema versions (e.g., `UserV2`) for breaking changes. |
| 50 | + |
| 51 | +--- |
| 52 | + |
| 53 | +## 2. Endpoint Documentation |
| 54 | + |
| 55 | +Clear endpoint docs help developers integrate quickly. |
| 56 | + |
| 57 | +### Best Practices: |
| 58 | +- **Summarize and Describe**: |
| 59 | + |
| 60 | + ```yaml |
| 61 | + /users/{id}: |
| 62 | + get: |
| 63 | + summary: Get a user by ID |
| 64 | + description: Retrieves a single user's details, including name and contact info. |
| 65 | + ``` |
| 66 | +- **Document All Parameters**: |
| 67 | + - Specify path, query, header, and body parameters. |
| 68 | + - Include examples and required flags. |
| 69 | + |
| 70 | + ```yaml |
| 71 | + parameters: |
| 72 | + - in: path |
| 73 | + name: id |
| 74 | + required: true |
| 75 | + schema: |
| 76 | + type: integer |
| 77 | + example: 42 |
| 78 | + ``` |
| 79 | +- **Cover All Responses**: |
| 80 | + - Define HTTP status codes, especially errors (4xx/5xx). |
| 81 | + - Add examples for success and failure cases. |
| 82 | + |
| 83 | + ```yaml |
| 84 | + responses: |
| 85 | + 200: |
| 86 | + content: |
| 87 | + application/json: |
| 88 | + schema: |
| 89 | + $ref: "#/components/schemas/User" |
| 90 | + 404: |
| 91 | + description: User not found |
| 92 | + ``` |
| 93 | +- **Use Tags for Grouping**: Organize endpoints by functionality (e.g., `Users`, `Auth`). |
| 94 | + |
| 95 | + ```yaml |
| 96 | + tags: |
| 97 | + - name: Users |
| 98 | + description: Manage user accounts |
| 99 | + ``` |
| 100 | +- **Show Request/Response Examples**: |
| 101 | + |
| 102 | + ```yaml |
| 103 | + requestBody: |
| 104 | + content: |
| 105 | + application/json: |
| 106 | + schema: |
| 107 | + $ref: "#/components/schemas/User" |
| 108 | + examples: |
| 109 | + UserExample: |
| 110 | + value: |
| 111 | + name: "Jane Doe" |
| 112 | + ``` |
| 113 | +- **Standardize Error Formats**: |
| 114 | + |
| 115 | + ```yaml |
| 116 | + components: |
| 117 | + schemas: |
| 118 | + Error: |
| 119 | + type: object |
| 120 | + properties: |
| 121 | + code: |
| 122 | + type: integer |
| 123 | + message: |
| 124 | + type: string |
| 125 | + ``` |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +## Where do I add documentation? |
| 130 | +Good question, cowboy. We use serverless.yml to manage API declarations and deployments to APIGateway. So, it made sense to add a block for endpoint documentation. |
| 131 | + |
| 132 | +All documentation is added in the `docs` folder. The yaml file follows the OpenAPI standards. Read about OpenAPI spec here [openAPI](https://github.com/OAI/OpenAPI-Specification) |
| 133 | + |
| 134 | +Next, edit serverless open `serverless.yml` in `be-api-test` [serverless](https://github.com/Advanced-Infrastructure/be-api-test/blob/dev/serverless.yml) |
| 135 | +Under functions, see the documentation block `documentation: ${file(./docs/api/documentation.yml):endpoints.api-endpoint}` |
| 136 | + |
| 137 | +Make sure you declare the documentation block for every endpoint. |
| 138 | + |
| 139 | + |
| 140 | +--- |
| 141 | + |
| 142 | + |
| 143 | +## Final Tips |
| 144 | +- **Validate Your Spec**: Use tools like Swagger Editor or Spectral to lint your OpenAPI file. |
| 145 | +- **Automate Documentation**: Generate interactive docs with Swagger UI or Redoc. |
| 146 | +- **Keep It Consistent**: Follow the same structure, naming conventions, and style across all endpoints. |
| 147 | + |
| 148 | +Well-documented APIs reduce support overhead and empower developers to build faster. 📘🚀 |
0 commit comments