operation.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // Operation describes a single API operation on a path, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#operationObject
  22. //
  23. // Note that this struct is actually a thin wrapper around OperationProps to make it referable and extensible
  24. type Operation struct {
  25. OperationProps
  26. spec.VendorExtensible
  27. }
  28. // MarshalJSON is a custom marshal function that knows how to encode Operation as JSON
  29. func (o *Operation) MarshalJSON() ([]byte, error) {
  30. if internal.UseOptimizedJSONMarshalingV3 {
  31. return internal.DeterministicMarshal(o)
  32. }
  33. b1, err := json.Marshal(o.OperationProps)
  34. if err != nil {
  35. return nil, err
  36. }
  37. b2, err := json.Marshal(o.VendorExtensible)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return swag.ConcatJSON(b1, b2), nil
  42. }
  43. func (o *Operation) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
  44. var x struct {
  45. spec.Extensions
  46. OperationProps operationPropsOmitZero `json:",inline"`
  47. }
  48. x.Extensions = internal.SanitizeExtensions(o.Extensions)
  49. x.OperationProps = operationPropsOmitZero(o.OperationProps)
  50. return opts.MarshalNext(enc, x)
  51. }
  52. // UnmarshalJSON hydrates this items instance with the data from JSON
  53. func (o *Operation) UnmarshalJSON(data []byte) error {
  54. if internal.UseOptimizedJSONUnmarshalingV3 {
  55. return jsonv2.Unmarshal(data, o)
  56. }
  57. if err := json.Unmarshal(data, &o.OperationProps); err != nil {
  58. return err
  59. }
  60. return json.Unmarshal(data, &o.VendorExtensible)
  61. }
  62. func (o *Operation) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  63. var x struct {
  64. spec.Extensions
  65. OperationProps
  66. }
  67. if err := opts.UnmarshalNext(dec, &x); err != nil {
  68. return err
  69. }
  70. o.Extensions = internal.SanitizeExtensions(x.Extensions)
  71. o.OperationProps = x.OperationProps
  72. return nil
  73. }
  74. // OperationProps describes a single API operation on a path, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#operationObject
  75. type OperationProps struct {
  76. // Tags holds a list of tags for API documentation control
  77. Tags []string `json:"tags,omitempty"`
  78. // Summary holds a short summary of what the operation does
  79. Summary string `json:"summary,omitempty"`
  80. // Description holds a verbose explanation of the operation behavior
  81. Description string `json:"description,omitempty"`
  82. // ExternalDocs holds additional external documentation for this operation
  83. ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
  84. // OperationId holds a unique string used to identify the operation
  85. OperationId string `json:"operationId,omitempty"`
  86. // Parameters a list of parameters that are applicable for this operation
  87. Parameters []*Parameter `json:"parameters,omitempty"`
  88. // RequestBody holds the request body applicable for this operation
  89. RequestBody *RequestBody `json:"requestBody,omitempty"`
  90. // Responses holds the list of possible responses as they are returned from executing this operation
  91. Responses *Responses `json:"responses,omitempty"`
  92. // Deprecated declares this operation to be deprecated
  93. Deprecated bool `json:"deprecated,omitempty"`
  94. // SecurityRequirement holds a declaration of which security mechanisms can be used for this operation
  95. SecurityRequirement []map[string][]string `json:"security,omitempty"`
  96. // Servers contains an alternative server array to service this operation
  97. Servers []*Server `json:"servers,omitempty"`
  98. }
  99. type operationPropsOmitZero struct {
  100. Tags []string `json:"tags,omitempty"`
  101. Summary string `json:"summary,omitempty"`
  102. Description string `json:"description,omitempty"`
  103. ExternalDocs *ExternalDocumentation `json:"externalDocs,omitzero"`
  104. OperationId string `json:"operationId,omitempty"`
  105. Parameters []*Parameter `json:"parameters,omitempty"`
  106. RequestBody *RequestBody `json:"requestBody,omitzero"`
  107. Responses *Responses `json:"responses,omitzero"`
  108. Deprecated bool `json:"deprecated,omitzero"`
  109. SecurityRequirement []map[string][]string `json:"security,omitempty"`
  110. Servers []*Server `json:"servers,omitempty"`
  111. }