AshOaskit. TagBuilder
(AshOasKit v0.2.1)
View Source
Builds OpenAPI tags for organizing operations in the specification.
This module provides functions to generate tags and assign operations to tags based on different grouping strategies. Tags help organize operations in documentation tools like Swagger UI and Redoc.
Grouping Strategies
By Resource (Default)
Groups operations by the Ash resource they operate on:
Posts:
- GET /posts
- POST /posts
- GET /posts/{id}
Comments:
- GET /comments
- POST /commentsBy Domain
Groups operations by the Ash domain they belong to:
Blog:
- GET /posts
- POST /posts
- GET /comments
Shop:
- GET /products
- POST /ordersBy Custom Tag
Uses custom tags defined in the resource or domain configuration.
Configuration
Tag configuration can come from:
AshJsonApi.Domain.Info.tag/1- Domain-level tag overrideAshJsonApi.Domain.Info.group_by/1- Grouping strategy- Resource name (default fallback)
Usage
# Build tags grouped by resource
AshOaskit.TagBuilder.build_tags(domains, group_by: :resource)
# Build tags grouped by domain
AshOaskit.TagBuilder.build_tags(domains, group_by: :domain)
# Get tag for an operation
AshOaskit.TagBuilder.operation_tag(route, group_by: :resource)
Summary
Functions
Builds tags based on custom configuration.
Builds tags based on domains.
Builds tags based on resources (default grouping).
Builds a tag object with optional description and external docs.
Builds tags for the OpenAPI specification.
Builds tags with external documentation links.
Extracts the tag name from a domain module.
Gets the default grouping strategy for the given domains.
Merges custom tags with generated tags.
Gets the tag name for an operation based on the route.
Gets tags for an operation (returns as a list for OpenAPI operation tags).
Extracts the tag name from a resource module.
Functions
Builds tags based on custom configuration.
Uses tag configuration from AshJsonApi domain info if available,
falls back to domain name.
Examples
iex> AshOaskit.TagBuilder.build_custom_tags([MyApp.Blog])
[%{name: "Custom Tag Name"}]
Builds tags based on domains.
Each domain becomes a tag, and all resources in that domain share the tag.
Examples
iex> AshOaskit.TagBuilder.build_domain_tags([MyApp.Blog, MyApp.Shop])
[
%{name: "Blog", description: "Blog domain operations"},
%{name: "Shop", description: "Shop domain operations"}
]
Builds tags based on resources (default grouping).
Each resource becomes a tag.
Examples
iex> AshOaskit.TagBuilder.build_resource_tags([MyApp.Blog])
[%{name: "Post"}, %{name: "Comment"}]
Builds a tag object with optional description and external docs.
Options
:external_docs- External documentation URL and description.
Examples
iex> AshOaskit.TagBuilder.build_tag("Posts", "Operations for blog posts")
%{name: "Posts", description: "Operations for blog posts"}
iex> AshOaskit.TagBuilder.build_tag("Posts", nil,
...> external_docs: %{url: "https://docs.example.com"}
...> )
%{name: "Posts", externalDocs: %{url: "https://docs.example.com"}}
Builds tags for the OpenAPI specification.
Options
:group_by- Grouping strategy::resource,:domain, or:custom. Defaults to checking domain configuration, then:resource.:include_descriptions- Whether to include tag descriptions. Defaults to true.
Examples
iex> AshOaskit.TagBuilder.build_tags([MyApp.Blog], group_by: :resource)
[
%{name: "Post", description: "Operations on Post resources"},
%{name: "Comment", description: "Operations on Comment resources"}
]
iex> AshOaskit.TagBuilder.build_tags([MyApp.Blog, MyApp.Shop], group_by: :domain)
[
%{name: "Blog", description: "Blog domain operations"},
%{name: "Shop", description: "Shop domain operations"}
]
Builds tags with external documentation links.
Options
:base_url- Base URL for external documentation.:group_by- Grouping strategy.
Examples
iex> AshOaskit.TagBuilder.build_tags_with_docs([MyApp.Blog],
...> base_url: "https://docs.example.com"
...> )
[
%{
name: "Post",
description: "Operations on Post resources",
externalDocs: %{url: "https://docs.example.com/post"}
}
]
Extracts the tag name from a domain module.
Examples
iex> AshOaskit.TagBuilder.domain_tag_name(MyApp.Blog)
"Blog"
Gets the default grouping strategy for the given domains.
Checks the first domain's configuration for group_by setting.
Defaults to :resource.
Examples
iex> AshOaskit.TagBuilder.get_default_grouping([MyApp.Blog])
:resource
Merges custom tags with generated tags.
Custom tags take precedence over generated tags with the same name.
Examples
iex> generated = [%{name: "Posts"}, %{name: "Comments"}]
...> custom = [%{name: "Posts", description: "Custom description"}]
...> AshOaskit.TagBuilder.merge_tags(generated, custom)
[%{name: "Posts", description: "Custom description"}, %{name: "Comments"}]
Gets the tag name for an operation based on the route.
Options
:group_by- Grouping strategy::resourceor:domain. Defaults to:resource.
Examples
iex> route = %{resource: MyApp.Blog.Post}
...> AshOaskit.TagBuilder.operation_tag(route, group_by: :resource)
"Post"
iex> route = %{resource: MyApp.Blog.Post}
...> AshOaskit.TagBuilder.operation_tag(route, group_by: :domain)
"Blog"
Gets tags for an operation (returns as a list for OpenAPI operation tags).
Examples
iex> route = %{resource: MyApp.Blog.Post}
...> AshOaskit.TagBuilder.operation_tags(route)
["Post"]
Extracts the tag name from a resource module.
Returns the last segment of the module name in PascalCase for human-readable tag names.
Examples
iex> AshOaskit.TagBuilder.resource_tag_name(MyApp.Blog.Post)
"Post"