这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@GreyXor
Copy link

@GreyXor GreyXor commented Mar 17, 2023

Allow to use server variables like this
image

ref: https://swagger.io/docs/specification/api-host-and-base-path/

Example PoC usage

humaRouter.ServerLink("Example server", "{scheme}://{host}:{port}/api", map[string]huma.CaServerVariable{
	"scheme": {
		Enum:        []string{"http", "https"},
		Default:     "https",
		Description: "Example description 🔥",
	},
	"host": {
		Default:     "127.0.0.1",
		Description: "IP or domain where your server is located.",
	},
	"port": {
		Default:     "1337",
		Description: "Server port.",
	},
})

@danielgtaylor
Copy link
Owner

Thanks for the contribution and sorry for not merging sooner. This is now available in the Huma v2 beta as you get full access to the OpenAPI (including setting your own extension fields as needed). See

huma/openapi.go

Lines 70 to 100 in de19e4c

// ServerVariable for server URL template substitution.
type ServerVariable struct {
// Enumeration of string values to be used if the substitution options are from a limited set. The array MUST NOT be empty.
Enum []string `yaml:"enum,omitempty"`
// Default value to use for substitution, which SHALL be sent if an alternate value is not supplied.
Default string `yaml:"default"`
// Description for the server variable. CommonMark syntax MAY be used for rich text representation.
Description string `yaml:"description,omitempty"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}
// Server URL, optionally with variables.
type Server struct {
// URL to the target host. This URL supports Server Variables and MAY be relative, to indicate that the host location is relative to the location where the OpenAPI document is being served. Variable substitutions will be made when a variable is named in {brackets}.
URL string `yaml:"url"`
// Description of the host designated by the URL. CommonMark syntax MAY be used for rich text representation.
Description string `yaml:"description,omitempty"`
// Variables map between a variable name and its value. The value is used for substitution in the server’s URL template.
Variables map[string]*ServerVariable `yaml:"variables,omitempty"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants