request_body.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. "github.com/go-openapi/swag"
  17. "k8s.io/kube-openapi/pkg/internal"
  18. jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json"
  19. "k8s.io/kube-openapi/pkg/validation/spec"
  20. )
  21. // RequestBody describes a single request body, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#requestBodyObject
  22. //
  23. // Note that this struct is actually a thin wrapper around RequestBodyProps to make it referable and extensible
  24. type RequestBody struct {
  25. spec.Refable
  26. RequestBodyProps
  27. spec.VendorExtensible
  28. }
  29. // MarshalJSON is a custom marshal function that knows how to encode RequestBody as JSON
  30. func (r *RequestBody) MarshalJSON() ([]byte, error) {
  31. if internal.UseOptimizedJSONMarshalingV3 {
  32. return internal.DeterministicMarshal(r)
  33. }
  34. b1, err := json.Marshal(r.Refable)
  35. if err != nil {
  36. return nil, err
  37. }
  38. b2, err := json.Marshal(r.RequestBodyProps)
  39. if err != nil {
  40. return nil, err
  41. }
  42. b3, err := json.Marshal(r.VendorExtensible)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return swag.ConcatJSON(b1, b2, b3), nil
  47. }
  48. func (r *RequestBody) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
  49. var x struct {
  50. Ref string `json:"$ref,omitempty"`
  51. RequestBodyProps requestBodyPropsOmitZero `json:",inline"`
  52. spec.Extensions
  53. }
  54. x.Ref = r.Refable.Ref.String()
  55. x.Extensions = internal.SanitizeExtensions(r.Extensions)
  56. x.RequestBodyProps = requestBodyPropsOmitZero(r.RequestBodyProps)
  57. return opts.MarshalNext(enc, x)
  58. }
  59. func (r *RequestBody) UnmarshalJSON(data []byte) error {
  60. if internal.UseOptimizedJSONUnmarshalingV3 {
  61. return jsonv2.Unmarshal(data, r)
  62. }
  63. if err := json.Unmarshal(data, &r.Refable); err != nil {
  64. return err
  65. }
  66. if err := json.Unmarshal(data, &r.RequestBodyProps); err != nil {
  67. return err
  68. }
  69. if err := json.Unmarshal(data, &r.VendorExtensible); err != nil {
  70. return err
  71. }
  72. return nil
  73. }
  74. // RequestBodyProps describes a single request body, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#requestBodyObject
  75. type RequestBodyProps struct {
  76. // Description holds a brief description of the request body
  77. Description string `json:"description,omitempty"`
  78. // Content is the content of the request body. The key is a media type or media type range and the value describes it
  79. Content map[string]*MediaType `json:"content,omitempty"`
  80. // Required determines if the request body is required in the request
  81. Required bool `json:"required,omitempty"`
  82. }
  83. type requestBodyPropsOmitZero struct {
  84. Description string `json:"description,omitempty"`
  85. Content map[string]*MediaType `json:"content,omitempty"`
  86. Required bool `json:"required,omitzero"`
  87. }
  88. func (r *RequestBody) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  89. var x struct {
  90. spec.Extensions
  91. RequestBodyProps
  92. }
  93. if err := opts.UnmarshalNext(dec, &x); err != nil {
  94. return err
  95. }
  96. if err := internal.JSONRefFromMap(&r.Ref.Ref, x.Extensions); err != nil {
  97. return err
  98. }
  99. r.Extensions = internal.SanitizeExtensions(x.Extensions)
  100. r.RequestBodyProps = x.RequestBodyProps
  101. return nil
  102. }