Tag Groups & Extensions
These fields are on
masterand planned for v1.4.0; they are not in a tagged release yet (go get github.com/gopackx/open-swag-go@master).
OpenAPI lets any object carry vendor-specific keys that start with x- (specification extensions). open-swag-go exposes the common ones as typed fields (Config.TagGroups, Tag.DisplayName), lets you attach arbitrary x-* keys to the root, info, tag and operation objects, and offers Config.PostBuild for anything the config cannot express.
When none of these fields are set, the generated spec is byte-for-byte the same as before.
Tag Groups
Config.TagGroups groups tags into sidebar sections. It is emitted as the root-level x-tagGroups extension, which Scalar renders. Each group lists tag names.
type TagGroup struct {
Name string // Section heading in the sidebar
Tags []string // Tag names (Tag.Name / Endpoint.Tags) in this section
}cfg := openswag.Config{
Info: openswag.Info{Title: "My API", Version: "1.0.0"},
Tags: []openswag.Tag{
{Name: "users"},
{Name: "billing"},
{Name: "admin"},
},
TagGroups: []openswag.TagGroup{
{Name: "Customers", Tags: []string{"users", "billing"}},
{Name: "Internal", Tags: []string{"admin"}},
},
}"x-tagGroups": [
{ "name": "Customers", "tags": ["users", "billing"] },
{ "name": "Internal", "tags": ["admin"] }
]Once
TagGroupsis set, Scalar may hide tags that are not listed in any group. Put every tag your endpoints use into a group.
UIConfig.TagGrouping is unrelated: it is deprecated and has no effect. Use Config.TagGroups to control sidebar sections.
Tag Display Names
Tag.DisplayName sets the label shown in the docs UI instead of the tag name. It is emitted as x-displayName on the tag. Endpoints still reference the tag by Name, so you can keep short, stable tag names in code and show friendlier labels.
Tags: []openswag.Tag{
{Name: "users", DisplayName: "User accounts"},
{Name: "billing", DisplayName: "Billing"},
},"tags": [
{ "name": "users", "x-displayName": "User accounts" },
{ "name": "billing", "x-displayName": "Billing" }
]Specification Extensions
Four places accept an Extensions map[string]any:
| Field | Emitted on |
|---|---|
Config.Extensions | The root of the document |
Info.Extensions | The info object |
Tag.Extensions | That tag in tags |
Endpoint.Extensions | That endpoint's operation object |
Rules:
- Only keys that start with
x-are emitted. Any other key is silently ignored. - Keys are written inline, as top-level keys of the object, after its fixed fields and sorted by key.
- Values are marshalled with
encoding/json, so any JSON-encodable value (strings, booleans, numbers, nested maps and slices) works. - The typed fields win: a non-empty
Config.TagGroupsreplaces anx-tagGroupsentry inConfig.Extensions, and a non-emptyTag.DisplayNamereplaces anx-displayNameentry inTag.Extensions. - The maps are copied when the spec is built, so changing a map (or a nested
map[string]any/[]any) afterwards does not change the cached spec.
docs := openswag.New(openswag.Config{
Info: openswag.Info{
Title: "My API",
Version: "1.0.0",
Extensions: map[string]any{"x-logo": map[string]any{"url": "/logo.png"}},
},
Extensions: map[string]any{"x-api-id": "my-api"},
})
docs.Add(openswag.Endpoint{
Method: "GET",
Path: "/users",
Tags: []string{"users"},
Responses: openswag.Responses{200: openswag.Response("OK")},
Extensions: map[string]any{"x-internal": true},
}){
"openapi": "3.1.0",
"info": {
"title": "My API",
"version": "1.0.0",
"x-logo": { "url": "/logo.png" }
},
"paths": {
"/users": {
"get": {
"tags": ["users"],
"operationId": "getUsers",
"responses": { "200": { "description": "OK" } },
"x-internal": true
}
}
},
"components": {},
"x-api-id": "my-api"
}open-swag-go only writes these keys into the spec; what a given x-* key does depends on the tool that reads the spec (Scalar, a code generator, a gateway).
Post-Build Hook
Config.PostBuild is called with each freshly built *spec.OpenAPI before BuildSpec caches and returns it. Use it for changes the Config cannot express, such as fields that only exist on the spec types.
PostBuild func(*spec.OpenAPI) // import "github.com/gopackx/open-swag-go/pkg/spec"import (
"time"
openswag "github.com/gopackx/open-swag-go"
"github.com/gopackx/open-swag-go/pkg/spec"
)
docs := openswag.New(openswag.Config{
Info: openswag.Info{Title: "My API", Version: "1.0.0"},
PostBuild: func(s *spec.OpenAPI) {
s.Info.Summary = "Built " + time.Now().Format(time.DateOnly)
},
})- It runs once per build: the spec is cached, and it is rebuilt (and the hook called again) only after
AddorAddAllinvalidates the cache. - It runs while the
Docslock is held, so it must not call methods on theDocs(BuildSpec,SpecJSON,Add, …); doing so would block forever. - The
spectypes carry the new fields too:OpenAPI.TagGroups([]spec.TagGroup),OpenAPI.Extensions,Info.Extensions,Tag.DisplayName,Tag.ExtensionsandOperation.Extensions, so the hook can add or change extensions as well.
Full Example
docs := openswag.New(openswag.Config{
Info: openswag.Info{
Title: "My API",
Version: "1.0.0",
Extensions: map[string]any{"x-logo": map[string]any{"url": "/logo.png"}},
},
Tags: []openswag.Tag{
{Name: "users", DisplayName: "User accounts"},
{Name: "billing", DisplayName: "Billing"},
},
TagGroups: []openswag.TagGroup{
{Name: "Customers", Tags: []string{"users", "billing"}},
},
Extensions: map[string]any{"x-api-id": "my-api"},
PostBuild: func(s *spec.OpenAPI) { // import "github.com/gopackx/open-swag-go/pkg/spec"
s.Info.Summary = "Built " + time.Now().Format(time.DateOnly)
},
})
docs.Add(openswag.Endpoint{
Method: "GET",
Path: "/users",
Tags: []string{"users"},
Responses: openswag.Responses{200: openswag.Response("OK")},
Extensions: map[string]any{"x-internal": true},
})