| 1 |
// Copyright 2015 go-swagger maintainers |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package spec |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
|
| 20 |
"github.com/go-openapi/swag" |
| 21 |
"k8s.io/kube-openapi/pkg/internal" |
| 22 |
jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json" |
| 23 |
"k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/jsontext" |
| 24 |
) |
| 25 |
|
| 26 |
// ParamProps describes the specific attributes of an operation parameter |
| 27 |
// |
| 28 |
// NOTE: |
| 29 |
// - Schema is defined when "in" == "body": see validate |
| 30 |
// - AllowEmptyValue is allowed where "in" == "query" || "formData" |
| 31 |
type ParamProps struct { |
| 32 |
Description string `json:"description,omitempty"` |
| 33 |
Name string `json:"name,omitempty"` |
| 34 |
In string `json:"in,omitempty"` |
| 35 |
Required bool `json:"required,omitempty"` |
| 36 |
Schema *Schema `json:"schema,omitempty"` |
| 37 |
AllowEmptyValue bool `json:"allowEmptyValue,omitempty"` |
| 38 |
} |
| 39 |
|
| 40 |
// Marshaling structure only, always edit along with corresponding |
| 41 |
// struct (or compilation will fail). |
| 42 |
type paramPropsOmitZero struct { |
| 43 |
Description string `json:"description,omitempty"` |
| 44 |
Name string `json:"name,omitempty"` |
| 45 |
In string `json:"in,omitempty"` |
| 46 |
Required bool `json:"required,omitzero"` |
| 47 |
Schema *Schema `json:"schema,omitzero"` |
| 48 |
AllowEmptyValue bool `json:"allowEmptyValue,omitzero"` |
| 49 |
} |
| 50 |
|
| 51 |
// Parameter a unique parameter is defined by a combination of a [name](#parameterName) and [location](#parameterIn). |
| 52 |
// |
| 53 |
// There are five possible parameter types. |
| 54 |
// * Path - Used together with [Path Templating](#pathTemplating), where the parameter value is actually part |
| 55 |
// |
| 56 |
// of the operation's URL. This does not include the host or base path of the API. For example, in `/items/{itemId}`, |
| 57 |
// the path parameter is `itemId`. |
| 58 |
// |
| 59 |
// * Query - Parameters that are appended to the URL. For example, in `/items?id=###`, the query parameter is `id`. |
| 60 |
// * Header - Custom headers that are expected as part of the request. |
| 61 |
// * Body - The payload that's appended to the HTTP request. Since there can only be one payload, there can only be |
| 62 |
// |
| 63 |
// _one_ body parameter. The name of the body parameter has no effect on the parameter itself and is used for |
| 64 |
// documentation purposes only. Since Form parameters are also in the payload, body and form parameters cannot exist |
| 65 |
// together for the same operation. |
| 66 |
// |
| 67 |
// * Form - Used to describe the payload of an HTTP request when either `application/x-www-form-urlencoded` or |
| 68 |
// |
| 69 |
// `multipart/form-data` are used as the content type of the request (in Swagger's definition, |
| 70 |
// the [`consumes`](#operationConsumes) property of an operation). This is the only parameter type that can be used |
| 71 |
// to send files, thus supporting the `file` type. Since form parameters are sent in the payload, they cannot be |
| 72 |
// declared together with a body parameter for the same operation. Form parameters have a different format based on |
| 73 |
// the content-type used (for further details, consult http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4). |
| 74 |
// * `application/x-www-form-urlencoded` - Similar to the format of Query parameters but as a payload. |
| 75 |
// For example, `foo=1&bar=swagger` - both `foo` and `bar` are form parameters. This is normally used for simple |
| 76 |
// parameters that are being transferred. |
| 77 |
// * `multipart/form-data` - each parameter takes a section in the payload with an internal header. |
| 78 |
// For example, for the header `Content-Disposition: form-data; name="submit-name"` the name of the parameter is |
| 79 |
// `submit-name`. This type of form parameters is more commonly used for file transfers. |
| 80 |
// |
| 81 |
// For more information: http://goo.gl/8us55a#parameterObject |
| 82 |
type Parameter struct { |
| 83 |
Refable |
| 84 |
CommonValidations |
| 85 |
SimpleSchema |
| 86 |
VendorExtensible |
| 87 |
ParamProps |
| 88 |
} |
| 89 |
|
| 90 |
// UnmarshalJSON hydrates this items instance with the data from JSON |
| 91 |
func (p *Parameter) UnmarshalJSON(data []byte) error { |
| 92 |
if internal.UseOptimizedJSONUnmarshaling { |
| 93 |
return jsonv2.Unmarshal(data, p) |
| 94 |
} |
| 95 |
|
| 96 |
if err := json.Unmarshal(data, &p.CommonValidations); err != nil { |
| 97 |
return err |
| 98 |
} |
| 99 |
if err := json.Unmarshal(data, &p.Refable); err != nil { |
| 100 |
return err |
| 101 |
} |
| 102 |
if err := json.Unmarshal(data, &p.SimpleSchema); err != nil { |
| 103 |
return err |
| 104 |
} |
| 105 |
if err := json.Unmarshal(data, &p.VendorExtensible); err != nil { |
| 106 |
return err |
| 107 |
} |
| 108 |
return json.Unmarshal(data, &p.ParamProps) |
| 109 |
} |
| 110 |
|
| 111 |
func (p *Parameter) UnmarshalJSONFrom(dec *jsontext.Decoder) error { |
| 112 |
var x struct { |
| 113 |
CommonValidations |
| 114 |
SimpleSchema |
| 115 |
Extensions Extensions `json:",inline"` |
| 116 |
ParamProps |
| 117 |
} |
| 118 |
if err := jsonv2.UnmarshalDecode(dec, &x); err != nil { |
| 119 |
return err |
| 120 |
} |
| 121 |
if err := p.Refable.Ref.fromMap(x.Extensions); err != nil { |
| 122 |
return err |
| 123 |
} |
| 124 |
p.CommonValidations = x.CommonValidations |
| 125 |
p.SimpleSchema = x.SimpleSchema |
| 126 |
p.Extensions = internal.SanitizeExtensions(x.Extensions) |
| 127 |
p.ParamProps = x.ParamProps |
| 128 |
return nil |
| 129 |
} |
| 130 |
|
| 131 |
// MarshalJSON converts this items object to JSON |
| 132 |
func (p Parameter) MarshalJSON() ([]byte, error) { |
| 133 |
if internal.UseOptimizedJSONMarshaling { |
| 134 |
return internal.DeterministicMarshal(p) |
| 135 |
} |
| 136 |
b1, err := json.Marshal(p.CommonValidations) |
| 137 |
if err != nil { |
| 138 |
return nil, err |
| 139 |
} |
| 140 |
b2, err := json.Marshal(p.SimpleSchema) |
| 141 |
if err != nil { |
| 142 |
return nil, err |
| 143 |
} |
| 144 |
b3, err := json.Marshal(p.Refable) |
| 145 |
if err != nil { |
| 146 |
return nil, err |
| 147 |
} |
| 148 |
b4, err := json.Marshal(p.VendorExtensible) |
| 149 |
if err != nil { |
| 150 |
return nil, err |
| 151 |
} |
| 152 |
b5, err := json.Marshal(p.ParamProps) |
| 153 |
if err != nil { |
| 154 |
return nil, err |
| 155 |
} |
| 156 |
return swag.ConcatJSON(b3, b1, b2, b4, b5), nil |
| 157 |
} |
| 158 |
|
| 159 |
func (p Parameter) MarshalJSONTo(enc *jsontext.Encoder) error { |
| 160 |
var x struct { |
| 161 |
CommonValidations commonValidationsOmitZero `json:",inline"` |
| 162 |
SimpleSchema simpleSchemaOmitZero `json:",inline"` |
| 163 |
ParamProps paramPropsOmitZero `json:",inline"` |
| 164 |
Ref string `json:"$ref,omitempty"` |
| 165 |
Extensions Extensions `json:",inline"` |
| 166 |
} |
| 167 |
x.CommonValidations = commonValidationsOmitZero(p.CommonValidations) |
| 168 |
x.SimpleSchema = simpleSchemaOmitZero(p.SimpleSchema) |
| 169 |
x.Extensions = internal.SanitizeExtensions(p.Extensions) |
| 170 |
x.ParamProps = paramPropsOmitZero(p.ParamProps) |
| 171 |
x.Ref = p.Refable.Ref.String() |
| 172 |
return jsonv2.MarshalEncode(enc, x) |
| 173 |
} |