| 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 |
"strings" |
| 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 |
// Paths describes the available paths and operations for the API, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#pathsObject |
| 32 |
type Paths struct { |
| 33 |
Paths map[string]*Path |
| 34 |
spec.VendorExtensible |
| 35 |
} |
| 36 |
|
| 37 |
// MarshalJSON is a custom marshal function that knows how to encode Paths as JSON |
| 38 |
func (p *Paths) MarshalJSON() ([]byte, error) { |
| 39 |
if internal.UseOptimizedJSONMarshalingV3 { |
| 40 |
return internal.DeterministicMarshal(p) |
| 41 |
} |
| 42 |
b1, err := json.Marshal(p.VendorExtensible) |
| 43 |
if err != nil { |
| 44 |
return nil, err |
| 45 |
} |
| 46 |
|
| 47 |
pths := make(map[string]*Path) |
| 48 |
for k, v := range p.Paths { |
| 49 |
if strings.HasPrefix(k, "/") { |
| 50 |
pths[k] = v |
| 51 |
} |
| 52 |
} |
| 53 |
b2, err := json.Marshal(pths) |
| 54 |
if err != nil { |
| 55 |
return nil, err |
| 56 |
} |
| 57 |
concated := swag.ConcatJSON(b1, b2) |
| 58 |
return concated, nil |
| 59 |
} |
| 60 |
|
| 61 |
func (p *Paths) MarshalJSONTo(enc *jsontext.Encoder) error { |
| 62 |
m := make(map[string]any, len(p.Extensions)+len(p.Paths)) |
| 63 |
for k, v := range p.Extensions { |
| 64 |
if internal.IsExtensionKey(k) { |
| 65 |
m[k] = v |
| 66 |
} |
| 67 |
} |
| 68 |
for k, v := range p.Paths { |
| 69 |
if strings.HasPrefix(k, "/") { |
| 70 |
m[k] = v |
| 71 |
} |
| 72 |
} |
| 73 |
return jsonv2.MarshalEncode(enc, m) |
| 74 |
} |
| 75 |
|
| 76 |
// UnmarshalJSON hydrates this items instance with the data from JSON |
| 77 |
func (p *Paths) UnmarshalJSON(data []byte) error { |
| 78 |
if internal.UseOptimizedJSONUnmarshalingV3 { |
| 79 |
return jsonv2.Unmarshal(data, p) |
| 80 |
} |
| 81 |
var res map[string]json.RawMessage |
| 82 |
if err := json.Unmarshal(data, &res); err != nil { |
| 83 |
return err |
| 84 |
} |
| 85 |
for k, v := range res { |
| 86 |
if strings.HasPrefix(strings.ToLower(k), "x-") { |
| 87 |
if p.Extensions == nil { |
| 88 |
p.Extensions = make(map[string]interface{}) |
| 89 |
} |
| 90 |
var d interface{} |
| 91 |
if err := json.Unmarshal(v, &d); err != nil { |
| 92 |
return err |
| 93 |
} |
| 94 |
p.Extensions[k] = d |
| 95 |
} |
| 96 |
if strings.HasPrefix(k, "/") { |
| 97 |
if p.Paths == nil { |
| 98 |
p.Paths = make(map[string]*Path) |
| 99 |
} |
| 100 |
var pi *Path |
| 101 |
if err := json.Unmarshal(v, &pi); err != nil { |
| 102 |
return err |
| 103 |
} |
| 104 |
p.Paths[k] = pi |
| 105 |
} |
| 106 |
} |
| 107 |
return nil |
| 108 |
} |
| 109 |
|
| 110 |
func (p *Paths) UnmarshalJSONFrom(dec *jsontext.Decoder) error { |
| 111 |
tok, err := dec.ReadToken() |
| 112 |
if err != nil { |
| 113 |
return err |
| 114 |
} |
| 115 |
switch k := tok.Kind(); k { |
| 116 |
case 'n': |
| 117 |
*p = Paths{} |
| 118 |
return nil |
| 119 |
case '{': |
| 120 |
for { |
| 121 |
tok, err := dec.ReadToken() |
| 122 |
if err != nil { |
| 123 |
return err |
| 124 |
} |
| 125 |
|
| 126 |
if tok.Kind() == '}' { |
| 127 |
return nil |
| 128 |
} |
| 129 |
|
| 130 |
switch k := tok.String(); { |
| 131 |
case internal.IsExtensionKey(k): |
| 132 |
var ext any |
| 133 |
if err := jsonv2.UnmarshalDecode(dec, &ext); err != nil { |
| 134 |
return err |
| 135 |
} |
| 136 |
|
| 137 |
if p.Extensions == nil { |
| 138 |
p.Extensions = make(map[string]any) |
| 139 |
} |
| 140 |
p.Extensions[k] = ext |
| 141 |
case len(k) > 0 && k[0] == '/': |
| 142 |
pi := Path{} |
| 143 |
if err := jsonv2.UnmarshalDecode(dec, &pi); err != nil { |
| 144 |
return err |
| 145 |
} |
| 146 |
|
| 147 |
if p.Paths == nil { |
| 148 |
p.Paths = make(map[string]*Path) |
| 149 |
} |
| 150 |
p.Paths[k] = &pi |
| 151 |
default: |
| 152 |
_, err := dec.ReadValue() // skip value |
| 153 |
if err != nil { |
| 154 |
return err |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
default: |
| 159 |
return fmt.Errorf("unknown JSON kind: %v", k) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
// Path describes the operations available on a single path, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#pathItemObject |
| 164 |
// |
| 165 |
// Note that this struct is actually a thin wrapper around PathProps to make it referable and extensible |
| 166 |
type Path struct { |
| 167 |
spec.Refable |
| 168 |
PathProps |
| 169 |
spec.VendorExtensible |
| 170 |
} |
| 171 |
|
| 172 |
// MarshalJSON is a custom marshal function that knows how to encode Path as JSON |
| 173 |
func (p *Path) MarshalJSON() ([]byte, error) { |
| 174 |
if internal.UseOptimizedJSONMarshalingV3 { |
| 175 |
return internal.DeterministicMarshal(p) |
| 176 |
} |
| 177 |
b1, err := json.Marshal(p.Refable) |
| 178 |
if err != nil { |
| 179 |
return nil, err |
| 180 |
} |
| 181 |
b2, err := json.Marshal(p.PathProps) |
| 182 |
if err != nil { |
| 183 |
return nil, err |
| 184 |
} |
| 185 |
b3, err := json.Marshal(p.VendorExtensible) |
| 186 |
if err != nil { |
| 187 |
return nil, err |
| 188 |
} |
| 189 |
return swag.ConcatJSON(b1, b2, b3), nil |
| 190 |
} |
| 191 |
|
| 192 |
func (p *Path) MarshalJSONTo(enc *jsontext.Encoder) error { |
| 193 |
var x struct { |
| 194 |
Ref string `json:"$ref,omitempty"` |
| 195 |
Extensions spec.Extensions `json:",inline"` |
| 196 |
PathProps |
| 197 |
} |
| 198 |
x.Ref = p.Refable.Ref.String() |
| 199 |
x.Extensions = internal.SanitizeExtensions(p.Extensions) |
| 200 |
x.PathProps = p.PathProps |
| 201 |
return jsonv2.MarshalEncode(enc, x) |
| 202 |
} |
| 203 |
|
| 204 |
func (p *Path) UnmarshalJSON(data []byte) error { |
| 205 |
if internal.UseOptimizedJSONUnmarshalingV3 { |
| 206 |
return jsonv2.Unmarshal(data, p) |
| 207 |
} |
| 208 |
if err := json.Unmarshal(data, &p.Refable); err != nil { |
| 209 |
return err |
| 210 |
} |
| 211 |
if err := json.Unmarshal(data, &p.PathProps); err != nil { |
| 212 |
return err |
| 213 |
} |
| 214 |
if err := json.Unmarshal(data, &p.VendorExtensible); err != nil { |
| 215 |
return err |
| 216 |
} |
| 217 |
return nil |
| 218 |
} |
| 219 |
|
| 220 |
func (p *Path) UnmarshalJSONFrom(dec *jsontext.Decoder) error { |
| 221 |
var x struct { |
| 222 |
Extensions spec.Extensions `json:",inline"` |
| 223 |
PathProps |
| 224 |
} |
| 225 |
|
| 226 |
if err := jsonv2.UnmarshalDecode(dec, &x); err != nil { |
| 227 |
return err |
| 228 |
} |
| 229 |
if err := internal.JSONRefFromMap(&p.Ref.Ref, x.Extensions); err != nil { |
| 230 |
return err |
| 231 |
} |
| 232 |
p.Extensions = internal.SanitizeExtensions(x.Extensions) |
| 233 |
p.PathProps = x.PathProps |
| 234 |
|
| 235 |
return nil |
| 236 |
} |
| 237 |
|
| 238 |
// PathProps describes the operations available on a single path, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#pathItemObject |
| 239 |
type PathProps struct { |
| 240 |
// Summary holds a summary for all operations in this path |
| 241 |
Summary string `json:"summary,omitempty"` |
| 242 |
// Description holds a description for all operations in this path |
| 243 |
Description string `json:"description,omitempty"` |
| 244 |
// Get defines GET operation |
| 245 |
Get *Operation `json:"get,omitempty"` |
| 246 |
// Put defines PUT operation |
| 247 |
Put *Operation `json:"put,omitempty"` |
| 248 |
// Post defines POST operation |
| 249 |
Post *Operation `json:"post,omitempty"` |
| 250 |
// Delete defines DELETE operation |
| 251 |
Delete *Operation `json:"delete,omitempty"` |
| 252 |
// Options defines OPTIONS operation |
| 253 |
Options *Operation `json:"options,omitempty"` |
| 254 |
// Head defines HEAD operation |
| 255 |
Head *Operation `json:"head,omitempty"` |
| 256 |
// Patch defines PATCH operation |
| 257 |
Patch *Operation `json:"patch,omitempty"` |
| 258 |
// Trace defines TRACE operation |
| 259 |
Trace *Operation `json:"trace,omitempty"` |
| 260 |
// Servers is an alternative server array to service all operations in this path |
| 261 |
Servers []*Server `json:"servers,omitempty"` |
| 262 |
// Parameters a list of parameters that are applicable for this operation |
| 263 |
Parameters []*Parameter `json:"parameters,omitempty"` |
| 264 |
} |