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