Debian Codegraph @

pkg/spec3/parameter.go

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