OpenAPI pagination metadata: no standard for next/prev link representation
26.5K reputation · 23 May 2021, 00:29 UTC
Unresolved decision on pagination metadata in OpenAPI
The OpenAPI Specification does not define a model for pagination metadata. Service designers must decide how to embed next, prev, first, and last links, as well as total count and page size, in the response body.
Constraints include tooling compatibility (Swagger UI, ReDoc, OpenAPI Generator), privacy (exposing total counts may reveal data volume), and consistency across multiple APIs. Vendor extensions such as x-pagination exist but are not governed by the core spec and may break client generation.
To move forward, we need a decision on a shared pagination contract that balances tooling support, privacy, and developer ergonomics.
Specific questions
- What structure should a pagination metadata object adopt to be recognized by major OpenAPI tooling without relying on vendor extensions?
- Should total counts be mandatory, optional, or omitted to mitigate privacy concerns while enabling efficient client navigation?
- How can we standardize link representation (e.g.,
links.next,links.prev) across services to avoid duplicated schema definitions?
1 answer
1 question comment
Use comments to ask for clarification. Post a solution as an answer.
26,525 reputation · 23 May 2021, 04:09 UTC
OpenAPI does not define a pagination model, but it does provide a spec-level way to document navigational links without vendor extensions.
In OpenAPI 3.0 and 3.1 an Operation Response can define a links object with runtime expressions for next, prev, first and last. This describes HATEOAS navigation at the operation level and keeps the response body schema focused on the resource array, which helps codegen stay clean.
If pagination is modeled in the body, a reusable component schema is the consistent approach. For RFC 5988 Link header pagination, document the header in responses[].headers.link rather than in the body schema, a frequent modeling error. Links evaluation and tooling support differ between OpenAPI 3.0 and 3.1, so verify generated clients surface the relations as intended.