parameter.go 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/validation/spec"
  18. )
  19. // 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
  20. //
  21. // Note that this struct is actually a thin wrapper around ParameterProps to make it referable and extensible
  22. type Parameter struct {
  23. spec.Refable
  24. ParameterProps
  25. spec.VendorExtensible
  26. }
  27. // MarshalJSON is a custom marshal function that knows how to encode Parameter as JSON
  28. func (p *Parameter) MarshalJSON() ([]byte, error) {
  29. b1, err := json.Marshal(p.Refable)
  30. if err != nil {
  31. return nil, err
  32. }
  33. b2, err := json.Marshal(p.ParameterProps)
  34. if err != nil {
  35. return nil, err
  36. }
  37. b3, err := json.Marshal(p.VendorExtensible)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return swag.ConcatJSON(b1, b2, b3), nil
  42. }
  43. func (p *Parameter) UnmarshalJSON(data []byte) error {
  44. if err := json.Unmarshal(data, &p.Refable); err != nil {
  45. return err
  46. }
  47. if err := json.Unmarshal(data, &p.ParameterProps); err != nil {
  48. return err
  49. }
  50. if err := json.Unmarshal(data, &p.VendorExtensible); err != nil {
  51. return err
  52. }
  53. return nil
  54. }
  55. // 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
  56. type ParameterProps struct {
  57. // Name holds the name of the parameter
  58. Name string `json:"name,omitempty"`
  59. // In holds the location of the parameter
  60. In string `json:"in,omitempty"`
  61. // Description holds a brief description of the parameter
  62. Description string `json:"description,omitempty"`
  63. // Required determines whether this parameter is mandatory
  64. Required bool `json:"required,omitempty"`
  65. // Deprecated declares this operation to be deprecated
  66. Deprecated bool `json:"deprecated,omitempty"`
  67. // AllowEmptyValue sets the ability to pass empty-valued parameters
  68. AllowEmptyValue bool `json:"allowEmptyValue,omitempty"`
  69. // Style describes how the parameter value will be serialized depending on the type of the parameter value
  70. Style string `json:"style,omitempty"`
  71. // 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
  72. Explode bool `json:"explode,omitempty"`
  73. // AllowReserved determines whether the parameter value SHOULD allow reserved characters, as defined by RFC3986
  74. AllowReserved bool `json:"allowReserved,omitempty"`
  75. // Schema holds the schema defining the type used for the parameter
  76. Schema *spec.Schema `json:"schema,omitempty"`
  77. // Content holds a map containing the representations for the parameter
  78. Content map[string]*MediaType `json:"content,omitempty"`
  79. // Example of the parameter's potential value
  80. Example interface{} `json:"example,omitempty"`
  81. // Examples of the parameter's potential value. Each example SHOULD contain a value in the correct format as specified in the parameter encoding
  82. Examples map[string]*Example `json:"examples,omitempty"`
  83. }