Debian Codegraph @

pkg/validation/spec/swagger.go

raw
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 "fmt"
20
21 "github.com/go-openapi/swag"
22 "k8s.io/kube-openapi/pkg/internal"
23 jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json"
24 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/jsontext"
25 )
26
27 // Swagger this is the root document object for the API specification.
28 // It combines what previously was the Resource Listing and API Declaration (version 1.2 and earlier)
29 // together into one document.
30 //
31 // For more information: http://goo.gl/8us55a#swagger-object-
32 type Swagger struct {
33 VendorExtensible
34 SwaggerProps
35 }
36
37 // MarshalJSON marshals this swagger structure to json
38 func (s Swagger) MarshalJSON() ([]byte, error) {
39 if internal.UseOptimizedJSONMarshaling {
40 return internal.DeterministicMarshal(s)
41 }
42 b1, err := json.Marshal(s.SwaggerProps)
43 if err != nil {
44 return nil, err
45 }
46 b2, err := json.Marshal(s.VendorExtensible)
47 if err != nil {
48 return nil, err
49 }
50 return swag.ConcatJSON(b1, b2), nil
51 }
52
53 // MarshalJSON marshals this swagger structure to json
54 func (s Swagger) MarshalJSONTo(enc *jsontext.Encoder) error {
55 var x struct {
56 Extensions Extensions `json:",inline"`
57 SwaggerProps
58 }
59 x.Extensions = internal.SanitizeExtensions(s.Extensions)
60 x.SwaggerProps = s.SwaggerProps
61 return jsonv2.MarshalEncode(enc, x)
62 }
63
64 // UnmarshalJSON unmarshals a swagger spec from json
65 func (s *Swagger) UnmarshalJSON(data []byte) error {
66 if internal.UseOptimizedJSONUnmarshaling {
67 return jsonv2.Unmarshal(data, s)
68 }
69 var sw Swagger
70 if err := json.Unmarshal(data, &sw.SwaggerProps); err != nil {
71 return err
72 }
73 if err := json.Unmarshal(data, &sw.VendorExtensible); err != nil {
74 return err
75 }
76 *s = sw
77 return nil
78 }
79
80 func (s *Swagger) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
81 // Note: If you're willing to make breaking changes, it is possible to
82 // optimize this and other usages of this pattern:
83 // https://github.com/kubernetes/kube-openapi/pull/319#discussion_r983165948
84 var x struct {
85 Extensions Extensions `json:",inline"`
86 SwaggerProps
87 }
88
89 if err := jsonv2.UnmarshalDecode(dec, &x); err != nil {
90 return err
91 }
92 s.Extensions = internal.SanitizeExtensions(x.Extensions)
93 s.SwaggerProps = x.SwaggerProps
94 return nil
95 }
96
97 // SwaggerProps captures the top-level properties of an Api specification
98 //
99 // NOTE: validation rules
100 // - the scheme, when present must be from [http, https, ws, wss]
101 // - BasePath must start with a leading "/"
102 // - Paths is required
103 type SwaggerProps struct {
104 ID string `json:"id,omitempty"`
105 Consumes []string `json:"consumes,omitempty"`
106 Produces []string `json:"produces,omitempty"`
107 Schemes []string `json:"schemes,omitempty"`
108 Swagger string `json:"swagger,omitempty"`
109 Info *Info `json:"info,omitempty"`
110 Host string `json:"host,omitempty"`
111 BasePath string `json:"basePath,omitempty"`
112 Paths *Paths `json:"paths"`
113 Definitions Definitions `json:"definitions,omitempty"`
114 Parameters map[string]Parameter `json:"parameters,omitempty"`
115 Responses map[string]Response `json:"responses,omitempty"`
116 SecurityDefinitions SecurityDefinitions `json:"securityDefinitions,omitempty"`
117 Security []map[string][]string `json:"security,omitempty"`
118 Tags []Tag `json:"tags,omitempty"`
119 ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
120 }
121
122 // Dependencies represent a dependencies property
123 type Dependencies map[string]SchemaOrStringArray
124
125 // SchemaOrBool represents a schema or boolean value, is biased towards true for the boolean property
126 type SchemaOrBool struct {
127 Allows bool
128 Schema *Schema
129 }
130
131 var jsTrue = []byte("true")
132 var jsFalse = []byte("false")
133
134 // MarshalJSON convert this object to JSON
135 func (s SchemaOrBool) MarshalJSON() ([]byte, error) {
136 if internal.UseOptimizedJSONMarshaling {
137 return internal.DeterministicMarshal(s)
138 }
139 if s.Schema != nil {
140 return json.Marshal(s.Schema)
141 }
142
143 if s.Schema == nil && !s.Allows {
144 return jsFalse, nil
145 }
146 return jsTrue, nil
147 }
148
149 // MarshalJSON convert this object to JSON
150 func (s SchemaOrBool) MarshalJSONTo(enc *jsontext.Encoder) error {
151 if s.Schema != nil {
152 return jsonv2.MarshalEncode(enc, s.Schema)
153 }
154
155 if s.Schema == nil && !s.Allows {
156 return enc.WriteToken(jsontext.False)
157 }
158 return enc.WriteToken(jsontext.True)
159 }
160
161 // UnmarshalJSON converts this bool or schema object from a JSON structure
162 func (s *SchemaOrBool) UnmarshalJSON(data []byte) error {
163 if internal.UseOptimizedJSONUnmarshaling {
164 return jsonv2.Unmarshal(data, s)
165 }
166
167 var nw SchemaOrBool
168 if len(data) > 0 && data[0] == '{' {
169 var sch Schema
170 if err := json.Unmarshal(data, &sch); err != nil {
171 return err
172 }
173 nw.Schema = &sch
174 nw.Allows = true
175 } else {
176 json.Unmarshal(data, &nw.Allows)
177 }
178 *s = nw
179 return nil
180 }
181
182 func (s *SchemaOrBool) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
183 switch k := dec.PeekKind(); k {
184 case '{':
185 err := jsonv2.UnmarshalDecode(dec, &s.Schema)
186 if err != nil {
187 return err
188 }
189 s.Allows = true
190 return nil
191 case 't', 'f':
192 err := jsonv2.UnmarshalDecode(dec, &s.Allows)
193 if err != nil {
194 return err
195 }
196 return nil
197 default:
198 return fmt.Errorf("expected object or bool, not '%v'", k.String())
199 }
200 }
201
202 // SchemaOrStringArray represents a schema or a string array
203 type SchemaOrStringArray struct {
204 Schema *Schema
205 Property []string
206 }
207
208 // MarshalJSON converts this schema object or array into JSON structure
209 func (s SchemaOrStringArray) MarshalJSON() ([]byte, error) {
210 if internal.UseOptimizedJSONMarshaling {
211 return internal.DeterministicMarshal(s)
212 }
213 if len(s.Property) > 0 {
214 return json.Marshal(s.Property)
215 }
216 if s.Schema != nil {
217 return json.Marshal(s.Schema)
218 }
219 return []byte("null"), nil
220 }
221
222 // MarshalJSON converts this schema object or array into JSON structure
223 func (s SchemaOrStringArray) MarshalJSONTo(enc *jsontext.Encoder) error {
224 if len(s.Property) > 0 {
225 return jsonv2.MarshalEncode(enc, s.Property)
226 }
227 if s.Schema != nil {
228 return jsonv2.MarshalEncode(enc, s.Schema)
229 }
230 return enc.WriteToken(jsontext.Null)
231 }
232
233 // UnmarshalJSON converts this schema object or array from a JSON structure
234 func (s *SchemaOrStringArray) UnmarshalJSON(data []byte) error {
235 if internal.UseOptimizedJSONUnmarshaling {
236 return jsonv2.Unmarshal(data, s)
237 }
238
239 var first byte
240 if len(data) > 1 {
241 first = data[0]
242 }
243 var nw SchemaOrStringArray
244 if first == '{' {
245 var sch Schema
246 if err := json.Unmarshal(data, &sch); err != nil {
247 return err
248 }
249 nw.Schema = &sch
250 }
251 if first == '[' {
252 if err := json.Unmarshal(data, &nw.Property); err != nil {
253 return err
254 }
255 }
256 *s = nw
257 return nil
258 }
259
260 func (s *SchemaOrStringArray) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
261 switch dec.PeekKind() {
262 case '{':
263 return jsonv2.UnmarshalDecode(dec, &s.Schema)
264 case '[':
265 return jsonv2.UnmarshalDecode(dec, &s.Property)
266 default:
267 _, err := dec.ReadValue()
268 return err
269 }
270 }
271
272 // Definitions contains the models explicitly defined in this spec
273 // An object to hold data types that can be consumed and produced by operations.
274 // These data types can be primitives, arrays or models.
275 //
276 // For more information: http://goo.gl/8us55a#definitionsObject
277 type Definitions map[string]Schema
278
279 // SecurityDefinitions a declaration of the security schemes available to be used in the specification.
280 // This does not enforce the security schemes on the operations and only serves to provide
281 // the relevant details for each scheme.
282 //
283 // For more information: http://goo.gl/8us55a#securityDefinitionsObject
284 type SecurityDefinitions map[string]*SecurityScheme
285
286 // StringOrArray represents a value that can either be a string
287 // or an array of strings. Mainly here for serialization purposes
288 type StringOrArray []string
289
290 // Contains returns true when the value is contained in the slice
291 func (s StringOrArray) Contains(value string) bool {
292 for _, str := range s {
293 if str == value {
294 return true
295 }
296 }
297 return false
298 }
299
300 // UnmarshalJSON unmarshals this string or array object from a JSON array or JSON string
301 func (s *StringOrArray) UnmarshalJSON(data []byte) error {
302 if internal.UseOptimizedJSONUnmarshaling {
303 return jsonv2.Unmarshal(data, s)
304 }
305
306 var first byte
307 if len(data) > 1 {
308 first = data[0]
309 }
310
311 if first == '[' {
312 var parsed []string
313 if err := json.Unmarshal(data, &parsed); err != nil {
314 return err
315 }
316 *s = StringOrArray(parsed)
317 return nil
318 }
319
320 var single interface{}
321 if err := json.Unmarshal(data, &single); err != nil {
322 return err
323 }
324 if single == nil {
325 return nil
326 }
327 switch v := single.(type) {
328 case string:
329 *s = StringOrArray([]string{v})
330 return nil
331 default:
332 return fmt.Errorf("only string or array is allowed, not %T", single)
333 }
334 }
335
336 func (s *StringOrArray) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
337 switch k := dec.PeekKind(); k {
338 case '[':
339 *s = StringOrArray{}
340 return jsonv2.UnmarshalDecode(dec, (*[]string)(s))
341 case '"':
342 *s = StringOrArray{""}
343 return jsonv2.UnmarshalDecode(dec, &(*s)[0])
344 case 'n':
345 // Throw out null token
346 _, _ = dec.ReadToken()
347 return nil
348 default:
349 return fmt.Errorf("expected string or array, not '%v'", k.String())
350 }
351 }
352
353 // MarshalJSON converts this string or array to a JSON array or JSON string
354 func (s StringOrArray) MarshalJSON() ([]byte, error) {
355 if len(s) == 1 {
356 return json.Marshal([]string(s)[0])
357 }
358 return json.Marshal([]string(s))
359 }
360
361 // SchemaOrArray represents a value that can either be a Schema
362 // or an array of Schema. Mainly here for serialization purposes
363 type SchemaOrArray struct {
364 Schema *Schema
365 Schemas []Schema
366 }
367
368 // Len returns the number of schemas in this property
369 func (s SchemaOrArray) Len() int {
370 if s.Schema != nil {
371 return 1
372 }
373 return len(s.Schemas)
374 }
375
376 // ContainsType returns true when one of the schemas is of the specified type
377 func (s *SchemaOrArray) ContainsType(name string) bool {
378 if s.Schema != nil {
379 return s.Schema.Type != nil && s.Schema.Type.Contains(name)
380 }
381 return false
382 }
383
384 // MarshalJSON converts this schema object or array into JSON structure
385 func (s SchemaOrArray) MarshalJSON() ([]byte, error) {
386 if internal.UseOptimizedJSONMarshaling {
387 return internal.DeterministicMarshal(s)
388 }
389 if s.Schemas != nil {
390 return json.Marshal(s.Schemas)
391 }
392 return json.Marshal(s.Schema)
393 }
394
395 // MarshalJSON converts this schema object or array into JSON structure
396 func (s SchemaOrArray) MarshalJSONTo(enc *jsontext.Encoder) error {
397 if s.Schemas != nil {
398 return jsonv2.MarshalEncode(enc, s.Schemas)
399 }
400 return jsonv2.MarshalEncode(enc, s.Schema)
401 }
402
403 // UnmarshalJSON converts this schema object or array from a JSON structure
404 func (s *SchemaOrArray) UnmarshalJSON(data []byte) error {
405 if internal.UseOptimizedJSONUnmarshaling {
406 return jsonv2.Unmarshal(data, s)
407 }
408
409 var nw SchemaOrArray
410 var first byte
411 if len(data) > 1 {
412 first = data[0]
413 }
414 if first == '{' {
415 var sch Schema
416 if err := json.Unmarshal(data, &sch); err != nil {
417 return err
418 }
419 nw.Schema = &sch
420 }
421 if first == '[' {
422 if err := json.Unmarshal(data, &nw.Schemas); err != nil {
423 return err
424 }
425 }
426 *s = nw
427 return nil
428 }
429
430 func (s *SchemaOrArray) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
431 switch dec.PeekKind() {
432 case '{':
433 return jsonv2.UnmarshalDecode(dec, &s.Schema)
434 case '[':
435 return jsonv2.UnmarshalDecode(dec, &s.Schemas)
436 default:
437 _, err := dec.ReadValue()
438 return err
439 }
440 }