parameter.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. // Parameter a unique parameter is defined by a combination of a [name](#parameterName) and [location](#parameterIn).
  35. //
  36. // There are five possible parameter types.
  37. // * Path - Used together with [Path Templating](#pathTemplating), where the parameter value is actually part
  38. //
  39. // of the operation's URL. This does not include the host or base path of the API. For example, in `/items/{itemId}`,
  40. // the path parameter is `itemId`.
  41. //
  42. // * Query - Parameters that are appended to the URL. For example, in `/items?id=###`, the query parameter is `id`.
  43. // * Header - Custom headers that are expected as part of the request.
  44. // * Body - The payload that's appended to the HTTP request. Since there can only be one payload, there can only be
  45. //
  46. // _one_ body parameter. The name of the body parameter has no effect on the parameter itself and is used for
  47. // documentation purposes only. Since Form parameters are also in the payload, body and form parameters cannot exist
  48. // together for the same operation.
  49. //
  50. // * Form - Used to describe the payload of an HTTP request when either `application/x-www-form-urlencoded` or
  51. //
  52. // `multipart/form-data` are used as the content type of the request (in Swagger's definition,
  53. // the [`consumes`](#operationConsumes) property of an operation). This is the only parameter type that can be used
  54. // to send files, thus supporting the `file` type. Since form parameters are sent in the payload, they cannot be
  55. // declared together with a body parameter for the same operation. Form parameters have a different format based on
  56. // the content-type used (for further details, consult http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4).
  57. // * `application/x-www-form-urlencoded` - Similar to the format of Query parameters but as a payload.
  58. // For example, `foo=1&bar=swagger` - both `foo` and `bar` are form parameters. This is normally used for simple
  59. // parameters that are being transferred.
  60. // * `multipart/form-data` - each parameter takes a section in the payload with an internal header.
  61. // For example, for the header `Content-Disposition: form-data; name="submit-name"` the name of the parameter is
  62. // `submit-name`. This type of form parameters is more commonly used for file transfers.
  63. //
  64. // For more information: http://goo.gl/8us55a#parameterObject
  65. type Parameter struct {
  66. Refable
  67. CommonValidations
  68. SimpleSchema
  69. VendorExtensible
  70. ParamProps
  71. }
  72. // UnmarshalJSON hydrates this items instance with the data from JSON
  73. func (p *Parameter) UnmarshalJSON(data []byte) error {
  74. if internal.UseOptimizedJSONUnmarshaling {
  75. return jsonv2.Unmarshal(data, p)
  76. }
  77. if err := json.Unmarshal(data, &p.CommonValidations); err != nil {
  78. return err
  79. }
  80. if err := json.Unmarshal(data, &p.Refable); err != nil {
  81. return err
  82. }
  83. if err := json.Unmarshal(data, &p.SimpleSchema); err != nil {
  84. return err
  85. }
  86. if err := json.Unmarshal(data, &p.VendorExtensible); err != nil {
  87. return err
  88. }
  89. return json.Unmarshal(data, &p.ParamProps)
  90. }
  91. func (p *Parameter) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  92. var x struct {
  93. CommonValidations
  94. SimpleSchema
  95. Extensions
  96. ParamProps
  97. }
  98. if err := opts.UnmarshalNext(dec, &x); err != nil {
  99. return err
  100. }
  101. if err := p.Refable.Ref.fromMap(x.Extensions); err != nil {
  102. return err
  103. }
  104. x.Extensions.sanitize()
  105. if len(x.Extensions) == 0 {
  106. x.Extensions = nil
  107. }
  108. p.CommonValidations = x.CommonValidations
  109. p.SimpleSchema = x.SimpleSchema
  110. p.VendorExtensible.Extensions = x.Extensions
  111. p.ParamProps = x.ParamProps
  112. return nil
  113. }
  114. // MarshalJSON converts this items object to JSON
  115. func (p Parameter) MarshalJSON() ([]byte, error) {
  116. b1, err := json.Marshal(p.CommonValidations)
  117. if err != nil {
  118. return nil, err
  119. }
  120. b2, err := json.Marshal(p.SimpleSchema)
  121. if err != nil {
  122. return nil, err
  123. }
  124. b3, err := json.Marshal(p.Refable)
  125. if err != nil {
  126. return nil, err
  127. }
  128. b4, err := json.Marshal(p.VendorExtensible)
  129. if err != nil {
  130. return nil, err
  131. }
  132. b5, err := json.Marshal(p.ParamProps)
  133. if err != nil {
  134. return nil, err
  135. }
  136. return swag.ConcatJSON(b3, b1, b2, b4, b5), nil
  137. }