How Schemas Are Generated
The serverpod_swagger generator automatically creates OpenAPI schemas from your Serverpod model files and Dart endpoint signatures. This page explains how schema generation works and how you can influence the output.
Automatic Schema Generation
The generator reads two sources to build schemas:
- YAML model files in
lib/src/models/(both.yamland.spy.yamlfiles) -- these produce named schemas undercomponents/schemas. - Dart endpoint parameters and return types -- primitive types are inlined, while references to model classes use
$refpointers.
Serverpod Model File Example
Given a standard Serverpod model file:
1class: User
2fields:
3 name: String
4 email: String
5 age: int?
6 role: StringThe generator produces the following OpenAPI schema:
1{
2 "User": {
3 "type": "object",
4 "properties": {
5 "name": { "type": "string" },
6 "email": { "type": "string" },
7 "age": { "type": "integer", "format": "int64" },
8 "role": { "type": "string" }
9 },
10 "required": ["name", "email", "role"]
11 }
12}Non-nullable fields are added to the required array. Nullable fields (ending with ?) are omitted from required.
Supported Type Mappings
| Dart/YAML Type | OpenAPI Type |
|---|---|
String | string |
int | integer (format: int64) |
double | number (format: double) |
bool | boolean |
DateTime | string (format: date-time) |
ByteData | string (format: byte) |
Duration | string (ISO 8601) |
Uri | string (format: uri) |
UuidValue | string (format: uuid) |
List<T> | array (items: schema of T) |
Map<K, V> | object (additionalProperties: true) |
| Enums | string with enum values |
| Model classes | $ref to #/components/schemas/ClassName |
Cross-Module Model Resolution
When an endpoint references a model from another Serverpod module (e.g., module:auth:UserInfo), the generator automatically locates and parses that model from the dependency's package using package_config.json.
Editing the Generated Spec
The generator writes apispec.json as a standard JSON file. You can edit it manually after generation to add custom descriptions, examples, or additional schemas. Use the --update flag on subsequent runs to apply CLI changes without overwriting your manual edits:
# First run: generate from source
dart run serverpod_swagger:generate --auth=jwt
# Later: update auth settings without regenerating
dart run serverpod_swagger:generate --update --unsecure-endpoints=publicNote: Running without --update regenerates the entire spec from source, which will overwrite any manual edits to apispec.json.