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