| 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 |
type ExternalDocumentation struct { |
| 30 |
ExternalDocumentationProps |
| 31 |
spec.VendorExtensible |
| 32 |
} |
| 33 |
|
| 34 |
type ExternalDocumentationProps struct { |
| 35 |
// Description is a short description of the target documentation. CommonMark syntax MAY be used for rich text representation. |
| 36 |
Description string `json:"description,omitempty"` |
| 37 |
// URL is the URL for the target documentation. |
| 38 |
URL string `json:"url"` |
| 39 |
} |
| 40 |
|
| 41 |
// MarshalJSON is a custom marshal function that knows how to encode Responses as JSON |
| 42 |
func (e *ExternalDocumentation) MarshalJSON() ([]byte, error) { |
| 43 |
if internal.UseOptimizedJSONMarshalingV3 { |
| 44 |
return internal.DeterministicMarshal(e) |
| 45 |
} |
| 46 |
b1, err := json.Marshal(e.ExternalDocumentationProps) |
| 47 |
if err != nil { |
| 48 |
return nil, err |
| 49 |
} |
| 50 |
b2, err := json.Marshal(e.VendorExtensible) |
| 51 |
if err != nil { |
| 52 |
return nil, err |
| 53 |
} |
| 54 |
return swag.ConcatJSON(b1, b2), nil |
| 55 |
} |
| 56 |
|
| 57 |
func (e *ExternalDocumentation) MarshalJSONTo(enc *jsontext.Encoder) error { |
| 58 |
var x struct { |
| 59 |
ExternalDocumentationProps `json:",inline"` |
| 60 |
Extensions spec.Extensions `json:",inline"` |
| 61 |
} |
| 62 |
x.Extensions = internal.SanitizeExtensions(e.Extensions) |
| 63 |
x.ExternalDocumentationProps = e.ExternalDocumentationProps |
| 64 |
return jsonv2.MarshalEncode(enc, x) |
| 65 |
} |
| 66 |
|
| 67 |
func (e *ExternalDocumentation) UnmarshalJSON(data []byte) error { |
| 68 |
if internal.UseOptimizedJSONUnmarshalingV3 { |
| 69 |
return jsonv2.Unmarshal(data, e) |
| 70 |
} |
| 71 |
if err := json.Unmarshal(data, &e.ExternalDocumentationProps); err != nil { |
| 72 |
return err |
| 73 |
} |
| 74 |
if err := json.Unmarshal(data, &e.VendorExtensible); err != nil { |
| 75 |
return err |
| 76 |
} |
| 77 |
return nil |
| 78 |
} |
| 79 |
|
| 80 |
func (e *ExternalDocumentation) UnmarshalJSONFrom(dec *jsontext.Decoder) error { |
| 81 |
var x struct { |
| 82 |
Extensions spec.Extensions `json:",inline"` |
| 83 |
ExternalDocumentationProps |
| 84 |
} |
| 85 |
if err := jsonv2.UnmarshalDecode(dec, &x); err != nil { |
| 86 |
return err |
| 87 |
} |
| 88 |
e.Extensions = internal.SanitizeExtensions(x.Extensions) |
| 89 |
e.ExternalDocumentationProps = x.ExternalDocumentationProps |
| 90 |
return nil |
| 91 |
} |