Understand OpenAPI 2.0 structure in drf-yasg
masterThe library generates OpenAPI 2.0 documents. The structure follows the official Swagger/OpenAPI 2.0 specification:
- Swagger Object: Root object containing
info,schemes,securityDefinitions,paths, anddefinitions. - Paths: A mapping of
{path}toPathItemobjects. EachPathItemcontains operations keyed by HTTP method (e.g.,GET,POST). - Operation: Identified by
(path, http_method). Containsparameters(query, header, form),responses(mapping of status codes toResponseobjects),operationId, andtags. - Definitions: A mapping of named models (
{ModelName}) toSchemaobjects.
Schema vs. Parameter
| Feature | Schema | Parameter |
|---|---|---|
| Nesting | Can nest other Schemas | Cannot nest other Parameters (except if in: body) |
| File Uploads | Cannot describe file uploads | Can describe via type = file (only in form operations) |
| Responses | Can be used in Responses | Cannot be used in Responses |
| Form Operations | Cannot be used in form operations | Can be used in form operations |
| Scope | Request or response bodies | query, form, header, or path parameters |