response.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. Copyright 2021 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package spec3
  14. import (
  15. "encoding/json"
  16. "strconv"
  17. "k8s.io/kube-openapi/pkg/validation/spec"
  18. "github.com/go-openapi/swag"
  19. )
  20. // Responses holds the list of possible responses as they are returned from executing this operation
  21. //
  22. // Note that this struct is actually a thin wrapper around ResponsesProps to make it referable and extensible
  23. type Responses struct {
  24. ResponsesProps
  25. spec.VendorExtensible
  26. }
  27. // MarshalJSON is a custom marshal function that knows how to encode Responses as JSON
  28. func (r *Responses) MarshalJSON() ([]byte, error) {
  29. b1, err := json.Marshal(r.ResponsesProps)
  30. if err != nil {
  31. return nil, err
  32. }
  33. b2, err := json.Marshal(r.VendorExtensible)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return swag.ConcatJSON(b1, b2), nil
  38. }
  39. func (r *Responses) UnmarshalJSON(data []byte) error {
  40. if err := json.Unmarshal(data, &r.ResponsesProps); err != nil {
  41. return err
  42. }
  43. if err := json.Unmarshal(data, &r.VendorExtensible); err != nil {
  44. return err
  45. }
  46. return nil
  47. }
  48. // ResponsesProps holds the list of possible responses as they are returned from executing this operation
  49. type ResponsesProps struct {
  50. // Default holds the documentation of responses other than the ones declared for specific HTTP response codes. Use this field to cover undeclared responses
  51. Default *Response `json:"-"`
  52. // StatusCodeResponses holds a map of any HTTP status code to the response definition
  53. StatusCodeResponses map[int]*Response `json:"-"`
  54. }
  55. // MarshalJSON is a custom marshal function that knows how to encode ResponsesProps as JSON
  56. func (r ResponsesProps) MarshalJSON() ([]byte, error) {
  57. toser := map[string]*Response{}
  58. if r.Default != nil {
  59. toser["default"] = r.Default
  60. }
  61. for k, v := range r.StatusCodeResponses {
  62. toser[strconv.Itoa(k)] = v
  63. }
  64. return json.Marshal(toser)
  65. }
  66. // UnmarshalJSON unmarshals responses from JSON
  67. func (r *ResponsesProps) UnmarshalJSON(data []byte) error {
  68. var res map[string]*Response
  69. if err := json.Unmarshal(data, &res); err != nil {
  70. return nil
  71. }
  72. if v, ok := res["default"]; ok {
  73. r.Default = v
  74. delete(res, "default")
  75. }
  76. for k, v := range res {
  77. if nk, err := strconv.Atoi(k); err == nil {
  78. if r.StatusCodeResponses == nil {
  79. r.StatusCodeResponses = map[int]*Response{}
  80. }
  81. r.StatusCodeResponses[nk] = v
  82. }
  83. }
  84. return nil
  85. }
  86. // Response describes a single response from an API Operation, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#responseObject
  87. //
  88. // Note that this struct is actually a thin wrapper around ResponseProps to make it referable and extensible
  89. type Response struct {
  90. spec.Refable
  91. ResponseProps
  92. spec.VendorExtensible
  93. }
  94. // MarshalJSON is a custom marshal function that knows how to encode Response as JSON
  95. func (r *Response) MarshalJSON() ([]byte, error) {
  96. b1, err := json.Marshal(r.Refable)
  97. if err != nil {
  98. return nil, err
  99. }
  100. b2, err := json.Marshal(r.ResponseProps)
  101. if err != nil {
  102. return nil, err
  103. }
  104. b3, err := json.Marshal(r.VendorExtensible)
  105. if err != nil {
  106. return nil, err
  107. }
  108. return swag.ConcatJSON(b1, b2, b3), nil
  109. }
  110. func (r *Response) UnmarshalJSON(data []byte) error {
  111. if err := json.Unmarshal(data, &r.Refable); err != nil {
  112. return err
  113. }
  114. if err := json.Unmarshal(data, &r.ResponseProps); err != nil {
  115. return err
  116. }
  117. if err := json.Unmarshal(data, &r.VendorExtensible); err != nil {
  118. return err
  119. }
  120. return nil
  121. }
  122. // ResponseProps describes a single response from an API Operation, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#responseObject
  123. type ResponseProps struct {
  124. // Description holds a short description of the response
  125. Description string `json:"description,omitempty"`
  126. // Headers holds a maps of a headers name to its definition
  127. Headers map[string]*Header `json:"headers,omitempty"`
  128. // Content holds a map containing descriptions of potential response payloads
  129. Content map[string]*MediaType `json:"content,omitempty"`
  130. // Links is a map of operations links that can be followed from the response
  131. Links map[string]*Link `json:"links,omitempty"`
  132. }
  133. // Link represents a possible design-time link for a response, more at https://swagger.io/specification/#link-object
  134. type Link struct {
  135. spec.Refable
  136. LinkProps
  137. spec.VendorExtensible
  138. }
  139. // MarshalJSON is a custom marshal function that knows how to encode Link as JSON
  140. func (r *Link) MarshalJSON() ([]byte, error) {
  141. b1, err := json.Marshal(r.Refable)
  142. if err != nil {
  143. return nil, err
  144. }
  145. b2, err := json.Marshal(r.LinkProps)
  146. if err != nil {
  147. return nil, err
  148. }
  149. b3, err := json.Marshal(r.VendorExtensible)
  150. if err != nil {
  151. return nil, err
  152. }
  153. return swag.ConcatJSON(b1, b2, b3), nil
  154. }
  155. func (r *Link) UnmarshalJSON(data []byte) error {
  156. if err := json.Unmarshal(data, &r.Refable); err != nil {
  157. return err
  158. }
  159. if err := json.Unmarshal(data, &r.LinkProps); err != nil {
  160. return err
  161. }
  162. if err := json.Unmarshal(data, &r.VendorExtensible); err != nil {
  163. return err
  164. }
  165. return nil
  166. }
  167. // LinkProps describes a single response from an API Operation, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#responseObject
  168. type LinkProps struct {
  169. // OperationId is the name of an existing, resolvable OAS operation
  170. OperationId string `json:"operationId,omitempty"`
  171. // Parameters is a map representing parameters to pass to an operation as specified with operationId or identified via operationRef
  172. Parameters map[string]interface{} `json:"parameters,omitempty"`
  173. // Description holds a description of the link
  174. Description string `json:"description,omitempty"`
  175. // RequestBody is a literal value or expresion to use as a request body when calling the target operation
  176. RequestBody interface{} `json:"requestBody,omitempty"`
  177. // Server holds a server object used by the target operation
  178. Server *Server `json:"server,omitempty"`
  179. }