| 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 |
// OperationProps describes an operation |
| 27 |
// |
| 28 |
// NOTES: |
| 29 |
// - schemes, when present must be from [http, https, ws, wss]: see validate |
| 30 |
// - Security is handled as a special case: see MarshalJSON function |
| 31 |
type OperationProps struct { |
| 32 |
Description string `json:"description,omitempty"` |
| 33 |
Consumes []string `json:"consumes,omitempty"` |
| 34 |
Produces []string `json:"produces,omitempty"` |
| 35 |
Schemes []string `json:"schemes,omitempty"` |
| 36 |
Tags []string `json:"tags,omitempty"` |
| 37 |
Summary string `json:"summary,omitempty"` |
| 38 |
ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"` |
| 39 |
ID string `json:"operationId,omitempty"` |
| 40 |
Deprecated bool `json:"deprecated,omitempty"` |
| 41 |
Security []map[string][]string `json:"security,omitempty"` |
| 42 |
Parameters []Parameter `json:"parameters,omitempty"` |
| 43 |
Responses *Responses `json:"responses,omitempty"` |
| 44 |
} |
| 45 |
|
| 46 |
// Marshaling structure only, always edit along with corresponding |
| 47 |
// struct (or compilation will fail). |
| 48 |
type operationPropsOmitZero struct { |
| 49 |
Description string `json:"description,omitempty"` |
| 50 |
Consumes []string `json:"consumes,omitempty"` |
| 51 |
Produces []string `json:"produces,omitempty"` |
| 52 |
Schemes []string `json:"schemes,omitempty"` |
| 53 |
Tags []string `json:"tags,omitempty"` |
| 54 |
Summary string `json:"summary,omitempty"` |
| 55 |
ExternalDocs *ExternalDocumentation `json:"externalDocs,omitzero"` |
| 56 |
ID string `json:"operationId,omitempty"` |
| 57 |
Deprecated bool `json:"deprecated,omitempty,omitzero"` |
| 58 |
Security []map[string][]string `json:"security,omitempty"` |
| 59 |
Parameters []Parameter `json:"parameters,omitempty"` |
| 60 |
Responses *Responses `json:"responses,omitzero"` |
| 61 |
} |
| 62 |
|
| 63 |
// MarshalJSON takes care of serializing operation properties to JSON |
| 64 |
// |
| 65 |
// We use a custom marhaller here to handle a special cases related to |
| 66 |
// the Security field. We need to preserve zero length slice |
| 67 |
// while omitting the field when the value is nil/unset. |
| 68 |
func (op OperationProps) MarshalJSON() ([]byte, error) { |
| 69 |
type Alias OperationProps |
| 70 |
if op.Security == nil { |
| 71 |
return json.Marshal(&struct { |
| 72 |
Security []map[string][]string `json:"security,omitempty"` |
| 73 |
*Alias |
| 74 |
}{ |
| 75 |
Security: op.Security, |
| 76 |
Alias: (*Alias)(&op), |
| 77 |
}) |
| 78 |
} |
| 79 |
return json.Marshal(&struct { |
| 80 |
Security []map[string][]string `json:"security"` |
| 81 |
*Alias |
| 82 |
}{ |
| 83 |
Security: op.Security, |
| 84 |
Alias: (*Alias)(&op), |
| 85 |
}) |
| 86 |
} |
| 87 |
|
| 88 |
// Operation describes a single API operation on a path. |
| 89 |
// |
| 90 |
// For more information: http://goo.gl/8us55a#operationObject |
| 91 |
type Operation struct { |
| 92 |
VendorExtensible |
| 93 |
OperationProps |
| 94 |
} |
| 95 |
|
| 96 |
// UnmarshalJSON hydrates this items instance with the data from JSON |
| 97 |
func (o *Operation) UnmarshalJSON(data []byte) error { |
| 98 |
if internal.UseOptimizedJSONUnmarshaling { |
| 99 |
return jsonv2.Unmarshal(data, o) |
| 100 |
} |
| 101 |
|
| 102 |
if err := json.Unmarshal(data, &o.OperationProps); err != nil { |
| 103 |
return err |
| 104 |
} |
| 105 |
return json.Unmarshal(data, &o.VendorExtensible) |
| 106 |
} |
| 107 |
|
| 108 |
func (o *Operation) UnmarshalJSONFrom(dec *jsontext.Decoder) error { |
| 109 |
type OperationPropsNoMethods OperationProps // strip MarshalJSON method |
| 110 |
var x struct { |
| 111 |
Extensions Extensions `json:",inline"` |
| 112 |
OperationPropsNoMethods |
| 113 |
} |
| 114 |
if err := jsonv2.UnmarshalDecode(dec, &x); err != nil { |
| 115 |
return err |
| 116 |
} |
| 117 |
o.Extensions = internal.SanitizeExtensions(x.Extensions) |
| 118 |
o.OperationProps = OperationProps(x.OperationPropsNoMethods) |
| 119 |
return nil |
| 120 |
} |
| 121 |
|
| 122 |
// MarshalJSON converts this items object to JSON |
| 123 |
func (o Operation) MarshalJSON() ([]byte, error) { |
| 124 |
if internal.UseOptimizedJSONMarshaling { |
| 125 |
return internal.DeterministicMarshal(o) |
| 126 |
} |
| 127 |
b1, err := json.Marshal(o.OperationProps) |
| 128 |
if err != nil { |
| 129 |
return nil, err |
| 130 |
} |
| 131 |
b2, err := json.Marshal(o.VendorExtensible) |
| 132 |
if err != nil { |
| 133 |
return nil, err |
| 134 |
} |
| 135 |
concated := swag.ConcatJSON(b1, b2) |
| 136 |
return concated, nil |
| 137 |
} |
| 138 |
|
| 139 |
func (o Operation) MarshalJSONTo(enc *jsontext.Encoder) error { |
| 140 |
var x struct { |
| 141 |
Extensions Extensions `json:",inline"` |
| 142 |
OperationProps operationPropsOmitZero `json:",inline"` |
| 143 |
} |
| 144 |
x.Extensions = internal.SanitizeExtensions(o.Extensions) |
| 145 |
x.OperationProps = operationPropsOmitZero(o.OperationProps) |
| 146 |
return jsonv2.MarshalEncode(enc, x) |
| 147 |
} |