From 0c811ff4bfd83c64ad9cc0c7b12756e3ee22453e Mon Sep 17 00:00:00 2001 From: GreyXor Date: Fri, 17 Mar 2023 17:09:50 +0100 Subject: [PATCH] feat: add server variables for server URL template substitution --- examples/bookstore/bookstore.go | 2 +- examples/notes/notes.go | 2 +- openapi.go | 12 ++++++++++-- router.go | 3 ++- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/examples/bookstore/bookstore.go b/examples/bookstore/bookstore.go index 99af1d6b..b2557fc5 100644 --- a/examples/bookstore/bookstore.go +++ b/examples/bookstore/bookstore.go @@ -60,7 +60,7 @@ var memoryDB = sync.Map{} func main() { // Create a new router and give our API a title and version. app := cli.NewRouter("BookStore API", "1.0.0") - app.ServerLink("Development server", "http://localhost:8888") + app.ServerLink("Development server", "http://localhost:8888", map[string]huma.CaServerVariable{}) genres := app.Resource("/v1/genres") genres.Get("list-genres", "Returns a list of all genres", diff --git a/examples/notes/notes.go b/examples/notes/notes.go index 8b66dc54..b54ca58f 100644 --- a/examples/notes/notes.go +++ b/examples/notes/notes.go @@ -36,7 +36,7 @@ var memoryDB = sync.Map{} func main() { // Create a new router and give our API a title and version. app := cli.NewRouter("Notes API", "1.0.0") - app.ServerLink("Development server", "http://localhost:8888") + app.ServerLink("Development server", "http://localhost:8888", map[string]huma.CaServerVariable{}) notes := app.Resource("/v1/notes") notes.Get("list-notes", "Returns a list of all notes", diff --git a/openapi.go b/openapi.go index 50a74f3a..a72bfe9e 100644 --- a/openapi.go +++ b/openapi.go @@ -16,8 +16,16 @@ type oaContact struct { // oaServer describes an OpenAPI 3 API server location type oaServer struct { - URL string `json:"url"` - Description string `json:"description,omitempty"` + URL string `json:"url"` + Description string `json:"description,omitempty"` + Variables map[string]CaServerVariable `json:"variables,omitempty"` +} + +// CaServerVariable describe a server variable for server URL template substitution. +type CaServerVariable struct { + Enum []string `json:"enum,omitempty"` + Default string `json:"default,omitempty"` + Description string `json:"description,omitempty"` } // paramLocation describes where in the HTTP request the parameter comes from. diff --git a/router.go b/router.go index e6ccfa9f..e4319dcf 100644 --- a/router.go +++ b/router.go @@ -144,10 +144,11 @@ func (r *Router) Contact(name, email, url string) { } // ServerLink adds a new server link to this router for documentation. -func (r *Router) ServerLink(description, uri string) { +func (r *Router) ServerLink(description, uri string, variables map[string]CaServerVariable) { r.servers = append(r.servers, oaServer{ Description: description, URL: uri, + Variables: variables, }) }