AshOaskit.Router.Plug (AshOasKit v0.2.1)

View Source

Plug for serving OpenAPI specifications via the Router macro.

This plug is used internally by AshOaskit.Router to handle requests for OpenAPI specifications. It reads configuration from conn.private.ash_oaskit.

Direct Usage (Advanced)

While typically used via AshOaskit.Router, you can use this plug directly:

# In your router
get "/openapi.json", AshOaskit.Router.Plug, :call,
  private: %{
    ash_oaskit: %{
      domains: [MyApp.Blog],
      title: "My API",
      version: "1.0.0",
      openapi_version: "3.1",
      format: :json
    }
  }

Configuration

The plug reads these keys from conn.private.ash_oaskit:

  • :domains - List of Ash domains to include (required)
  • :title - API title (default: "API")
  • :version - API version string (default: "1.0.0")
  • :description - API description (optional)
  • :openapi_version - OpenAPI version: "3.0" or "3.1" (default: "3.1")
  • :format - Output format: :json or :yaml (default: :json)
  • :servers - List of server URLs or objects (optional)
  • :spec_builder - Custom SpecBuilder module (default: AshOaskit.SpecBuilder.Default)

Summary

Functions

Generate OpenAPI spec from configuration map.

Functions

generate_spec(config)

@spec generate_spec(map()) :: map()

Generate OpenAPI spec from configuration map.

Uses the configured spec_builder module if provided, otherwise falls back to AshOaskit.SpecBuilder.Default.

Examples

config = %{
  domains: [MyApp.Blog],
  title: "My API",
  openapi_version: "3.1"
}
spec = AshOaskit.Router.Plug.generate_spec(config)

# With custom spec_builder
config = %{
  domains: [MyApp.Blog],
  spec_builder: MyApp.CustomSpecBuilder,
  openapi_version: "3.1"
}
spec = AshOaskit.Router.Plug.generate_spec(config)