responses.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. b1, err := json.Marshal(r.ResponsesProps)
  60. if err != nil {
  61. return nil, err
  62. }
  63. b2, err := json.Marshal(r.VendorExtensible)
  64. if err != nil {
  65. return nil, err
  66. }
  67. concated := swag.ConcatJSON(b1, b2)
  68. return concated, nil
  69. }
  70. // ResponsesProps describes all responses for an operation.
  71. // It tells what is the default response and maps all responses with a
  72. // HTTP status code.
  73. type ResponsesProps struct {
  74. Default *Response
  75. StatusCodeResponses map[int]Response
  76. }
  77. // MarshalJSON marshals responses as JSON
  78. func (r ResponsesProps) MarshalJSON() ([]byte, error) {
  79. toser := map[string]Response{}
  80. if r.Default != nil {
  81. toser["default"] = *r.Default
  82. }
  83. for k, v := range r.StatusCodeResponses {
  84. toser[strconv.Itoa(k)] = v
  85. }
  86. return json.Marshal(toser)
  87. }
  88. // UnmarshalJSON unmarshals responses from JSON
  89. func (r *ResponsesProps) UnmarshalJSON(data []byte) error {
  90. if internal.UseOptimizedJSONUnmarshaling {
  91. return jsonv2.Unmarshal(data, r)
  92. }
  93. var res map[string]json.RawMessage
  94. if err := json.Unmarshal(data, &res); err != nil {
  95. return err
  96. }
  97. if v, ok := res["default"]; ok {
  98. value := Response{}
  99. if err := json.Unmarshal(v, &value); err != nil {
  100. return err
  101. }
  102. r.Default = &value
  103. delete(res, "default")
  104. }
  105. for k, v := range res {
  106. // Take all integral keys
  107. if nk, err := strconv.Atoi(k); err == nil {
  108. if r.StatusCodeResponses == nil {
  109. r.StatusCodeResponses = map[int]Response{}
  110. }
  111. value := Response{}
  112. if err := json.Unmarshal(v, &value); err != nil {
  113. return err
  114. }
  115. r.StatusCodeResponses[nk] = value
  116. }
  117. }
  118. return nil
  119. }
  120. func (r *Responses) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) (err error) {
  121. tok, err := dec.ReadToken()
  122. if err != nil {
  123. return err
  124. }
  125. var ext any
  126. var resp Response
  127. switch k := tok.Kind(); k {
  128. case 'n':
  129. return nil // noop
  130. case '{':
  131. for {
  132. tok, err := dec.ReadToken()
  133. if err != nil {
  134. return err
  135. }
  136. if tok.Kind() == '}' {
  137. return nil
  138. }
  139. switch k := tok.String(); {
  140. case isExtensionKey(k):
  141. ext = nil
  142. if err := opts.UnmarshalNext(dec, &ext); err != nil {
  143. return err
  144. }
  145. if r.Extensions == nil {
  146. r.Extensions = make(map[string]any)
  147. }
  148. r.Extensions[k] = ext
  149. case k == "default":
  150. resp = Response{}
  151. if err := opts.UnmarshalNext(dec, &resp); err != nil {
  152. return err
  153. }
  154. respCopy := resp
  155. r.ResponsesProps.Default = &respCopy
  156. default:
  157. if nk, err := strconv.Atoi(k); err == nil {
  158. resp = Response{}
  159. if err := opts.UnmarshalNext(dec, &resp); err != nil {
  160. return err
  161. }
  162. if r.StatusCodeResponses == nil {
  163. r.StatusCodeResponses = map[int]Response{}
  164. }
  165. r.StatusCodeResponses[nk] = resp
  166. }
  167. }
  168. }
  169. default:
  170. return fmt.Errorf("unknown JSON kind: %v", k)
  171. }
  172. }