header.go 3.1 KB

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