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