AshOaskit.ResponseLinks (AshOasKit v0.2.0)

View Source

Generates JSON:API response links schemas for OpenAPI specifications.

This module provides functions to build link objects for JSON:API responses, including self links, related links, and pagination links (first, last, prev, next). Links are essential for HATEOAS compliance and enable clients to navigate the API without hardcoding URLs.

Every resource and collection response should include a self link pointing to the canonical URL for that resource or collection.

Relationship objects include related links that point to the related resource collection endpoint.

Collection responses include pagination links:

  • first - URL to the first page of results
  • last - URL to the last page of results
  • prev - URL to the previous page (null if on first page)
  • next - URL to the next page (null if on last page)

OpenAPI Version Differences

  • OpenAPI 3.1: Uses type: ["string", "null"] for nullable fields
  • OpenAPI 3.0: Uses type: "string" with nullable: true

Usage

# Build a resource links schema
AshOaskit.ResponseLinks.build_resource_links_schema(version: "3.1")

# Build pagination links schema
AshOaskit.ResponseLinks.build_pagination_links_schema(version: "3.1")

# Build relationship links schema
AshOaskit.ResponseLinks.build_relationship_links_schema(version: "3.1")
{
  "links": {
    "self": "https://api.example.com/posts/1",
    "related": "https://api.example.com/posts/1/comments"
  }
}

For paginated collections:

{
  "links": {
    "self": "https://api.example.com/posts?page[offset]=20&page[limit]=10",
    "first": "https://api.example.com/posts?page[offset]=0&page[limit]=10",
    "last": "https://api.example.com/posts?page[offset]=90&page[limit]=10",
    "prev": "https://api.example.com/posts?page[offset]=10&page[limit]=10",
    "next": "https://api.example.com/posts?page[offset]=30&page[limit]=10"
  }
}

Summary

Functions

Adds links schema to an existing response schema.

Builds a links schema for collection responses with pagination.

Builds a top-level document links schema.

Builds a comprehensive links schema that can contain any valid link type.

Builds a link object schema (for when links themselves have objects as values).

Generates a named links schema reference for use in components.

Builds a nullable link object schema.

Builds a links schema specifically for pagination.

Builds a links schema for relationship objects.

Builds a complete links schema for resource responses.

Checks if a route type should include pagination links.

Functions

paginated_route?(route_type)

@spec paginated_route?(atom()) :: boolean()

Checks if a route type should include pagination links.

Examples

iex> AshOaskit.ResponseLinks.paginated_route?(:index)
true

iex> AshOaskit.ResponseLinks.paginated_route?(:get)
false