| 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 |
|
| 22 |
"github.com/go-openapi/swag" |
| 23 |
"k8s.io/kube-openapi/pkg/internal" |
| 24 |
jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json" |
| 25 |
"k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/jsontext" |
| 26 |
"k8s.io/kube-openapi/pkg/validation/spec" |
| 27 |
) |
| 28 |
|
| 29 |
// Operation describes a single API operation on a path, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#operationObject |
| 30 |
// |
| 31 |
// Note that this struct is actually a thin wrapper around OperationProps to make it referable and extensible |
| 32 |
type Operation struct { |
| 33 |
OperationProps |
| 34 |
spec.VendorExtensible |
| 35 |
} |
| 36 |
|
| 37 |
// MarshalJSON is a custom marshal function that knows how to encode Operation as JSON |
| 38 |
func (o *Operation) MarshalJSON() ([]byte, error) { |
| 39 |
if internal.UseOptimizedJSONMarshalingV3 { |
| 40 |
return internal.DeterministicMarshal(o) |
| 41 |
} |
| 42 |
b1, err := json.Marshal(o.OperationProps) |
| 43 |
if err != nil { |
| 44 |
return nil, err |
| 45 |
} |
| 46 |
b2, err := json.Marshal(o.VendorExtensible) |
| 47 |
if err != nil { |
| 48 |
return nil, err |
| 49 |
} |
| 50 |
return swag.ConcatJSON(b1, b2), nil |
| 51 |
} |
| 52 |
|
| 53 |
func (o *Operation) MarshalJSONTo(enc *jsontext.Encoder) error { |
| 54 |
var x struct { |
| 55 |
Extensions spec.Extensions `json:",inline"` |
| 56 |
OperationProps operationPropsOmitZero `json:",inline"` |
| 57 |
} |
| 58 |
x.Extensions = internal.SanitizeExtensions(o.Extensions) |
| 59 |
x.OperationProps = operationPropsOmitZero(o.OperationProps) |
| 60 |
return jsonv2.MarshalEncode(enc, x) |
| 61 |
} |
| 62 |
|
| 63 |
// UnmarshalJSON hydrates this items instance with the data from JSON |
| 64 |
func (o *Operation) UnmarshalJSON(data []byte) error { |
| 65 |
if internal.UseOptimizedJSONUnmarshalingV3 { |
| 66 |
return jsonv2.Unmarshal(data, o) |
| 67 |
} |
| 68 |
if err := json.Unmarshal(data, &o.OperationProps); err != nil { |
| 69 |
return err |
| 70 |
} |
| 71 |
return json.Unmarshal(data, &o.VendorExtensible) |
| 72 |
} |
| 73 |
|
| 74 |
func (o *Operation) UnmarshalJSONFrom(dec *jsontext.Decoder) error { |
| 75 |
var x struct { |
| 76 |
Extensions spec.Extensions `json:",inline"` |
| 77 |
OperationProps |
| 78 |
} |
| 79 |
if err := jsonv2.UnmarshalDecode(dec, &x); err != nil { |
| 80 |
return err |
| 81 |
} |
| 82 |
o.Extensions = internal.SanitizeExtensions(x.Extensions) |
| 83 |
o.OperationProps = x.OperationProps |
| 84 |
return nil |
| 85 |
} |
| 86 |
|
| 87 |
// OperationProps describes a single API operation on a path, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#operationObject |
| 88 |
type OperationProps struct { |
| 89 |
// Tags holds a list of tags for API documentation control |
| 90 |
Tags []string `json:"tags,omitempty"` |
| 91 |
// Summary holds a short summary of what the operation does |
| 92 |
Summary string `json:"summary,omitempty"` |
| 93 |
// Description holds a verbose explanation of the operation behavior |
| 94 |
Description string `json:"description,omitempty"` |
| 95 |
// ExternalDocs holds additional external documentation for this operation |
| 96 |
ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"` |
| 97 |
// OperationId holds a unique string used to identify the operation |
| 98 |
OperationId string `json:"operationId,omitempty"` |
| 99 |
// Parameters a list of parameters that are applicable for this operation |
| 100 |
Parameters []*Parameter `json:"parameters,omitempty"` |
| 101 |
// RequestBody holds the request body applicable for this operation |
| 102 |
RequestBody *RequestBody `json:"requestBody,omitempty"` |
| 103 |
// Responses holds the list of possible responses as they are returned from executing this operation |
| 104 |
Responses *Responses `json:"responses,omitempty"` |
| 105 |
// Deprecated declares this operation to be deprecated |
| 106 |
Deprecated bool `json:"deprecated,omitempty"` |
| 107 |
// SecurityRequirement holds a declaration of which security mechanisms can be used for this operation |
| 108 |
SecurityRequirement []map[string][]string `json:"security,omitempty"` |
| 109 |
// Servers contains an alternative server array to service this operation |
| 110 |
Servers []*Server `json:"servers,omitempty"` |
| 111 |
} |
| 112 |
|
| 113 |
type operationPropsOmitZero struct { |
| 114 |
Tags []string `json:"tags,omitempty"` |
| 115 |
Summary string `json:"summary,omitempty"` |
| 116 |
Description string `json:"description,omitempty"` |
| 117 |
ExternalDocs *ExternalDocumentation `json:"externalDocs,omitzero"` |
| 118 |
OperationId string `json:"operationId,omitempty"` |
| 119 |
Parameters []*Parameter `json:"parameters,omitempty"` |
| 120 |
RequestBody *RequestBody `json:"requestBody,omitzero"` |
| 121 |
Responses *Responses `json:"responses,omitzero"` |
| 122 |
Deprecated bool `json:"deprecated,omitzero"` |
| 123 |
SecurityRequirement []map[string][]string `json:"security,omitempty"` |
| 124 |
Servers []*Server `json:"servers,omitempty"` |
| 125 |
} |