| 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 |
|
| 27 |
"k8s.io/kube-openapi/pkg/validation/spec" |
| 28 |
) |
| 29 |
|
| 30 |
// Example https://swagger.io/specification/#example-object |
| 31 |
|
| 32 |
type Example struct { |
| 33 |
spec.Refable |
| 34 |
ExampleProps |
| 35 |
spec.VendorExtensible |
| 36 |
} |
| 37 |
|
| 38 |
// MarshalJSON is a custom marshal function that knows how to encode RequestBody as JSON |
| 39 |
func (e *Example) MarshalJSON() ([]byte, error) { |
| 40 |
if internal.UseOptimizedJSONMarshalingV3 { |
| 41 |
return internal.DeterministicMarshal(e) |
| 42 |
} |
| 43 |
b1, err := json.Marshal(e.Refable) |
| 44 |
if err != nil { |
| 45 |
return nil, err |
| 46 |
} |
| 47 |
b2, err := json.Marshal(e.ExampleProps) |
| 48 |
if err != nil { |
| 49 |
return nil, err |
| 50 |
} |
| 51 |
b3, err := json.Marshal(e.VendorExtensible) |
| 52 |
if err != nil { |
| 53 |
return nil, err |
| 54 |
} |
| 55 |
return swag.ConcatJSON(b1, b2, b3), nil |
| 56 |
} |
| 57 |
func (e *Example) MarshalJSONTo(enc *jsontext.Encoder) error { |
| 58 |
var x struct { |
| 59 |
Ref string `json:"$ref,omitempty"` |
| 60 |
ExampleProps `json:",inline"` |
| 61 |
Extensions spec.Extensions `json:",inline"` |
| 62 |
} |
| 63 |
x.Ref = e.Refable.Ref.String() |
| 64 |
x.Extensions = internal.SanitizeExtensions(e.Extensions) |
| 65 |
x.ExampleProps = e.ExampleProps |
| 66 |
return jsonv2.MarshalEncode(enc, x) |
| 67 |
} |
| 68 |
|
| 69 |
func (e *Example) UnmarshalJSON(data []byte) error { |
| 70 |
if internal.UseOptimizedJSONUnmarshalingV3 { |
| 71 |
return jsonv2.Unmarshal(data, e) |
| 72 |
} |
| 73 |
if err := json.Unmarshal(data, &e.Refable); err != nil { |
| 74 |
return err |
| 75 |
} |
| 76 |
if err := json.Unmarshal(data, &e.ExampleProps); err != nil { |
| 77 |
return err |
| 78 |
} |
| 79 |
if err := json.Unmarshal(data, &e.VendorExtensible); err != nil { |
| 80 |
return err |
| 81 |
} |
| 82 |
return nil |
| 83 |
} |
| 84 |
|
| 85 |
func (e *Example) UnmarshalJSONFrom(dec *jsontext.Decoder) error { |
| 86 |
var x struct { |
| 87 |
Extensions spec.Extensions `json:",inline"` |
| 88 |
ExampleProps |
| 89 |
} |
| 90 |
if err := jsonv2.UnmarshalDecode(dec, &x); err != nil { |
| 91 |
return err |
| 92 |
} |
| 93 |
if err := internal.JSONRefFromMap(&e.Ref.Ref, x.Extensions); err != nil { |
| 94 |
return err |
| 95 |
} |
| 96 |
e.Extensions = internal.SanitizeExtensions(x.Extensions) |
| 97 |
e.ExampleProps = x.ExampleProps |
| 98 |
|
| 99 |
return nil |
| 100 |
} |
| 101 |
|
| 102 |
type ExampleProps struct { |
| 103 |
// Summary holds a short description of the example |
| 104 |
Summary string `json:"summary,omitempty"` |
| 105 |
// Description holds a long description of the example |
| 106 |
Description string `json:"description,omitempty"` |
| 107 |
// Embedded literal example. |
| 108 |
Value interface{} `json:"value,omitempty"` |
| 109 |
// A URL that points to the literal example. This provides the capability to reference examples that cannot easily be included in JSON or YAML documents. |
| 110 |
ExternalValue string `json:"externalValue,omitempty"` |
| 111 |
} |