| 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 |
// Header a struct that describes a single operation parameter, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#parameterObject |
| 30 |
// |
| 31 |
// Note that this struct is actually a thin wrapper around HeaderProps to make it referable and extensible |
| 32 |
type Header struct { |
| 33 |
spec.Refable |
| 34 |
HeaderProps |
| 35 |
spec.VendorExtensible |
| 36 |
} |
| 37 |
|
| 38 |
// MarshalJSON is a custom marshal function that knows how to encode Header as JSON |
| 39 |
func (h *Header) MarshalJSON() ([]byte, error) { |
| 40 |
if internal.UseOptimizedJSONMarshalingV3 { |
| 41 |
return internal.DeterministicMarshal(h) |
| 42 |
} |
| 43 |
b1, err := json.Marshal(h.Refable) |
| 44 |
if err != nil { |
| 45 |
return nil, err |
| 46 |
} |
| 47 |
b2, err := json.Marshal(h.HeaderProps) |
| 48 |
if err != nil { |
| 49 |
return nil, err |
| 50 |
} |
| 51 |
b3, err := json.Marshal(h.VendorExtensible) |
| 52 |
if err != nil { |
| 53 |
return nil, err |
| 54 |
} |
| 55 |
return swag.ConcatJSON(b1, b2, b3), nil |
| 56 |
} |
| 57 |
|
| 58 |
func (h *Header) MarshalJSONTo(enc *jsontext.Encoder) error { |
| 59 |
var x struct { |
| 60 |
Ref string `json:"$ref,omitempty"` |
| 61 |
HeaderProps headerPropsOmitZero `json:",inline"` |
| 62 |
Extensions spec.Extensions `json:",inline"` |
| 63 |
} |
| 64 |
x.Ref = h.Refable.Ref.String() |
| 65 |
x.Extensions = internal.SanitizeExtensions(h.Extensions) |
| 66 |
x.HeaderProps = headerPropsOmitZero(h.HeaderProps) |
| 67 |
return jsonv2.MarshalEncode(enc, x) |
| 68 |
} |
| 69 |
|
| 70 |
func (h *Header) UnmarshalJSON(data []byte) error { |
| 71 |
if internal.UseOptimizedJSONUnmarshalingV3 { |
| 72 |
return jsonv2.Unmarshal(data, h) |
| 73 |
} |
| 74 |
if err := json.Unmarshal(data, &h.Refable); err != nil { |
| 75 |
return err |
| 76 |
} |
| 77 |
if err := json.Unmarshal(data, &h.HeaderProps); err != nil { |
| 78 |
return err |
| 79 |
} |
| 80 |
if err := json.Unmarshal(data, &h.VendorExtensible); err != nil { |
| 81 |
return err |
| 82 |
} |
| 83 |
|
| 84 |
return nil |
| 85 |
} |
| 86 |
|
| 87 |
func (h *Header) UnmarshalJSONFrom(dec *jsontext.Decoder) error { |
| 88 |
var x struct { |
| 89 |
Extensions spec.Extensions `json:",inline"` |
| 90 |
HeaderProps |
| 91 |
} |
| 92 |
if err := jsonv2.UnmarshalDecode(dec, &x); err != nil { |
| 93 |
return err |
| 94 |
} |
| 95 |
if err := internal.JSONRefFromMap(&h.Ref.Ref, x.Extensions); err != nil { |
| 96 |
return err |
| 97 |
} |
| 98 |
h.Extensions = internal.SanitizeExtensions(x.Extensions) |
| 99 |
h.HeaderProps = x.HeaderProps |
| 100 |
return nil |
| 101 |
} |
| 102 |
|
| 103 |
// HeaderProps a struct that describes a header object |
| 104 |
type HeaderProps struct { |
| 105 |
// Description holds a brief description of the parameter |
| 106 |
Description string `json:"description,omitempty"` |
| 107 |
// Required determines whether this parameter is mandatory |
| 108 |
Required bool `json:"required,omitempty"` |
| 109 |
// Deprecated declares this operation to be deprecated |
| 110 |
Deprecated bool `json:"deprecated,omitempty"` |
| 111 |
// AllowEmptyValue sets the ability to pass empty-valued parameters |
| 112 |
AllowEmptyValue bool `json:"allowEmptyValue,omitempty"` |
| 113 |
// Style describes how the parameter value will be serialized depending on the type of the parameter value |
| 114 |
Style string `json:"style,omitempty"` |
| 115 |
// Explode when true, parameter values of type array or object generate separate parameters for each value of the array or key-value pair of the map |
| 116 |
Explode bool `json:"explode,omitempty"` |
| 117 |
// AllowReserved determines whether the parameter value SHOULD allow reserved characters, as defined by RFC3986 |
| 118 |
AllowReserved bool `json:"allowReserved,omitempty"` |
| 119 |
// Schema holds the schema defining the type used for the parameter |
| 120 |
Schema *spec.Schema `json:"schema,omitempty"` |
| 121 |
// Content holds a map containing the representations for the parameter |
| 122 |
Content map[string]*MediaType `json:"content,omitempty"` |
| 123 |
// Example of the header |
| 124 |
Example interface{} `json:"example,omitempty"` |
| 125 |
// Examples of the header |
| 126 |
Examples map[string]*Example `json:"examples,omitempty"` |
| 127 |
} |
| 128 |
|
| 129 |
// Marshaling structure only, always edit along with corresponding |
| 130 |
// struct (or compilation will fail). |
| 131 |
type headerPropsOmitZero struct { |
| 132 |
Description string `json:"description,omitempty"` |
| 133 |
Required bool `json:"required,omitzero"` |
| 134 |
Deprecated bool `json:"deprecated,omitzero"` |
| 135 |
AllowEmptyValue bool `json:"allowEmptyValue,omitzero"` |
| 136 |
Style string `json:"style,omitempty"` |
| 137 |
Explode bool `json:"explode,omitzero"` |
| 138 |
AllowReserved bool `json:"allowReserved,omitzero"` |
| 139 |
Schema *spec.Schema `json:"schema,omitzero"` |
| 140 |
Content map[string]*MediaType `json:"content,omitempty"` |
| 141 |
Example interface{} `json:"example,omitempty"` |
| 142 |
Examples map[string]*Example `json:"examples,omitempty"` |
| 143 |
} |