Debian Codegraph @

pkg/validation/spec/responses.go

raw
1 // Copyright 2015 go-swagger maintainers
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package spec
16
17 import (
18 "encoding/json"
19 "fmt"
20 "reflect"
21 "strconv"
22
23 "github.com/go-openapi/swag"
24 "k8s.io/kube-openapi/pkg/internal"
25 jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json"
26 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/jsontext"
27 )
28
29 // Responses is a container for the expected responses of an operation.
30 // The container maps a HTTP response code to the expected response.
31 // It is not expected from the documentation to necessarily cover all possible HTTP response codes,
32 // since they may not be known in advance. However, it is expected from the documentation to cover
33 // a successful operation response and any known errors.
34 //
35 // The `default` can be used a default response object for all HTTP codes that are not covered
36 // individually by the specification.
37 //
38 // The `Responses Object` MUST contain at least one response code, and it SHOULD be the response
39 // for a successful operation call.
40 //
41 // For more information: http://goo.gl/8us55a#responsesObject
42 type Responses struct {
43 VendorExtensible
44 ResponsesProps
45 }
46
47 // UnmarshalJSON hydrates this items instance with the data from JSON
48 func (r *Responses) UnmarshalJSON(data []byte) error {
49 if internal.UseOptimizedJSONUnmarshaling {
50 return jsonv2.Unmarshal(data, r)
51 }
52
53 if err := json.Unmarshal(data, &r.ResponsesProps); err != nil {
54 return err
55 }
56 if err := json.Unmarshal(data, &r.VendorExtensible); err != nil {
57 return err
58 }
59 if reflect.DeepEqual(ResponsesProps{}, r.ResponsesProps) {
60 r.ResponsesProps = ResponsesProps{}
61 }
62 return nil
63 }
64
65 // MarshalJSON converts this items object to JSON
66 func (r Responses) MarshalJSON() ([]byte, error) {
67 if internal.UseOptimizedJSONMarshaling {
68 return internal.DeterministicMarshal(r)
69 }
70 b1, err := json.Marshal(r.ResponsesProps)
71 if err != nil {
72 return nil, err
73 }
74 b2, err := json.Marshal(r.VendorExtensible)
75 if err != nil {
76 return nil, err
77 }
78 concated := swag.ConcatJSON(b1, b2)
79 return concated, nil
80 }
81
82 func (r Responses) MarshalJSONTo(enc *jsontext.Encoder) error {
83 type ArbitraryKeys map[string]interface{}
84 var x struct {
85 ArbitraryKeys ArbitraryKeys `json:",inline"`
86 Default *Response `json:"default,omitempty"`
87 }
88 x.ArbitraryKeys = make(map[string]any, len(r.Extensions)+len(r.StatusCodeResponses))
89 for k, v := range r.Extensions {
90 if internal.IsExtensionKey(k) {
91 x.ArbitraryKeys[k] = v
92 }
93 }
94 for k, v := range r.StatusCodeResponses {
95 x.ArbitraryKeys[strconv.Itoa(k)] = v
96 }
97 x.Default = r.Default
98 return jsonv2.MarshalEncode(enc, x)
99 }
100
101 // ResponsesProps describes all responses for an operation.
102 // It tells what is the default response and maps all responses with a
103 // HTTP status code.
104 type ResponsesProps struct {
105 Default *Response
106 StatusCodeResponses map[int]Response
107 }
108
109 // MarshalJSON marshals responses as JSON
110 func (r ResponsesProps) MarshalJSON() ([]byte, error) {
111 toser := map[string]Response{}
112 if r.Default != nil {
113 toser["default"] = *r.Default
114 }
115 for k, v := range r.StatusCodeResponses {
116 toser[strconv.Itoa(k)] = v
117 }
118 return json.Marshal(toser)
119 }
120
121 // UnmarshalJSON unmarshals responses from JSON
122 func (r *ResponsesProps) UnmarshalJSON(data []byte) error {
123 if internal.UseOptimizedJSONUnmarshaling {
124 return jsonv2.Unmarshal(data, r)
125 }
126 var res map[string]json.RawMessage
127 if err := json.Unmarshal(data, &res); err != nil {
128 return err
129 }
130 if v, ok := res["default"]; ok {
131 value := Response{}
132 if err := json.Unmarshal(v, &value); err != nil {
133 return err
134 }
135 r.Default = &value
136 delete(res, "default")
137 }
138 for k, v := range res {
139 // Take all integral keys
140 if nk, err := strconv.Atoi(k); err == nil {
141 if r.StatusCodeResponses == nil {
142 r.StatusCodeResponses = map[int]Response{}
143 }
144 value := Response{}
145 if err := json.Unmarshal(v, &value); err != nil {
146 return err
147 }
148 r.StatusCodeResponses[nk] = value
149 }
150 }
151 return nil
152 }
153
154 func (r *Responses) UnmarshalJSONFrom(dec *jsontext.Decoder) (err error) {
155 tok, err := dec.ReadToken()
156 if err != nil {
157 return err
158 }
159 var ext any
160 var resp Response
161 switch k := tok.Kind(); k {
162 case 'n':
163 return nil // noop
164 case '{':
165 for {
166 tok, err := dec.ReadToken()
167 if err != nil {
168 return err
169 }
170 if tok.Kind() == '}' {
171 return nil
172 }
173 switch k := tok.String(); {
174 case internal.IsExtensionKey(k):
175 ext = nil
176 if err := jsonv2.UnmarshalDecode(dec, &ext); err != nil {
177 return err
178 }
179
180 if r.Extensions == nil {
181 r.Extensions = make(map[string]any)
182 }
183 r.Extensions[k] = ext
184 case k == "default":
185 resp = Response{}
186 if err := jsonv2.UnmarshalDecode(dec, &resp); err != nil {
187 return err
188 }
189
190 respCopy := resp
191 r.ResponsesProps.Default = &respCopy
192 default:
193 if nk, err := strconv.Atoi(k); err == nil {
194 resp = Response{}
195 if err := jsonv2.UnmarshalDecode(dec, &resp); err != nil {
196 return err
197 }
198
199 if r.StatusCodeResponses == nil {
200 r.StatusCodeResponses = map[int]Response{}
201 }
202 r.StatusCodeResponses[nk] = resp
203 }
204 }
205 }
206 default:
207 return fmt.Errorf("unknown JSON kind: %v", k)
208 }
209 }