mirror of
https://github.com/Fishwaldo/huma.git
synced 2025-03-15 19:31:27 +00:00
chore: make param location an enum
This commit is contained in:
parent
6eacc7a29e
commit
dc85cf12cb
3 changed files with 37 additions and 27 deletions
34
openapi.go
34
openapi.go
|
@ -10,15 +10,25 @@ import (
|
|||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ParamLocation describes where in the HTTP request the parameter comes from.
|
||||
type ParamLocation string
|
||||
|
||||
// Parameter locations supported by OpenAPI 3
|
||||
const (
|
||||
InPath ParamLocation = "path"
|
||||
InQuery ParamLocation = "query"
|
||||
InHeader ParamLocation = "header"
|
||||
)
|
||||
|
||||
// Param describes an OpenAPI 3 parameter
|
||||
type Param struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description,omitempty"`
|
||||
In string `json:"in"`
|
||||
Required bool `json:"required,omitempty"`
|
||||
Schema *Schema `json:"schema,omitempty"`
|
||||
Deprecated bool `json:"deprecated,omitempty"`
|
||||
Example interface{} `json:"example,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description,omitempty"`
|
||||
In ParamLocation `json:"in"`
|
||||
Required bool `json:"required,omitempty"`
|
||||
Schema *Schema `json:"schema,omitempty"`
|
||||
Deprecated bool `json:"deprecated,omitempty"`
|
||||
Example interface{} `json:"example,omitempty"`
|
||||
|
||||
// Internal params are excluded from the OpenAPI document and can set up
|
||||
// params sent between a load balander / proxy and the service internally.
|
||||
|
@ -37,7 +47,7 @@ func PathParamExample(name string, description string, example interface{}, sche
|
|||
p := &Param{
|
||||
Name: name,
|
||||
Description: description,
|
||||
In: "path",
|
||||
In: InPath,
|
||||
Required: true,
|
||||
Example: example,
|
||||
}
|
||||
|
@ -59,7 +69,7 @@ func QueryParamExample(name string, description string, defaultValue interface{}
|
|||
p := &Param{
|
||||
Name: name,
|
||||
Description: description,
|
||||
In: "query",
|
||||
In: InQuery,
|
||||
Example: example,
|
||||
def: defaultValue,
|
||||
}
|
||||
|
@ -76,7 +86,7 @@ func QueryParamInternal(name string, description string, defaultValue interface{
|
|||
return &Param{
|
||||
Name: name,
|
||||
Description: description,
|
||||
In: "query",
|
||||
In: InQuery,
|
||||
internal: true,
|
||||
def: defaultValue,
|
||||
}
|
||||
|
@ -92,7 +102,7 @@ func HeaderParamExample(name string, description string, defaultValue interface{
|
|||
p := &Param{
|
||||
Name: name,
|
||||
Description: description,
|
||||
In: "header",
|
||||
In: InHeader,
|
||||
Example: example,
|
||||
def: defaultValue,
|
||||
}
|
||||
|
@ -109,7 +119,7 @@ func HeaderParamInternal(name string, description string, defaultValue interface
|
|||
return &Param{
|
||||
Name: name,
|
||||
Description: description,
|
||||
In: "header",
|
||||
In: InHeader,
|
||||
internal: true,
|
||||
def: defaultValue,
|
||||
}
|
||||
|
|
|
@ -16,23 +16,23 @@ var paramFuncsTable = []struct {
|
|||
param *Param
|
||||
name string
|
||||
description string
|
||||
in string
|
||||
in ParamLocation
|
||||
required bool
|
||||
internal bool
|
||||
def interface{}
|
||||
example interface{}
|
||||
}{
|
||||
{"PathParam", PathParam("test", "desc"), "test", "desc", "path", true, false, nil, nil},
|
||||
{"PathParamSchema", PathParam("test", "desc", &Schema{}), "test", "desc", "path", true, false, nil, nil},
|
||||
{"PathParamExample", PathParamExample("test", "desc", 123), "test", "desc", "path", true, false, nil, 123},
|
||||
{"QueryParam", QueryParam("test", "desc", "def"), "test", "desc", "query", false, false, "def", nil},
|
||||
{"QueryParamSchema", QueryParam("test", "desc", "def", &Schema{}), "test", "desc", "query", false, false, "def", nil},
|
||||
{"QueryParamExample", QueryParamExample("test", "desc", "def", "foo"), "test", "desc", "query", false, false, "def", "foo"},
|
||||
{"QueryParamInternal", QueryParamInternal("test", "desc", "def"), "test", "desc", "query", false, true, "def", nil},
|
||||
{"HeaderParam", HeaderParam("test", "desc", "def"), "test", "desc", "header", false, false, "def", nil},
|
||||
{"HeaderParamSchema", HeaderParam("test", "desc", "def", &Schema{}), "test", "desc", "header", false, false, "def", nil},
|
||||
{"HeaderParamExample", HeaderParamExample("test", "desc", "def", "foo"), "test", "desc", "header", false, false, "def", "foo"},
|
||||
{"HeaderParamInternal", HeaderParamInternal("test", "desc", "def"), "test", "desc", "header", false, true, "def", nil},
|
||||
{"PathParam", PathParam("test", "desc"), "test", "desc", InPath, true, false, nil, nil},
|
||||
{"PathParamSchema", PathParam("test", "desc", &Schema{}), "test", "desc", InPath, true, false, nil, nil},
|
||||
{"PathParamExample", PathParamExample("test", "desc", 123), "test", "desc", InPath, true, false, nil, 123},
|
||||
{"QueryParam", QueryParam("test", "desc", "def"), "test", "desc", InQuery, false, false, "def", nil},
|
||||
{"QueryParamSchema", QueryParam("test", "desc", "def", &Schema{}), "test", "desc", InQuery, false, false, "def", nil},
|
||||
{"QueryParamExample", QueryParamExample("test", "desc", "def", "foo"), "test", "desc", InQuery, false, false, "def", "foo"},
|
||||
{"QueryParamInternal", QueryParamInternal("test", "desc", "def"), "test", "desc", InQuery, false, true, "def", nil},
|
||||
{"HeaderParam", HeaderParam("test", "desc", "def"), "test", "desc", InHeader, false, false, "def", nil},
|
||||
{"HeaderParamSchema", HeaderParam("test", "desc", "def", &Schema{}), "test", "desc", InHeader, false, false, "def", nil},
|
||||
{"HeaderParamExample", HeaderParamExample("test", "desc", "def", "foo"), "test", "desc", InHeader, false, false, "def", "foo"},
|
||||
{"HeaderParamInternal", HeaderParamInternal("test", "desc", "def"), "test", "desc", InHeader, false, true, "def", nil},
|
||||
}
|
||||
|
||||
func TestParamFuncs(outer *testing.T) {
|
||||
|
|
|
@ -53,14 +53,14 @@ func validAgainstSchema(c *gin.Context, schema *Schema, data []byte) bool {
|
|||
func getParamValue(c *gin.Context, param *Param) (interface{}, bool) {
|
||||
var pstr string
|
||||
switch param.In {
|
||||
case "path":
|
||||
case InPath:
|
||||
pstr = c.Param(param.Name)
|
||||
case "query":
|
||||
case InQuery:
|
||||
pstr = c.Query(param.Name)
|
||||
if pstr == "" {
|
||||
return param.def, true
|
||||
}
|
||||
case "header":
|
||||
case InHeader:
|
||||
pstr = c.GetHeader(param.Name)
|
||||
if pstr == "" {
|
||||
return param.def, true
|
||||
|
|
Loading…
Add table
Reference in a new issue