header.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. // Header a struct that describes a single operation parameter, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#parameterObject
  22. //
  23. // Note that this struct is actually a thin wrapper around HeaderProps to make it referable and extensible
  24. type Header struct {
  25. spec.Refable
  26. HeaderProps
  27. spec.VendorExtensible
  28. }
  29. // MarshalJSON is a custom marshal function that knows how to encode Header as JSON
  30. func (h *Header) MarshalJSON() ([]byte, error) {
  31. if internal.UseOptimizedJSONMarshalingV3 {
  32. return internal.DeterministicMarshal(h)
  33. }
  34. b1, err := json.Marshal(h.Refable)
  35. if err != nil {
  36. return nil, err
  37. }
  38. b2, err := json.Marshal(h.HeaderProps)
  39. if err != nil {
  40. return nil, err
  41. }
  42. b3, err := json.Marshal(h.VendorExtensible)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return swag.ConcatJSON(b1, b2, b3), nil
  47. }
  48. func (h *Header) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
  49. var x struct {
  50. Ref string `json:"$ref,omitempty"`
  51. HeaderProps headerPropsOmitZero `json:",inline"`
  52. spec.Extensions
  53. }
  54. x.Ref = h.Refable.Ref.String()
  55. x.Extensions = internal.SanitizeExtensions(h.Extensions)
  56. x.HeaderProps = headerPropsOmitZero(h.HeaderProps)
  57. return opts.MarshalNext(enc, x)
  58. }
  59. func (h *Header) UnmarshalJSON(data []byte) error {
  60. if internal.UseOptimizedJSONUnmarshalingV3 {
  61. return jsonv2.Unmarshal(data, h)
  62. }
  63. if err := json.Unmarshal(data, &h.Refable); err != nil {
  64. return err
  65. }
  66. if err := json.Unmarshal(data, &h.HeaderProps); err != nil {
  67. return err
  68. }
  69. if err := json.Unmarshal(data, &h.VendorExtensible); err != nil {
  70. return err
  71. }
  72. return nil
  73. }
  74. func (h *Header) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  75. var x struct {
  76. spec.Extensions
  77. HeaderProps
  78. }
  79. if err := opts.UnmarshalNext(dec, &x); err != nil {
  80. return err
  81. }
  82. if err := internal.JSONRefFromMap(&h.Ref.Ref, x.Extensions); err != nil {
  83. return err
  84. }
  85. h.Extensions = internal.SanitizeExtensions(x.Extensions)
  86. h.HeaderProps = x.HeaderProps
  87. return nil
  88. }
  89. // HeaderProps a struct that describes a header object
  90. type HeaderProps struct {
  91. // Description holds a brief description of the parameter
  92. Description string `json:"description,omitempty"`
  93. // Required determines whether this parameter is mandatory
  94. Required bool `json:"required,omitempty"`
  95. // Deprecated declares this operation to be deprecated
  96. Deprecated bool `json:"deprecated,omitempty"`
  97. // AllowEmptyValue sets the ability to pass empty-valued parameters
  98. AllowEmptyValue bool `json:"allowEmptyValue,omitempty"`
  99. // Style describes how the parameter value will be serialized depending on the type of the parameter value
  100. Style string `json:"style,omitempty"`
  101. // Explode when true, parameter values of type array or object generate separate parameters for each value of the array or key-value pair of the map
  102. Explode bool `json:"explode,omitempty"`
  103. // AllowReserved determines whether the parameter value SHOULD allow reserved characters, as defined by RFC3986
  104. AllowReserved bool `json:"allowReserved,omitempty"`
  105. // Schema holds the schema defining the type used for the parameter
  106. Schema *spec.Schema `json:"schema,omitempty"`
  107. // Content holds a map containing the representations for the parameter
  108. Content map[string]*MediaType `json:"content,omitempty"`
  109. // Example of the header
  110. Example interface{} `json:"example,omitempty"`
  111. // Examples of the header
  112. Examples map[string]*Example `json:"examples,omitempty"`
  113. }
  114. // Marshaling structure only, always edit along with corresponding
  115. // struct (or compilation will fail).
  116. type headerPropsOmitZero struct {
  117. Description string `json:"description,omitempty"`
  118. Required bool `json:"required,omitzero"`
  119. Deprecated bool `json:"deprecated,omitzero"`
  120. AllowEmptyValue bool `json:"allowEmptyValue,omitzero"`
  121. Style string `json:"style,omitempty"`
  122. Explode bool `json:"explode,omitzero"`
  123. AllowReserved bool `json:"allowReserved,omitzero"`
  124. Schema *spec.Schema `json:"schema,omitzero"`
  125. Content map[string]*MediaType `json:"content,omitempty"`
  126. Example interface{} `json:"example,omitempty"`
  127. Examples map[string]*Example `json:"examples,omitempty"`
  128. }