Debian Codegraph @

pkg/validation/spec/responses.go

raw symbols SCIP
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 )
27
28 // Responses is a container for the expected responses of an operation.
29 // The container maps a HTTP response code to the expected response.
30 // It is not expected from the documentation to necessarily cover all possible HTTP response codes,
31 // since they may not be known in advance. However, it is expected from the documentation to cover
32 // a successful operation response and any known errors.
33 //
34 // The `default` can be used a default response object for all HTTP codes that are not covered
35 // individually by the specification.
36 //
37 // The `Responses Object` MUST contain at least one response code, and it SHOULD be the response
38 // for a successful operation call.
39 //
40 // For more information: http://goo.gl/8us55a#responsesObject
41 type Responses struct {
42 VendorExtensible
43 ResponsesProps
44 }
45
46 // UnmarshalJSON hydrates this items instance with the data from JSON
47 func (r *Responses) UnmarshalJSON(data []byte) error {
48 if internal.UseOptimizedJSONUnmarshaling {
49 return jsonv2.Unmarshal(data, r)
50 }
51
52 if err := json.Unmarshal(data, &r.ResponsesProps); err != nil {
53 return err
54 }
55 if err := json.Unmarshal(data, &r.VendorExtensible); err != nil {
56 return err
57 }
58 if reflect.DeepEqual(ResponsesProps{}, r.ResponsesProps) {
59 r.ResponsesProps = ResponsesProps{}
60 }
61 return nil
62 }
63
64 // MarshalJSON converts this items object to JSON
65 func (r Responses) MarshalJSON() ([]byte, error) {
66 if internal.UseOptimizedJSONMarshaling {
67 return internal.DeterministicMarshal(r)
68 }
69 b1, err := json.Marshal(r.ResponsesProps)
70 if err != nil {
71 return nil, err
72 }
73 b2, err := json.Marshal(r.VendorExtensible)
74 if err != nil {
75 return nil, err
76 }
77 concated := swag.ConcatJSON(b1, b2)
78 return concated, nil
79 }
80
81 func (r Responses) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
82 type ArbitraryKeys map[string]interface{}
83 var x struct {
84 ArbitraryKeys
85 Default *Response `json:"default,omitempty"`
86 }
87 x.ArbitraryKeys = make(map[string]any, len(r.Extensions)+len(r.StatusCodeResponses))
88 for k, v := range r.Extensions {
89 if internal.IsExtensionKey(k) {
90 x.ArbitraryKeys[k] = v
91 }
92 }
93 for k, v := range r.StatusCodeResponses {
94 x.ArbitraryKeys[strconv.Itoa(k)] = v
95 }
96 x.Default = r.Default
97 return opts.MarshalNext(enc, x)
98 }
99
100 // ResponsesProps describes all responses for an operation.
101 // It tells what is the default response and maps all responses with a
102 // HTTP status code.
103 type ResponsesProps struct {
104 Default *Response
105 StatusCodeResponses map[int]Response
106 }
107
108 // MarshalJSON marshals responses as JSON
109 func (r ResponsesProps) MarshalJSON() ([]byte, error) {
110 toser := map[string]Response{}
111 if r.Default != nil {
112 toser["default"] = *r.Default
113 }
114 for k, v := range r.StatusCodeResponses {
115 toser[strconv.Itoa(k)] = v
116 }
117 return json.Marshal(toser)
118 }
119
120 // UnmarshalJSON unmarshals responses from JSON
121 func (r *ResponsesProps) UnmarshalJSON(data []byte) error {
122 if internal.UseOptimizedJSONUnmarshaling {
123 return jsonv2.Unmarshal(data, r)
124 }
125 var res map[string]json.RawMessage
126 if err := json.Unmarshal(data, &res); err != nil {
127 return err
128 }
129 if v, ok := res["default"]; ok {
130 value := Response{}
131 if err := json.Unmarshal(v, &value); err != nil {
132 return err
133 }
134 r.Default = &value
135 delete(res, "default")
136 }
137 for k, v := range res {
138 // Take all integral keys
139 if nk, err := strconv.Atoi(k); err == nil {
140 if r.StatusCodeResponses == nil {
141 r.StatusCodeResponses = map[int]Response{}
142 }
143 value := Response{}
144 if err := json.Unmarshal(v, &value); err != nil {
145 return err
146 }
147 r.StatusCodeResponses[nk] = value
148 }
149 }
150 return nil
151 }
152
153 func (r *Responses) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) (err error) {
154 tok, err := dec.ReadToken()
155 if err != nil {
156 return err
157 }
158 var ext any
159 var resp Response
160 switch k := tok.Kind(); k {
161 case 'n':
162 return nil // noop
163 case '{':
164 for {
165 tok, err := dec.ReadToken()
166 if err != nil {
167 return err
168 }
169 if tok.Kind() == '}' {
170 return nil
171 }
172 switch k := tok.String(); {
173 case internal.IsExtensionKey(k):
174 ext = nil
175 if err := opts.UnmarshalNext(dec, &ext); err != nil {
176 return err
177 }
178
179 if r.Extensions == nil {
180 r.Extensions = make(map[string]any)
181 }
182 r.Extensions[k] = ext
183 case k == "default":
184 resp = Response{}
185 if err := opts.UnmarshalNext(dec, &resp); err != nil {
186 return err
187 }
188
189 respCopy := resp
190 r.ResponsesProps.Default = &respCopy
191 default:
192 if nk, err := strconv.Atoi(k); err == nil {
193 resp = Response{}
194 if err := opts.UnmarshalNext(dec, &resp); err != nil {
195 return err
196 }
197
198 if r.StatusCodeResponses == nil {
199 r.StatusCodeResponses = map[int]Response{}
200 }
201 r.StatusCodeResponses[nk] = resp
202 }
203 }
204 }
205 default:
206 return fmt.Errorf("unknown JSON kind: %v", k)
207 }
208 }