operation.go 5.0 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. // OperationProps describes an operation
  22. //
  23. // NOTES:
  24. // - schemes, when present must be from [http, https, ws, wss]: see validate
  25. // - Security is handled as a special case: see MarshalJSON function
  26. type OperationProps struct {
  27. Description string `json:"description,omitempty"`
  28. Consumes []string `json:"consumes,omitempty"`
  29. Produces []string `json:"produces,omitempty"`
  30. Schemes []string `json:"schemes,omitempty"`
  31. Tags []string `json:"tags,omitempty"`
  32. Summary string `json:"summary,omitempty"`
  33. ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
  34. ID string `json:"operationId,omitempty"`
  35. Deprecated bool `json:"deprecated,omitempty"`
  36. Security []map[string][]string `json:"security,omitempty"`
  37. Parameters []Parameter `json:"parameters,omitempty"`
  38. Responses *Responses `json:"responses,omitempty"`
  39. }
  40. // Marshaling structure only, always edit along with corresponding
  41. // struct (or compilation will fail).
  42. type operationPropsOmitZero struct {
  43. Description string `json:"description,omitempty"`
  44. Consumes []string `json:"consumes,omitempty"`
  45. Produces []string `json:"produces,omitempty"`
  46. Schemes []string `json:"schemes,omitempty"`
  47. Tags []string `json:"tags,omitempty"`
  48. Summary string `json:"summary,omitempty"`
  49. ExternalDocs *ExternalDocumentation `json:"externalDocs,omitzero"`
  50. ID string `json:"operationId,omitempty"`
  51. Deprecated bool `json:"deprecated,omitempty,omitzero"`
  52. Security []map[string][]string `json:"security,omitempty"`
  53. Parameters []Parameter `json:"parameters,omitempty"`
  54. Responses *Responses `json:"responses,omitzero"`
  55. }
  56. // MarshalJSON takes care of serializing operation properties to JSON
  57. //
  58. // We use a custom marhaller here to handle a special cases related to
  59. // the Security field. We need to preserve zero length slice
  60. // while omitting the field when the value is nil/unset.
  61. func (op OperationProps) MarshalJSON() ([]byte, error) {
  62. type Alias OperationProps
  63. if op.Security == nil {
  64. return json.Marshal(&struct {
  65. Security []map[string][]string `json:"security,omitempty"`
  66. *Alias
  67. }{
  68. Security: op.Security,
  69. Alias: (*Alias)(&op),
  70. })
  71. }
  72. return json.Marshal(&struct {
  73. Security []map[string][]string `json:"security"`
  74. *Alias
  75. }{
  76. Security: op.Security,
  77. Alias: (*Alias)(&op),
  78. })
  79. }
  80. // Operation describes a single API operation on a path.
  81. //
  82. // For more information: http://goo.gl/8us55a#operationObject
  83. type Operation struct {
  84. VendorExtensible
  85. OperationProps
  86. }
  87. // UnmarshalJSON hydrates this items instance with the data from JSON
  88. func (o *Operation) UnmarshalJSON(data []byte) error {
  89. if internal.UseOptimizedJSONUnmarshaling {
  90. return jsonv2.Unmarshal(data, o)
  91. }
  92. if err := json.Unmarshal(data, &o.OperationProps); err != nil {
  93. return err
  94. }
  95. return json.Unmarshal(data, &o.VendorExtensible)
  96. }
  97. func (o *Operation) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  98. type OperationPropsNoMethods OperationProps // strip MarshalJSON method
  99. var x struct {
  100. Extensions
  101. OperationPropsNoMethods
  102. }
  103. if err := opts.UnmarshalNext(dec, &x); err != nil {
  104. return err
  105. }
  106. o.Extensions = internal.SanitizeExtensions(x.Extensions)
  107. o.OperationProps = OperationProps(x.OperationPropsNoMethods)
  108. return nil
  109. }
  110. // MarshalJSON converts this items object to JSON
  111. func (o Operation) MarshalJSON() ([]byte, error) {
  112. if internal.UseOptimizedJSONMarshaling {
  113. return internal.DeterministicMarshal(o)
  114. }
  115. b1, err := json.Marshal(o.OperationProps)
  116. if err != nil {
  117. return nil, err
  118. }
  119. b2, err := json.Marshal(o.VendorExtensible)
  120. if err != nil {
  121. return nil, err
  122. }
  123. concated := swag.ConcatJSON(b1, b2)
  124. return concated, nil
  125. }
  126. func (o Operation) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
  127. var x struct {
  128. Extensions
  129. OperationProps operationPropsOmitZero `json:",inline"`
  130. }
  131. x.Extensions = internal.SanitizeExtensions(o.Extensions)
  132. x.OperationProps = operationPropsOmitZero(o.OperationProps)
  133. return opts.MarshalNext(enc, x)
  134. }