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