parameter.go 5.5 KB

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