responses.go 5.5 KB

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