| 1 |
/* |
| 2 |
Copyright 2021 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package spec3 |
| 18 |
|
| 19 |
import ( |
| 20 |
"encoding/json" |
| 21 |
"fmt" |
| 22 |
"strconv" |
| 23 |
|
| 24 |
"github.com/go-openapi/swag" |
| 25 |
"k8s.io/kube-openapi/pkg/internal" |
| 26 |
jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json" |
| 27 |
"k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/jsontext" |
| 28 |
"k8s.io/kube-openapi/pkg/validation/spec" |
| 29 |
) |
| 30 |
|
| 31 |
// Responses holds the list of possible responses as they are returned from executing this operation |
| 32 |
// |
| 33 |
// Note that this struct is actually a thin wrapper around ResponsesProps to make it referable and extensible |
| 34 |
type Responses struct { |
| 35 |
ResponsesProps |
| 36 |
spec.VendorExtensible |
| 37 |
} |
| 38 |
|
| 39 |
// MarshalJSON is a custom marshal function that knows how to encode Responses as JSON |
| 40 |
func (r *Responses) MarshalJSON() ([]byte, error) { |
| 41 |
if internal.UseOptimizedJSONMarshalingV3 { |
| 42 |
return internal.DeterministicMarshal(r) |
| 43 |
} |
| 44 |
b1, err := json.Marshal(r.ResponsesProps) |
| 45 |
if err != nil { |
| 46 |
return nil, err |
| 47 |
} |
| 48 |
b2, err := json.Marshal(r.VendorExtensible) |
| 49 |
if err != nil { |
| 50 |
return nil, err |
| 51 |
} |
| 52 |
return swag.ConcatJSON(b1, b2), nil |
| 53 |
} |
| 54 |
|
| 55 |
func (r Responses) MarshalJSONTo(enc *jsontext.Encoder) error { |
| 56 |
type ArbitraryKeys map[string]interface{} |
| 57 |
var x struct { |
| 58 |
ArbitraryKeys ArbitraryKeys `json:",inline"` |
| 59 |
Default *Response `json:"default,omitzero"` |
| 60 |
} |
| 61 |
x.ArbitraryKeys = make(map[string]any, len(r.Extensions)+len(r.StatusCodeResponses)) |
| 62 |
for k, v := range r.Extensions { |
| 63 |
if internal.IsExtensionKey(k) { |
| 64 |
x.ArbitraryKeys[k] = v |
| 65 |
} |
| 66 |
} |
| 67 |
for k, v := range r.StatusCodeResponses { |
| 68 |
x.ArbitraryKeys[strconv.Itoa(k)] = v |
| 69 |
} |
| 70 |
x.Default = r.Default |
| 71 |
return jsonv2.MarshalEncode(enc, x) |
| 72 |
} |
| 73 |
|
| 74 |
func (r *Responses) UnmarshalJSON(data []byte) error { |
| 75 |
if internal.UseOptimizedJSONUnmarshalingV3 { |
| 76 |
return jsonv2.Unmarshal(data, r) |
| 77 |
} |
| 78 |
if err := json.Unmarshal(data, &r.ResponsesProps); err != nil { |
| 79 |
return err |
| 80 |
} |
| 81 |
if err := json.Unmarshal(data, &r.VendorExtensible); err != nil { |
| 82 |
return err |
| 83 |
} |
| 84 |
return nil |
| 85 |
} |
| 86 |
|
| 87 |
// ResponsesProps holds the list of possible responses as they are returned from executing this operation |
| 88 |
type ResponsesProps struct { |
| 89 |
// Default holds the documentation of responses other than the ones declared for specific HTTP response codes. Use this field to cover undeclared responses |
| 90 |
Default *Response `json:"-"` |
| 91 |
// StatusCodeResponses holds a map of any HTTP status code to the response definition |
| 92 |
StatusCodeResponses map[int]*Response `json:"-"` |
| 93 |
} |
| 94 |
|
| 95 |
// MarshalJSON is a custom marshal function that knows how to encode ResponsesProps as JSON |
| 96 |
func (r ResponsesProps) MarshalJSON() ([]byte, error) { |
| 97 |
toser := map[string]*Response{} |
| 98 |
if r.Default != nil { |
| 99 |
toser["default"] = r.Default |
| 100 |
} |
| 101 |
for k, v := range r.StatusCodeResponses { |
| 102 |
toser[strconv.Itoa(k)] = v |
| 103 |
} |
| 104 |
return json.Marshal(toser) |
| 105 |
} |
| 106 |
|
| 107 |
// UnmarshalJSON unmarshals responses from JSON |
| 108 |
func (r *ResponsesProps) UnmarshalJSON(data []byte) error { |
| 109 |
if internal.UseOptimizedJSONUnmarshalingV3 { |
| 110 |
return jsonv2.Unmarshal(data, r) |
| 111 |
} |
| 112 |
var res map[string]json.RawMessage |
| 113 |
if err := json.Unmarshal(data, &res); err != nil { |
| 114 |
return err |
| 115 |
} |
| 116 |
if v, ok := res["default"]; ok { |
| 117 |
value := Response{} |
| 118 |
if err := json.Unmarshal(v, &value); err != nil { |
| 119 |
return err |
| 120 |
} |
| 121 |
r.Default = &value |
| 122 |
delete(res, "default") |
| 123 |
} |
| 124 |
for k, v := range res { |
| 125 |
// Take all integral keys |
| 126 |
if nk, err := strconv.Atoi(k); err == nil { |
| 127 |
if r.StatusCodeResponses == nil { |
| 128 |
r.StatusCodeResponses = map[int]*Response{} |
| 129 |
} |
| 130 |
value := Response{} |
| 131 |
if err := json.Unmarshal(v, &value); err != nil { |
| 132 |
return err |
| 133 |
} |
| 134 |
r.StatusCodeResponses[nk] = &value |
| 135 |
} |
| 136 |
} |
| 137 |
return nil |
| 138 |
} |
| 139 |
|
| 140 |
func (r *Responses) UnmarshalJSONFrom(dec *jsontext.Decoder) (err error) { |
| 141 |
tok, err := dec.ReadToken() |
| 142 |
if err != nil { |
| 143 |
return err |
| 144 |
} |
| 145 |
switch k := tok.Kind(); k { |
| 146 |
case 'n': |
| 147 |
*r = Responses{} |
| 148 |
return nil |
| 149 |
case '{': |
| 150 |
for { |
| 151 |
tok, err := dec.ReadToken() |
| 152 |
if err != nil { |
| 153 |
return err |
| 154 |
} |
| 155 |
if tok.Kind() == '}' { |
| 156 |
return nil |
| 157 |
} |
| 158 |
switch k := tok.String(); { |
| 159 |
case internal.IsExtensionKey(k): |
| 160 |
var ext any |
| 161 |
if err := jsonv2.UnmarshalDecode(dec, &ext); err != nil { |
| 162 |
return err |
| 163 |
} |
| 164 |
|
| 165 |
if r.Extensions == nil { |
| 166 |
r.Extensions = make(map[string]any) |
| 167 |
} |
| 168 |
r.Extensions[k] = ext |
| 169 |
case k == "default": |
| 170 |
resp := Response{} |
| 171 |
if err := jsonv2.UnmarshalDecode(dec, &resp); err != nil { |
| 172 |
return err |
| 173 |
} |
| 174 |
r.ResponsesProps.Default = &resp |
| 175 |
default: |
| 176 |
if nk, err := strconv.Atoi(k); err == nil { |
| 177 |
resp := Response{} |
| 178 |
if err := jsonv2.UnmarshalDecode(dec, &resp); err != nil { |
| 179 |
return err |
| 180 |
} |
| 181 |
|
| 182 |
if r.StatusCodeResponses == nil { |
| 183 |
r.StatusCodeResponses = map[int]*Response{} |
| 184 |
} |
| 185 |
r.StatusCodeResponses[nk] = &resp |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
default: |
| 190 |
return fmt.Errorf("unknown JSON kind: %v", k) |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
// Response describes a single response from an API Operation, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#responseObject |
| 195 |
// |
| 196 |
// Note that this struct is actually a thin wrapper around ResponseProps to make it referable and extensible |
| 197 |
type Response struct { |
| 198 |
spec.Refable |
| 199 |
ResponseProps |
| 200 |
spec.VendorExtensible |
| 201 |
} |
| 202 |
|
| 203 |
// MarshalJSON is a custom marshal function that knows how to encode Response as JSON |
| 204 |
func (r *Response) MarshalJSON() ([]byte, error) { |
| 205 |
if internal.UseOptimizedJSONMarshalingV3 { |
| 206 |
return internal.DeterministicMarshal(r) |
| 207 |
} |
| 208 |
b1, err := json.Marshal(r.Refable) |
| 209 |
if err != nil { |
| 210 |
return nil, err |
| 211 |
} |
| 212 |
b2, err := json.Marshal(r.ResponseProps) |
| 213 |
if err != nil { |
| 214 |
return nil, err |
| 215 |
} |
| 216 |
b3, err := json.Marshal(r.VendorExtensible) |
| 217 |
if err != nil { |
| 218 |
return nil, err |
| 219 |
} |
| 220 |
return swag.ConcatJSON(b1, b2, b3), nil |
| 221 |
} |
| 222 |
|
| 223 |
func (r Response) MarshalJSONTo(enc *jsontext.Encoder) error { |
| 224 |
var x struct { |
| 225 |
Ref string `json:"$ref,omitempty"` |
| 226 |
Extensions spec.Extensions `json:",inline"` |
| 227 |
ResponseProps `json:",inline"` |
| 228 |
} |
| 229 |
x.Ref = r.Refable.Ref.String() |
| 230 |
x.Extensions = internal.SanitizeExtensions(r.Extensions) |
| 231 |
x.ResponseProps = r.ResponseProps |
| 232 |
return jsonv2.MarshalEncode(enc, x) |
| 233 |
} |
| 234 |
|
| 235 |
func (r *Response) UnmarshalJSON(data []byte) error { |
| 236 |
if internal.UseOptimizedJSONUnmarshalingV3 { |
| 237 |
return jsonv2.Unmarshal(data, r) |
| 238 |
} |
| 239 |
if err := json.Unmarshal(data, &r.Refable); err != nil { |
| 240 |
return err |
| 241 |
} |
| 242 |
if err := json.Unmarshal(data, &r.ResponseProps); err != nil { |
| 243 |
return err |
| 244 |
} |
| 245 |
if err := json.Unmarshal(data, &r.VendorExtensible); err != nil { |
| 246 |
return err |
| 247 |
} |
| 248 |
return nil |
| 249 |
} |
| 250 |
|
| 251 |
func (r *Response) UnmarshalJSONFrom(dec *jsontext.Decoder) error { |
| 252 |
var x struct { |
| 253 |
Extensions spec.Extensions `json:",inline"` |
| 254 |
ResponseProps |
| 255 |
} |
| 256 |
if err := jsonv2.UnmarshalDecode(dec, &x); err != nil { |
| 257 |
return err |
| 258 |
} |
| 259 |
if err := internal.JSONRefFromMap(&r.Ref.Ref, x.Extensions); err != nil { |
| 260 |
return err |
| 261 |
} |
| 262 |
r.Extensions = internal.SanitizeExtensions(x.Extensions) |
| 263 |
r.ResponseProps = x.ResponseProps |
| 264 |
return nil |
| 265 |
} |
| 266 |
|
| 267 |
// ResponseProps describes a single response from an API Operation, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#responseObject |
| 268 |
type ResponseProps struct { |
| 269 |
// Description holds a short description of the response |
| 270 |
Description string `json:"description,omitempty"` |
| 271 |
// Headers holds a maps of a headers name to its definition |
| 272 |
Headers map[string]*Header `json:"headers,omitempty"` |
| 273 |
// Content holds a map containing descriptions of potential response payloads |
| 274 |
Content map[string]*MediaType `json:"content,omitempty"` |
| 275 |
// Links is a map of operations links that can be followed from the response |
| 276 |
Links map[string]*Link `json:"links,omitempty"` |
| 277 |
} |
| 278 |
|
| 279 |
// Link represents a possible design-time link for a response, more at https://swagger.io/specification/#link-object |
| 280 |
type Link struct { |
| 281 |
spec.Refable |
| 282 |
LinkProps |
| 283 |
spec.VendorExtensible |
| 284 |
} |
| 285 |
|
| 286 |
// MarshalJSON is a custom marshal function that knows how to encode Link as JSON |
| 287 |
func (r *Link) MarshalJSON() ([]byte, error) { |
| 288 |
if internal.UseOptimizedJSONMarshalingV3 { |
| 289 |
return internal.DeterministicMarshal(r) |
| 290 |
} |
| 291 |
b1, err := json.Marshal(r.Refable) |
| 292 |
if err != nil { |
| 293 |
return nil, err |
| 294 |
} |
| 295 |
b2, err := json.Marshal(r.LinkProps) |
| 296 |
if err != nil { |
| 297 |
return nil, err |
| 298 |
} |
| 299 |
b3, err := json.Marshal(r.VendorExtensible) |
| 300 |
if err != nil { |
| 301 |
return nil, err |
| 302 |
} |
| 303 |
return swag.ConcatJSON(b1, b2, b3), nil |
| 304 |
} |
| 305 |
|
| 306 |
func (r *Link) MarshalJSONTo(enc *jsontext.Encoder) error { |
| 307 |
var x struct { |
| 308 |
Ref string `json:"$ref,omitempty"` |
| 309 |
Extensions spec.Extensions `json:",inline"` |
| 310 |
LinkProps `json:",inline"` |
| 311 |
} |
| 312 |
x.Ref = r.Refable.Ref.String() |
| 313 |
x.Extensions = internal.SanitizeExtensions(r.Extensions) |
| 314 |
x.LinkProps = r.LinkProps |
| 315 |
return jsonv2.MarshalEncode(enc, x) |
| 316 |
} |
| 317 |
|
| 318 |
func (r *Link) UnmarshalJSON(data []byte) error { |
| 319 |
if internal.UseOptimizedJSONUnmarshalingV3 { |
| 320 |
return jsonv2.Unmarshal(data, r) |
| 321 |
} |
| 322 |
if err := json.Unmarshal(data, &r.Refable); err != nil { |
| 323 |
return err |
| 324 |
} |
| 325 |
if err := json.Unmarshal(data, &r.LinkProps); err != nil { |
| 326 |
return err |
| 327 |
} |
| 328 |
if err := json.Unmarshal(data, &r.VendorExtensible); err != nil { |
| 329 |
return err |
| 330 |
} |
| 331 |
|
| 332 |
return nil |
| 333 |
} |
| 334 |
|
| 335 |
func (l *Link) UnmarshalJSONFrom(dec *jsontext.Decoder) error { |
| 336 |
var x struct { |
| 337 |
Extensions spec.Extensions `json:",inline"` |
| 338 |
LinkProps |
| 339 |
} |
| 340 |
if err := jsonv2.UnmarshalDecode(dec, &x); err != nil { |
| 341 |
return err |
| 342 |
} |
| 343 |
if err := internal.JSONRefFromMap(&l.Ref.Ref, x.Extensions); err != nil { |
| 344 |
return err |
| 345 |
} |
| 346 |
l.Extensions = internal.SanitizeExtensions(x.Extensions) |
| 347 |
l.LinkProps = x.LinkProps |
| 348 |
return nil |
| 349 |
} |
| 350 |
|
| 351 |
// LinkProps describes a single response from an API Operation, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#responseObject |
| 352 |
type LinkProps struct { |
| 353 |
// OperationId is the name of an existing, resolvable OAS operation |
| 354 |
OperationId string `json:"operationId,omitempty"` |
| 355 |
// Parameters is a map representing parameters to pass to an operation as specified with operationId or identified via operationRef |
| 356 |
Parameters map[string]interface{} `json:"parameters,omitempty"` |
| 357 |
// Description holds a description of the link |
| 358 |
Description string `json:"description,omitempty"` |
| 359 |
// RequestBody is a literal value or expresion to use as a request body when calling the target operation |
| 360 |
RequestBody interface{} `json:"requestBody,omitempty"` |
| 361 |
// Server holds a server object used by the target operation |
| 362 |
Server *Server `json:"server,omitempty"` |
| 363 |
} |