forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/go-openapi/spec/parameter.go

566 lines
11 KiB
Go
Raw Normal View History

2024-05-14 13:07:09 +00:00
// Copyright 2015 go-swagger maintainers
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
2024-05-14 13:07:09 +00:00
// you may not use this file except in compliance with the License.
2024-05-14 13:07:09 +00:00
// You may obtain a copy of the License at
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Unless required by applicable law or agreed to in writing, software
2024-05-14 13:07:09 +00:00
// distributed under the License is distributed on an "AS IS" BASIS,
2024-05-14 13:07:09 +00:00
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2024-05-14 13:07:09 +00:00
// See the License for the specific language governing permissions and
2024-05-14 13:07:09 +00:00
// limitations under the License.
package spec
import (
"encoding/json"
"strings"
"github.com/go-openapi/jsonpointer"
"github.com/go-openapi/swag"
)
// QueryParam creates a query parameter
2024-05-14 13:07:09 +00:00
func QueryParam(name string) *Parameter {
2024-05-14 13:07:09 +00:00
return &Parameter{ParamProps: ParamProps{Name: name, In: "query"}}
2024-05-14 13:07:09 +00:00
}
// HeaderParam creates a header parameter, this is always required by default
2024-05-14 13:07:09 +00:00
func HeaderParam(name string) *Parameter {
2024-05-14 13:07:09 +00:00
return &Parameter{ParamProps: ParamProps{Name: name, In: "header", Required: true}}
2024-05-14 13:07:09 +00:00
}
// PathParam creates a path parameter, this is always required
2024-05-14 13:07:09 +00:00
func PathParam(name string) *Parameter {
2024-05-14 13:07:09 +00:00
return &Parameter{ParamProps: ParamProps{Name: name, In: "path", Required: true}}
2024-05-14 13:07:09 +00:00
}
// BodyParam creates a body parameter
2024-05-14 13:07:09 +00:00
func BodyParam(name string, schema *Schema) *Parameter {
2024-05-14 13:07:09 +00:00
return &Parameter{ParamProps: ParamProps{Name: name, In: "body", Schema: schema}}
2024-05-14 13:07:09 +00:00
}
// FormDataParam creates a body parameter
2024-05-14 13:07:09 +00:00
func FormDataParam(name string) *Parameter {
2024-05-14 13:07:09 +00:00
return &Parameter{ParamProps: ParamProps{Name: name, In: "formData"}}
2024-05-14 13:07:09 +00:00
}
// FileParam creates a body parameter
2024-05-14 13:07:09 +00:00
func FileParam(name string) *Parameter {
2024-05-14 13:07:09 +00:00
return &Parameter{ParamProps: ParamProps{Name: name, In: "formData"},
2024-05-14 13:07:09 +00:00
SimpleSchema: SimpleSchema{Type: "file"}}
2024-05-14 13:07:09 +00:00
}
// SimpleArrayParam creates a param for a simple array (string, int, date etc)
2024-05-14 13:07:09 +00:00
func SimpleArrayParam(name, tpe, fmt string) *Parameter {
2024-05-14 13:07:09 +00:00
return &Parameter{ParamProps: ParamProps{Name: name},
2024-05-14 13:07:09 +00:00
SimpleSchema: SimpleSchema{Type: jsonArray, CollectionFormat: "csv",
2024-05-14 13:07:09 +00:00
Items: &Items{SimpleSchema: SimpleSchema{Type: tpe, Format: fmt}}}}
2024-05-14 13:07:09 +00:00
}
// ParamRef creates a parameter that's a json reference
2024-05-14 13:07:09 +00:00
func ParamRef(uri string) *Parameter {
2024-05-14 13:07:09 +00:00
p := new(Parameter)
2024-05-14 13:07:09 +00:00
p.Ref = MustCreateRef(uri)
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// ParamProps describes the specific attributes of an operation parameter
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// NOTE:
2024-05-14 13:07:09 +00:00
// - Schema is defined when "in" == "body": see validate
2024-05-14 13:07:09 +00:00
// - AllowEmptyValue is allowed where "in" == "query" || "formData"
2024-05-14 13:07:09 +00:00
type ParamProps struct {
Description string `json:"description,omitempty"`
Name string `json:"name,omitempty"`
In string `json:"in,omitempty"`
Required bool `json:"required,omitempty"`
Schema *Schema `json:"schema,omitempty"`
AllowEmptyValue bool `json:"allowEmptyValue,omitempty"`
2024-05-14 13:07:09 +00:00
}
// Parameter a unique parameter is defined by a combination of a [name](#parameterName) and [location](#parameterIn).
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// There are five possible parameter types.
2024-05-14 13:07:09 +00:00
// - Path - Used together with [Path Templating](#pathTemplating), where the parameter value is actually part
2024-05-14 13:07:09 +00:00
// of the operation's URL. This does not include the host or base path of the API. For example, in `/items/{itemId}`,
2024-05-14 13:07:09 +00:00
// the path parameter is `itemId`.
2024-05-14 13:07:09 +00:00
// - Query - Parameters that are appended to the URL. For example, in `/items?id=###`, the query parameter is `id`.
2024-05-14 13:07:09 +00:00
// - Header - Custom headers that are expected as part of the request.
2024-05-14 13:07:09 +00:00
// - Body - The payload that's appended to the HTTP request. Since there can only be one payload, there can only be
2024-05-14 13:07:09 +00:00
// _one_ body parameter. The name of the body parameter has no effect on the parameter itself and is used for
2024-05-14 13:07:09 +00:00
// documentation purposes only. Since Form parameters are also in the payload, body and form parameters cannot exist
2024-05-14 13:07:09 +00:00
// together for the same operation.
2024-05-14 13:07:09 +00:00
// - Form - Used to describe the payload of an HTTP request when either `application/x-www-form-urlencoded` or
2024-05-14 13:07:09 +00:00
// `multipart/form-data` are used as the content type of the request (in Swagger's definition,
2024-05-14 13:07:09 +00:00
// the [`consumes`](#operationConsumes) property of an operation). This is the only parameter type that can be used
2024-05-14 13:07:09 +00:00
// to send files, thus supporting the `file` type. Since form parameters are sent in the payload, they cannot be
2024-05-14 13:07:09 +00:00
// declared together with a body parameter for the same operation. Form parameters have a different format based on
2024-05-14 13:07:09 +00:00
// the content-type used (for further details, consult http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4).
2024-05-14 13:07:09 +00:00
// - `application/x-www-form-urlencoded` - Similar to the format of Query parameters but as a payload.
2024-05-14 13:07:09 +00:00
// For example, `foo=1&bar=swagger` - both `foo` and `bar` are form parameters. This is normally used for simple
2024-05-14 13:07:09 +00:00
// parameters that are being transferred.
2024-05-14 13:07:09 +00:00
// - `multipart/form-data` - each parameter takes a section in the payload with an internal header.
2024-05-14 13:07:09 +00:00
// For example, for the header `Content-Disposition: form-data; name="submit-name"` the name of the parameter is
2024-05-14 13:07:09 +00:00
// `submit-name`. This type of form parameters is more commonly used for file transfers.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// For more information: http://goo.gl/8us55a#parameterObject
2024-05-14 13:07:09 +00:00
type Parameter struct {
Refable
2024-05-14 13:07:09 +00:00
CommonValidations
2024-05-14 13:07:09 +00:00
SimpleSchema
2024-05-14 13:07:09 +00:00
VendorExtensible
2024-05-14 13:07:09 +00:00
ParamProps
}
// JSONLookup look up a value by the json property name
2024-05-14 13:07:09 +00:00
func (p Parameter) JSONLookup(token string) (interface{}, error) {
2024-05-14 13:07:09 +00:00
if ex, ok := p.Extensions[token]; ok {
2024-05-14 13:07:09 +00:00
return &ex, nil
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if token == jsonRef {
2024-05-14 13:07:09 +00:00
return &p.Ref, nil
2024-05-14 13:07:09 +00:00
}
r, _, err := jsonpointer.GetForToken(p.CommonValidations, token)
2024-05-14 13:07:09 +00:00
if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if r != nil {
2024-05-14 13:07:09 +00:00
return r, nil
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
r, _, err = jsonpointer.GetForToken(p.SimpleSchema, token)
2024-05-14 13:07:09 +00:00
if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if r != nil {
2024-05-14 13:07:09 +00:00
return r, nil
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
r, _, err = jsonpointer.GetForToken(p.ParamProps, token)
2024-05-14 13:07:09 +00:00
return r, err
2024-05-14 13:07:09 +00:00
}
// WithDescription a fluent builder method for the description of the parameter
2024-05-14 13:07:09 +00:00
func (p *Parameter) WithDescription(description string) *Parameter {
2024-05-14 13:07:09 +00:00
p.Description = description
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// Named a fluent builder method to override the name of the parameter
2024-05-14 13:07:09 +00:00
func (p *Parameter) Named(name string) *Parameter {
2024-05-14 13:07:09 +00:00
p.Name = name
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// WithLocation a fluent builder method to override the location of the parameter
2024-05-14 13:07:09 +00:00
func (p *Parameter) WithLocation(in string) *Parameter {
2024-05-14 13:07:09 +00:00
p.In = in
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// Typed a fluent builder method for the type of the parameter value
2024-05-14 13:07:09 +00:00
func (p *Parameter) Typed(tpe, format string) *Parameter {
2024-05-14 13:07:09 +00:00
p.Type = tpe
2024-05-14 13:07:09 +00:00
p.Format = format
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// CollectionOf a fluent builder method for an array parameter
2024-05-14 13:07:09 +00:00
func (p *Parameter) CollectionOf(items *Items, format string) *Parameter {
2024-05-14 13:07:09 +00:00
p.Type = jsonArray
2024-05-14 13:07:09 +00:00
p.Items = items
2024-05-14 13:07:09 +00:00
p.CollectionFormat = format
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// WithDefault sets the default value on this parameter
2024-05-14 13:07:09 +00:00
func (p *Parameter) WithDefault(defaultValue interface{}) *Parameter {
2024-05-14 13:07:09 +00:00
p.AsOptional() // with default implies optional
2024-05-14 13:07:09 +00:00
p.Default = defaultValue
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// AllowsEmptyValues flags this parameter as being ok with empty values
2024-05-14 13:07:09 +00:00
func (p *Parameter) AllowsEmptyValues() *Parameter {
2024-05-14 13:07:09 +00:00
p.AllowEmptyValue = true
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// NoEmptyValues flags this parameter as not liking empty values
2024-05-14 13:07:09 +00:00
func (p *Parameter) NoEmptyValues() *Parameter {
2024-05-14 13:07:09 +00:00
p.AllowEmptyValue = false
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// AsOptional flags this parameter as optional
2024-05-14 13:07:09 +00:00
func (p *Parameter) AsOptional() *Parameter {
2024-05-14 13:07:09 +00:00
p.Required = false
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// AsRequired flags this parameter as required
2024-05-14 13:07:09 +00:00
func (p *Parameter) AsRequired() *Parameter {
2024-05-14 13:07:09 +00:00
if p.Default != nil { // with a default required makes no sense
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
p.Required = true
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// WithMaxLength sets a max length value
2024-05-14 13:07:09 +00:00
func (p *Parameter) WithMaxLength(max int64) *Parameter {
2024-05-14 13:07:09 +00:00
p.MaxLength = &max
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// WithMinLength sets a min length value
2024-05-14 13:07:09 +00:00
func (p *Parameter) WithMinLength(min int64) *Parameter {
2024-05-14 13:07:09 +00:00
p.MinLength = &min
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// WithPattern sets a pattern value
2024-05-14 13:07:09 +00:00
func (p *Parameter) WithPattern(pattern string) *Parameter {
2024-05-14 13:07:09 +00:00
p.Pattern = pattern
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// WithMultipleOf sets a multiple of value
2024-05-14 13:07:09 +00:00
func (p *Parameter) WithMultipleOf(number float64) *Parameter {
2024-05-14 13:07:09 +00:00
p.MultipleOf = &number
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// WithMaximum sets a maximum number value
2024-05-14 13:07:09 +00:00
func (p *Parameter) WithMaximum(max float64, exclusive bool) *Parameter {
2024-05-14 13:07:09 +00:00
p.Maximum = &max
2024-05-14 13:07:09 +00:00
p.ExclusiveMaximum = exclusive
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// WithMinimum sets a minimum number value
2024-05-14 13:07:09 +00:00
func (p *Parameter) WithMinimum(min float64, exclusive bool) *Parameter {
2024-05-14 13:07:09 +00:00
p.Minimum = &min
2024-05-14 13:07:09 +00:00
p.ExclusiveMinimum = exclusive
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// WithEnum sets a the enum values (replace)
2024-05-14 13:07:09 +00:00
func (p *Parameter) WithEnum(values ...interface{}) *Parameter {
2024-05-14 13:07:09 +00:00
p.Enum = append([]interface{}{}, values...)
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// WithMaxItems sets the max items
2024-05-14 13:07:09 +00:00
func (p *Parameter) WithMaxItems(size int64) *Parameter {
2024-05-14 13:07:09 +00:00
p.MaxItems = &size
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// WithMinItems sets the min items
2024-05-14 13:07:09 +00:00
func (p *Parameter) WithMinItems(size int64) *Parameter {
2024-05-14 13:07:09 +00:00
p.MinItems = &size
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// UniqueValues dictates that this array can only have unique items
2024-05-14 13:07:09 +00:00
func (p *Parameter) UniqueValues() *Parameter {
2024-05-14 13:07:09 +00:00
p.UniqueItems = true
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// AllowDuplicates this array can have duplicates
2024-05-14 13:07:09 +00:00
func (p *Parameter) AllowDuplicates() *Parameter {
2024-05-14 13:07:09 +00:00
p.UniqueItems = false
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// WithValidations is a fluent method to set parameter validations
2024-05-14 13:07:09 +00:00
func (p *Parameter) WithValidations(val CommonValidations) *Parameter {
2024-05-14 13:07:09 +00:00
p.SetValidations(SchemaValidations{CommonValidations: val})
2024-05-14 13:07:09 +00:00
return p
2024-05-14 13:07:09 +00:00
}
// UnmarshalJSON hydrates this items instance with the data from JSON
2024-05-14 13:07:09 +00:00
func (p *Parameter) UnmarshalJSON(data []byte) error {
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(data, &p.CommonValidations); err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(data, &p.Refable); err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(data, &p.SimpleSchema); err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(data, &p.VendorExtensible); err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return json.Unmarshal(data, &p.ParamProps)
2024-05-14 13:07:09 +00:00
}
// MarshalJSON converts this items object to JSON
2024-05-14 13:07:09 +00:00
func (p Parameter) MarshalJSON() ([]byte, error) {
2024-05-14 13:07:09 +00:00
b1, err := json.Marshal(p.CommonValidations)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
b2, err := json.Marshal(p.SimpleSchema)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
b3, err := json.Marshal(p.Refable)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
b4, err := json.Marshal(p.VendorExtensible)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
b5, err := json.Marshal(p.ParamProps)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return swag.ConcatJSON(b3, b1, b2, b4, b5), nil
2024-05-14 13:07:09 +00:00
}