request_body.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. "k8s.io/kube-openapi/pkg/validation/spec"
  17. "github.com/go-openapi/swag"
  18. )
  19. // RequestBody describes a single request body, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#requestBodyObject
  20. //
  21. // Note that this struct is actually a thin wrapper around RequestBodyProps to make it referable and extensible
  22. type RequestBody struct {
  23. spec.Refable
  24. RequestBodyProps
  25. spec.VendorExtensible
  26. }
  27. // MarshalJSON is a custom marshal function that knows how to encode RequestBody as JSON
  28. func (r *RequestBody) MarshalJSON() ([]byte, error) {
  29. b1, err := json.Marshal(r.Refable)
  30. if err != nil {
  31. return nil, err
  32. }
  33. b2, err := json.Marshal(r.RequestBodyProps)
  34. if err != nil {
  35. return nil, err
  36. }
  37. b3, err := json.Marshal(r.VendorExtensible)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return swag.ConcatJSON(b1, b2, b3), nil
  42. }
  43. func (r *RequestBody) UnmarshalJSON(data []byte) error {
  44. if err := json.Unmarshal(data, &r.Refable); err != nil {
  45. return err
  46. }
  47. if err := json.Unmarshal(data, &r.RequestBodyProps); err != nil {
  48. return err
  49. }
  50. if err := json.Unmarshal(data, &r.VendorExtensible); err != nil {
  51. return err
  52. }
  53. return nil
  54. }
  55. // RequestBodyProps describes a single request body, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#requestBodyObject
  56. type RequestBodyProps struct {
  57. // Description holds a brief description of the request body
  58. Description string `json:"description,omitempty"`
  59. // Content is the content of the request body. The key is a media type or media type range and the value describes it
  60. Content map[string]*MediaType `json:"content,omitempty"`
  61. // Required determines if the request body is required in the request
  62. Required bool `json:"required,omitempty"`
  63. }