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

675 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 (
"bytes"
"encoding/gob"
"encoding/json"
"sort"
"github.com/go-openapi/jsonpointer"
"github.com/go-openapi/swag"
)
func init() {
2024-05-14 13:07:09 +00:00
gob.Register(map[string]interface{}{})
2024-05-14 13:07:09 +00:00
gob.Register([]interface{}{})
2024-05-14 13:07:09 +00:00
}
// OperationProps describes an operation
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// NOTES:
2024-05-14 13:07:09 +00:00
// - schemes, when present must be from [http, https, ws, wss]: see validate
2024-05-14 13:07:09 +00:00
// - Security is handled as a special case: see MarshalJSON function
2024-05-14 13:07:09 +00:00
type OperationProps struct {
Description string `json:"description,omitempty"`
Consumes []string `json:"consumes,omitempty"`
Produces []string `json:"produces,omitempty"`
Schemes []string `json:"schemes,omitempty"`
Tags []string `json:"tags,omitempty"`
Summary string `json:"summary,omitempty"`
2024-05-14 13:07:09 +00:00
ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
ID string `json:"operationId,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
Security []map[string][]string `json:"security,omitempty"`
Parameters []Parameter `json:"parameters,omitempty"`
Responses *Responses `json:"responses,omitempty"`
2024-05-14 13:07:09 +00:00
}
// MarshalJSON takes care of serializing operation properties to JSON
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// We use a custom marhaller here to handle a special cases related to
2024-05-14 13:07:09 +00:00
// the Security field. We need to preserve zero length slice
2024-05-14 13:07:09 +00:00
// while omitting the field when the value is nil/unset.
2024-05-14 13:07:09 +00:00
func (op OperationProps) MarshalJSON() ([]byte, error) {
2024-05-14 13:07:09 +00:00
type Alias OperationProps
2024-05-14 13:07:09 +00:00
if op.Security == nil {
2024-05-14 13:07:09 +00:00
return json.Marshal(&struct {
Security []map[string][]string `json:"security,omitempty"`
2024-05-14 13:07:09 +00:00
*Alias
}{
2024-05-14 13:07:09 +00:00
Security: op.Security,
Alias: (*Alias)(&op),
2024-05-14 13:07:09 +00:00
})
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return json.Marshal(&struct {
Security []map[string][]string `json:"security"`
2024-05-14 13:07:09 +00:00
*Alias
}{
2024-05-14 13:07:09 +00:00
Security: op.Security,
Alias: (*Alias)(&op),
2024-05-14 13:07:09 +00:00
})
2024-05-14 13:07:09 +00:00
}
// Operation describes a single API operation on a path.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// For more information: http://goo.gl/8us55a#operationObject
2024-05-14 13:07:09 +00:00
type Operation struct {
VendorExtensible
2024-05-14 13:07:09 +00:00
OperationProps
}
// SuccessResponse gets a success response model
2024-05-14 13:07:09 +00:00
func (o *Operation) SuccessResponse() (*Response, int, bool) {
2024-05-14 13:07:09 +00:00
if o.Responses == nil {
2024-05-14 13:07:09 +00:00
return nil, 0, false
2024-05-14 13:07:09 +00:00
}
responseCodes := make([]int, 0, len(o.Responses.StatusCodeResponses))
2024-05-14 13:07:09 +00:00
for k := range o.Responses.StatusCodeResponses {
2024-05-14 13:07:09 +00:00
if k >= 200 && k < 300 {
2024-05-14 13:07:09 +00:00
responseCodes = append(responseCodes, k)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if len(responseCodes) > 0 {
2024-05-14 13:07:09 +00:00
sort.Ints(responseCodes)
2024-05-14 13:07:09 +00:00
v := o.Responses.StatusCodeResponses[responseCodes[0]]
2024-05-14 13:07:09 +00:00
return &v, responseCodes[0], true
2024-05-14 13:07:09 +00:00
}
return o.Responses.Default, 0, false
2024-05-14 13:07:09 +00:00
}
// JSONLookup look up a value by the json property name
2024-05-14 13:07:09 +00:00
func (o Operation) JSONLookup(token string) (interface{}, error) {
2024-05-14 13:07:09 +00:00
if ex, ok := o.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
r, _, err := jsonpointer.GetForToken(o.OperationProps, token)
2024-05-14 13:07:09 +00:00
return r, err
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 (o *Operation) UnmarshalJSON(data []byte) error {
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(data, &o.OperationProps); 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, &o.VendorExtensible)
2024-05-14 13:07:09 +00:00
}
// MarshalJSON converts this items object to JSON
2024-05-14 13:07:09 +00:00
func (o Operation) MarshalJSON() ([]byte, error) {
2024-05-14 13:07:09 +00:00
b1, err := json.Marshal(o.OperationProps)
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(o.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
concated := swag.ConcatJSON(b1, b2)
2024-05-14 13:07:09 +00:00
return concated, nil
2024-05-14 13:07:09 +00:00
}
// NewOperation creates a new operation instance.
2024-05-14 13:07:09 +00:00
// It expects an ID as parameter but not passing an ID is also valid.
2024-05-14 13:07:09 +00:00
func NewOperation(id string) *Operation {
2024-05-14 13:07:09 +00:00
op := new(Operation)
2024-05-14 13:07:09 +00:00
op.ID = id
2024-05-14 13:07:09 +00:00
return op
2024-05-14 13:07:09 +00:00
}
// WithID sets the ID property on this operation, allows for chaining.
2024-05-14 13:07:09 +00:00
func (o *Operation) WithID(id string) *Operation {
2024-05-14 13:07:09 +00:00
o.ID = id
2024-05-14 13:07:09 +00:00
return o
2024-05-14 13:07:09 +00:00
}
// WithDescription sets the description on this operation, allows for chaining
2024-05-14 13:07:09 +00:00
func (o *Operation) WithDescription(description string) *Operation {
2024-05-14 13:07:09 +00:00
o.Description = description
2024-05-14 13:07:09 +00:00
return o
2024-05-14 13:07:09 +00:00
}
// WithSummary sets the summary on this operation, allows for chaining
2024-05-14 13:07:09 +00:00
func (o *Operation) WithSummary(summary string) *Operation {
2024-05-14 13:07:09 +00:00
o.Summary = summary
2024-05-14 13:07:09 +00:00
return o
2024-05-14 13:07:09 +00:00
}
// WithExternalDocs sets/removes the external docs for/from this operation.
2024-05-14 13:07:09 +00:00
// When you pass empty strings as params the external documents will be removed.
2024-05-14 13:07:09 +00:00
// When you pass non-empty string as one value then those values will be used on the external docs object.
2024-05-14 13:07:09 +00:00
// So when you pass a non-empty description, you should also pass the url and vice versa.
2024-05-14 13:07:09 +00:00
func (o *Operation) WithExternalDocs(description, url string) *Operation {
2024-05-14 13:07:09 +00:00
if description == "" && url == "" {
2024-05-14 13:07:09 +00:00
o.ExternalDocs = nil
2024-05-14 13:07:09 +00:00
return o
2024-05-14 13:07:09 +00:00
}
if o.ExternalDocs == nil {
2024-05-14 13:07:09 +00:00
o.ExternalDocs = &ExternalDocumentation{}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
o.ExternalDocs.Description = description
2024-05-14 13:07:09 +00:00
o.ExternalDocs.URL = url
2024-05-14 13:07:09 +00:00
return o
2024-05-14 13:07:09 +00:00
}
// Deprecate marks the operation as deprecated
2024-05-14 13:07:09 +00:00
func (o *Operation) Deprecate() *Operation {
2024-05-14 13:07:09 +00:00
o.Deprecated = true
2024-05-14 13:07:09 +00:00
return o
2024-05-14 13:07:09 +00:00
}
// Undeprecate marks the operation as not deprected
2024-05-14 13:07:09 +00:00
func (o *Operation) Undeprecate() *Operation {
2024-05-14 13:07:09 +00:00
o.Deprecated = false
2024-05-14 13:07:09 +00:00
return o
2024-05-14 13:07:09 +00:00
}
// WithConsumes adds media types for incoming body values
2024-05-14 13:07:09 +00:00
func (o *Operation) WithConsumes(mediaTypes ...string) *Operation {
2024-05-14 13:07:09 +00:00
o.Consumes = append(o.Consumes, mediaTypes...)
2024-05-14 13:07:09 +00:00
return o
2024-05-14 13:07:09 +00:00
}
// WithProduces adds media types for outgoing body values
2024-05-14 13:07:09 +00:00
func (o *Operation) WithProduces(mediaTypes ...string) *Operation {
2024-05-14 13:07:09 +00:00
o.Produces = append(o.Produces, mediaTypes...)
2024-05-14 13:07:09 +00:00
return o
2024-05-14 13:07:09 +00:00
}
// WithTags adds tags for this operation
2024-05-14 13:07:09 +00:00
func (o *Operation) WithTags(tags ...string) *Operation {
2024-05-14 13:07:09 +00:00
o.Tags = append(o.Tags, tags...)
2024-05-14 13:07:09 +00:00
return o
2024-05-14 13:07:09 +00:00
}
// AddParam adds a parameter to this operation, when a parameter for that location
2024-05-14 13:07:09 +00:00
// and with that name already exists it will be replaced
2024-05-14 13:07:09 +00:00
func (o *Operation) AddParam(param *Parameter) *Operation {
2024-05-14 13:07:09 +00:00
if param == nil {
2024-05-14 13:07:09 +00:00
return o
2024-05-14 13:07:09 +00:00
}
for i, p := range o.Parameters {
2024-05-14 13:07:09 +00:00
if p.Name == param.Name && p.In == param.In {
2024-05-14 13:07:09 +00:00
params := make([]Parameter, 0, len(o.Parameters)+1)
2024-05-14 13:07:09 +00:00
params = append(params, o.Parameters[:i]...)
2024-05-14 13:07:09 +00:00
params = append(params, *param)
2024-05-14 13:07:09 +00:00
params = append(params, o.Parameters[i+1:]...)
2024-05-14 13:07:09 +00:00
o.Parameters = params
return o
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
o.Parameters = append(o.Parameters, *param)
2024-05-14 13:07:09 +00:00
return o
2024-05-14 13:07:09 +00:00
}
// RemoveParam removes a parameter from the operation
2024-05-14 13:07:09 +00:00
func (o *Operation) RemoveParam(name, in string) *Operation {
2024-05-14 13:07:09 +00:00
for i, p := range o.Parameters {
2024-05-14 13:07:09 +00:00
if p.Name == name && p.In == in {
2024-05-14 13:07:09 +00:00
o.Parameters = append(o.Parameters[:i], o.Parameters[i+1:]...)
2024-05-14 13:07:09 +00:00
return o
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return o
2024-05-14 13:07:09 +00:00
}
// SecuredWith adds a security scope to this operation.
2024-05-14 13:07:09 +00:00
func (o *Operation) SecuredWith(name string, scopes ...string) *Operation {
2024-05-14 13:07:09 +00:00
o.Security = append(o.Security, map[string][]string{name: scopes})
2024-05-14 13:07:09 +00:00
return o
2024-05-14 13:07:09 +00:00
}
// WithDefaultResponse adds a default response to the operation.
2024-05-14 13:07:09 +00:00
// Passing a nil value will remove the response
2024-05-14 13:07:09 +00:00
func (o *Operation) WithDefaultResponse(response *Response) *Operation {
2024-05-14 13:07:09 +00:00
return o.RespondsWith(0, response)
2024-05-14 13:07:09 +00:00
}
// RespondsWith adds a status code response to the operation.
2024-05-14 13:07:09 +00:00
// When the code is 0 the value of the response will be used as default response value.
2024-05-14 13:07:09 +00:00
// When the value of the response is nil it will be removed from the operation
2024-05-14 13:07:09 +00:00
func (o *Operation) RespondsWith(code int, response *Response) *Operation {
2024-05-14 13:07:09 +00:00
if o.Responses == nil {
2024-05-14 13:07:09 +00:00
o.Responses = new(Responses)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if code == 0 {
2024-05-14 13:07:09 +00:00
o.Responses.Default = response
2024-05-14 13:07:09 +00:00
return o
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if response == nil {
2024-05-14 13:07:09 +00:00
delete(o.Responses.StatusCodeResponses, code)
2024-05-14 13:07:09 +00:00
return o
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if o.Responses.StatusCodeResponses == nil {
2024-05-14 13:07:09 +00:00
o.Responses.StatusCodeResponses = make(map[int]Response)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
o.Responses.StatusCodeResponses[code] = *response
2024-05-14 13:07:09 +00:00
return o
2024-05-14 13:07:09 +00:00
}
type opsAlias OperationProps
type gobAlias struct {
Security []map[string]struct {
List []string
Pad bool
2024-05-14 13:07:09 +00:00
}
Alias *opsAlias
2024-05-14 13:07:09 +00:00
SecurityIsEmpty bool
}
// GobEncode provides a safe gob encoder for Operation, including empty security requirements
2024-05-14 13:07:09 +00:00
func (o Operation) GobEncode() ([]byte, error) {
2024-05-14 13:07:09 +00:00
raw := struct {
Ext VendorExtensible
2024-05-14 13:07:09 +00:00
Props OperationProps
}{
Ext: o.VendorExtensible,
2024-05-14 13:07:09 +00:00
Props: o.OperationProps,
}
2024-05-14 13:07:09 +00:00
var b bytes.Buffer
2024-05-14 13:07:09 +00:00
err := gob.NewEncoder(&b).Encode(raw)
2024-05-14 13:07:09 +00:00
return b.Bytes(), err
2024-05-14 13:07:09 +00:00
}
// GobDecode provides a safe gob decoder for Operation, including empty security requirements
2024-05-14 13:07:09 +00:00
func (o *Operation) GobDecode(b []byte) error {
2024-05-14 13:07:09 +00:00
var raw struct {
Ext VendorExtensible
2024-05-14 13:07:09 +00:00
Props OperationProps
}
buf := bytes.NewBuffer(b)
2024-05-14 13:07:09 +00:00
err := gob.NewDecoder(buf).Decode(&raw)
2024-05-14 13:07:09 +00:00
if 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
o.VendorExtensible = raw.Ext
2024-05-14 13:07:09 +00:00
o.OperationProps = raw.Props
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
// GobEncode provides a safe gob encoder for Operation, including empty security requirements
2024-05-14 13:07:09 +00:00
func (op OperationProps) GobEncode() ([]byte, error) {
2024-05-14 13:07:09 +00:00
raw := gobAlias{
2024-05-14 13:07:09 +00:00
Alias: (*opsAlias)(&op),
}
var b bytes.Buffer
2024-05-14 13:07:09 +00:00
if op.Security == nil {
2024-05-14 13:07:09 +00:00
// nil security requirement
2024-05-14 13:07:09 +00:00
err := gob.NewEncoder(&b).Encode(raw)
2024-05-14 13:07:09 +00:00
return b.Bytes(), err
2024-05-14 13:07:09 +00:00
}
if len(op.Security) == 0 {
2024-05-14 13:07:09 +00:00
// empty, but non-nil security requirement
2024-05-14 13:07:09 +00:00
raw.SecurityIsEmpty = true
2024-05-14 13:07:09 +00:00
raw.Alias.Security = nil
2024-05-14 13:07:09 +00:00
err := gob.NewEncoder(&b).Encode(raw)
2024-05-14 13:07:09 +00:00
return b.Bytes(), err
2024-05-14 13:07:09 +00:00
}
raw.Security = make([]map[string]struct {
List []string
Pad bool
2024-05-14 13:07:09 +00:00
}, 0, len(op.Security))
2024-05-14 13:07:09 +00:00
for _, req := range op.Security {
2024-05-14 13:07:09 +00:00
v := make(map[string]struct {
List []string
Pad bool
2024-05-14 13:07:09 +00:00
}, len(req))
2024-05-14 13:07:09 +00:00
for k, val := range req {
2024-05-14 13:07:09 +00:00
v[k] = struct {
List []string
Pad bool
2024-05-14 13:07:09 +00:00
}{
2024-05-14 13:07:09 +00:00
List: val,
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
raw.Security = append(raw.Security, v)
2024-05-14 13:07:09 +00:00
}
err := gob.NewEncoder(&b).Encode(raw)
2024-05-14 13:07:09 +00:00
return b.Bytes(), err
2024-05-14 13:07:09 +00:00
}
// GobDecode provides a safe gob decoder for Operation, including empty security requirements
2024-05-14 13:07:09 +00:00
func (op *OperationProps) GobDecode(b []byte) error {
2024-05-14 13:07:09 +00:00
var raw gobAlias
buf := bytes.NewBuffer(b)
2024-05-14 13:07:09 +00:00
err := gob.NewDecoder(buf).Decode(&raw)
2024-05-14 13:07:09 +00:00
if 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 raw.Alias == nil {
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
switch {
2024-05-14 13:07:09 +00:00
case raw.SecurityIsEmpty:
2024-05-14 13:07:09 +00:00
// empty, but non-nil security requirement
2024-05-14 13:07:09 +00:00
raw.Alias.Security = []map[string][]string{}
2024-05-14 13:07:09 +00:00
case len(raw.Alias.Security) == 0:
2024-05-14 13:07:09 +00:00
// nil security requirement
2024-05-14 13:07:09 +00:00
raw.Alias.Security = nil
2024-05-14 13:07:09 +00:00
default:
2024-05-14 13:07:09 +00:00
raw.Alias.Security = make([]map[string][]string, 0, len(raw.Security))
2024-05-14 13:07:09 +00:00
for _, req := range raw.Security {
2024-05-14 13:07:09 +00:00
v := make(map[string][]string, len(req))
2024-05-14 13:07:09 +00:00
for k, val := range req {
2024-05-14 13:07:09 +00:00
v[k] = make([]string, 0, len(val.List))
2024-05-14 13:07:09 +00:00
v[k] = append(v[k], val.List...)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
raw.Alias.Security = append(raw.Alias.Security, v)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
*op = *(*OperationProps)(raw.Alias)
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}