parameter.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2015 go-swagger maintainers
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package spec
  15. import (
  16. "encoding/json"
  17. "github.com/go-openapi/swag"
  18. "k8s.io/kube-openapi/pkg/internal"
  19. jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json"
  20. )
  21. // ParamProps describes the specific attributes of an operation parameter
  22. //
  23. // NOTE:
  24. // - Schema is defined when "in" == "body": see validate
  25. // - AllowEmptyValue is allowed where "in" == "query" || "formData"
  26. type ParamProps struct {
  27. Description string `json:"description,omitempty"`
  28. Name string `json:"name,omitempty"`
  29. In string `json:"in,omitempty"`
  30. Required bool `json:"required,omitempty"`
  31. Schema *Schema `json:"schema,omitempty"`
  32. AllowEmptyValue bool `json:"allowEmptyValue,omitempty"`
  33. }
  34. // Marshaling structure only, always edit along with corresponding
  35. // struct (or compilation will fail).
  36. type paramPropsOmitZero struct {
  37. Description string `json:"description,omitempty"`
  38. Name string `json:"name,omitempty"`
  39. In string `json:"in,omitempty"`
  40. Required bool `json:"required,omitzero"`
  41. Schema *Schema `json:"schema,omitzero"`
  42. AllowEmptyValue bool `json:"allowEmptyValue,omitzero"`
  43. }
  44. // Parameter a unique parameter is defined by a combination of a [name](#parameterName) and [location](#parameterIn).
  45. //
  46. // There are five possible parameter types.
  47. // * Path - Used together with [Path Templating](#pathTemplating), where the parameter value is actually part
  48. //
  49. // of the operation's URL. This does not include the host or base path of the API. For example, in `/items/{itemId}`,
  50. // the path parameter is `itemId`.
  51. //
  52. // * Query - Parameters that are appended to the URL. For example, in `/items?id=###`, the query parameter is `id`.
  53. // * Header - Custom headers that are expected as part of the request.
  54. // * Body - The payload that's appended to the HTTP request. Since there can only be one payload, there can only be
  55. //
  56. // _one_ body parameter. The name of the body parameter has no effect on the parameter itself and is used for
  57. // documentation purposes only. Since Form parameters are also in the payload, body and form parameters cannot exist
  58. // together for the same operation.
  59. //
  60. // * Form - Used to describe the payload of an HTTP request when either `application/x-www-form-urlencoded` or
  61. //
  62. // `multipart/form-data` are used as the content type of the request (in Swagger's definition,
  63. // the [`consumes`](#operationConsumes) property of an operation). This is the only parameter type that can be used
  64. // to send files, thus supporting the `file` type. Since form parameters are sent in the payload, they cannot be
  65. // declared together with a body parameter for the same operation. Form parameters have a different format based on
  66. // the content-type used (for further details, consult http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4).
  67. // * `application/x-www-form-urlencoded` - Similar to the format of Query parameters but as a payload.
  68. // For example, `foo=1&bar=swagger` - both `foo` and `bar` are form parameters. This is normally used for simple
  69. // parameters that are being transferred.
  70. // * `multipart/form-data` - each parameter takes a section in the payload with an internal header.
  71. // For example, for the header `Content-Disposition: form-data; name="submit-name"` the name of the parameter is
  72. // `submit-name`. This type of form parameters is more commonly used for file transfers.
  73. //
  74. // For more information: http://goo.gl/8us55a#parameterObject
  75. type Parameter struct {
  76. Refable
  77. CommonValidations
  78. SimpleSchema
  79. VendorExtensible
  80. ParamProps
  81. }
  82. // UnmarshalJSON hydrates this items instance with the data from JSON
  83. func (p *Parameter) UnmarshalJSON(data []byte) error {
  84. if internal.UseOptimizedJSONUnmarshaling {
  85. return jsonv2.Unmarshal(data, p)
  86. }
  87. if err := json.Unmarshal(data, &p.CommonValidations); err != nil {
  88. return err
  89. }
  90. if err := json.Unmarshal(data, &p.Refable); err != nil {
  91. return err
  92. }
  93. if err := json.Unmarshal(data, &p.SimpleSchema); err != nil {
  94. return err
  95. }
  96. if err := json.Unmarshal(data, &p.VendorExtensible); err != nil {
  97. return err
  98. }
  99. return json.Unmarshal(data, &p.ParamProps)
  100. }
  101. func (p *Parameter) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  102. var x struct {
  103. CommonValidations
  104. SimpleSchema
  105. Extensions
  106. ParamProps
  107. }
  108. if err := opts.UnmarshalNext(dec, &x); err != nil {
  109. return err
  110. }
  111. if err := p.Refable.Ref.fromMap(x.Extensions); err != nil {
  112. return err
  113. }
  114. p.CommonValidations = x.CommonValidations
  115. p.SimpleSchema = x.SimpleSchema
  116. p.Extensions = internal.SanitizeExtensions(x.Extensions)
  117. p.ParamProps = x.ParamProps
  118. return nil
  119. }
  120. // MarshalJSON converts this items object to JSON
  121. func (p Parameter) MarshalJSON() ([]byte, error) {
  122. if internal.UseOptimizedJSONMarshaling {
  123. return internal.DeterministicMarshal(p)
  124. }
  125. b1, err := json.Marshal(p.CommonValidations)
  126. if err != nil {
  127. return nil, err
  128. }
  129. b2, err := json.Marshal(p.SimpleSchema)
  130. if err != nil {
  131. return nil, err
  132. }
  133. b3, err := json.Marshal(p.Refable)
  134. if err != nil {
  135. return nil, err
  136. }
  137. b4, err := json.Marshal(p.VendorExtensible)
  138. if err != nil {
  139. return nil, err
  140. }
  141. b5, err := json.Marshal(p.ParamProps)
  142. if err != nil {
  143. return nil, err
  144. }
  145. return swag.ConcatJSON(b3, b1, b2, b4, b5), nil
  146. }
  147. func (p Parameter) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
  148. var x struct {
  149. CommonValidations commonValidationsOmitZero `json:",inline"`
  150. SimpleSchema simpleSchemaOmitZero `json:",inline"`
  151. ParamProps paramPropsOmitZero `json:",inline"`
  152. Ref string `json:"$ref,omitempty"`
  153. Extensions
  154. }
  155. x.CommonValidations = commonValidationsOmitZero(p.CommonValidations)
  156. x.SimpleSchema = simpleSchemaOmitZero(p.SimpleSchema)
  157. x.Extensions = internal.SanitizeExtensions(p.Extensions)
  158. x.ParamProps = paramPropsOmitZero(p.ParamProps)
  159. x.Ref = p.Refable.Ref.String()
  160. return opts.MarshalNext(enc, x)
  161. }