operation.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // MarshalJSON takes care of serializing operation properties to JSON
  41. //
  42. // We use a custom marhaller here to handle a special cases related to
  43. // the Security field. We need to preserve zero length slice
  44. // while omitting the field when the value is nil/unset.
  45. func (op OperationProps) MarshalJSON() ([]byte, error) {
  46. type Alias OperationProps
  47. if op.Security == nil {
  48. return json.Marshal(&struct {
  49. Security []map[string][]string `json:"security,omitempty"`
  50. *Alias
  51. }{
  52. Security: op.Security,
  53. Alias: (*Alias)(&op),
  54. })
  55. }
  56. return json.Marshal(&struct {
  57. Security []map[string][]string `json:"security"`
  58. *Alias
  59. }{
  60. Security: op.Security,
  61. Alias: (*Alias)(&op),
  62. })
  63. }
  64. // Operation describes a single API operation on a path.
  65. //
  66. // For more information: http://goo.gl/8us55a#operationObject
  67. type Operation struct {
  68. VendorExtensible
  69. OperationProps
  70. }
  71. // UnmarshalJSON hydrates this items instance with the data from JSON
  72. func (o *Operation) UnmarshalJSON(data []byte) error {
  73. if internal.UseOptimizedJSONUnmarshaling {
  74. return jsonv2.Unmarshal(data, o)
  75. }
  76. if err := json.Unmarshal(data, &o.OperationProps); err != nil {
  77. return err
  78. }
  79. return json.Unmarshal(data, &o.VendorExtensible)
  80. }
  81. func (o *Operation) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  82. type OperationPropsNoMethods OperationProps // strip MarshalJSON method
  83. var x struct {
  84. Extensions
  85. OperationPropsNoMethods
  86. }
  87. if err := opts.UnmarshalNext(dec, &x); err != nil {
  88. return err
  89. }
  90. x.Extensions.sanitize()
  91. if len(x.Extensions) == 0 {
  92. x.Extensions = nil
  93. }
  94. o.VendorExtensible.Extensions = x.Extensions
  95. o.OperationProps = OperationProps(x.OperationPropsNoMethods)
  96. return nil
  97. }
  98. // MarshalJSON converts this items object to JSON
  99. func (o Operation) MarshalJSON() ([]byte, error) {
  100. b1, err := json.Marshal(o.OperationProps)
  101. if err != nil {
  102. return nil, err
  103. }
  104. b2, err := json.Marshal(o.VendorExtensible)
  105. if err != nil {
  106. return nil, err
  107. }
  108. concated := swag.ConcatJSON(b1, b2)
  109. return concated, nil
  110. }