diff --git a/internal/common_context.go b/internal/common_context.go index 48f1a1fd..92e8b54e 100644 --- a/internal/common_context.go +++ b/internal/common_context.go @@ -36,6 +36,7 @@ type OpenAPIParam struct { Required bool Nullable bool + Enum []any } // CommonContext is a base context shared by all adaptors (net/http, gin, echo, etc...) diff --git a/option.go b/option.go index b80bab02..8ab2b93f 100644 --- a/option.go +++ b/option.go @@ -280,6 +280,13 @@ func buildParam(name string, options ...ParamOption) (OpenAPIParam, *openapi3.Pa if param.Required { openapiParam.Required = param.Required } + + // construct enum + for _, enumValue := range param.Enum { + panicsIfNotCorrectType(openapiParam, enumValue) + } + openapiParam.Schema.Value.Enum = param.Enum + for name, exampleValue := range param.Examples { if openapiParam.Examples == nil { openapiParam.Examples = make(openapi3.Examples) diff --git a/param.go b/param.go index 8c4db3a1..0f6f905d 100644 --- a/param.go +++ b/param.go @@ -56,6 +56,13 @@ func ParamExample(exampleName string, value any) ParamOption { } } +// ParamEnum sets the allowed values for the parameter. +func ParamEnum(values ...any) ParamOption { + return func(param *OpenAPIParam) { + param.Enum = values + } +} + // ParamStatusCodes sets the status codes for which this parameter is required. // Only used for response parameters. // If empty, it is required for 200 status codes. diff --git a/param/param.go b/param/param.go index 3ad56671..03ade881 100644 --- a/param/param.go +++ b/param/param.go @@ -31,6 +31,9 @@ var Default = fuego.ParamDefault // Example adds an example to the parameter. As per the OpenAPI 3.0 standard, the example must be given a name. var Example = fuego.ParamExample +// Enum sets the allowed values for the parameter. +var Enum = fuego.ParamEnum + // StatusCodes sets the status codes for which this parameter is required. // Only used for response parameters. // If empty, it is required for 200 status codes.