Serializers control API response shape. Use them to decide what leaves the server and how associations are exposed.
- Expose only the fields needed by the client
- Keep controller responses consistent
- Separate response formatting from models and services
- Group serializers by resource under
api/src/serializers/{resource}/ - Export resource serializers through the resource folder index when the folder has multiple serializers
- Use distinct serializers for list and detail responses when they expose different fields
- Name output types to match the response shape, such as
UserAsIndexandUserAsShow - Validate association assumptions before serializing nested data
- Keep serializers declarative and focused on transformation, not authorization or persistence
- When index rows need policy data, serialize it as
PolicyAsReferenceusing a smallserializePolicy(record, currentUser)helper that returnsnew SomePolicy(currentUser, record).toJSON().