path_item.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // PathItemProps the path item specific properties
  22. type PathItemProps struct {
  23. Get *Operation `json:"get,omitempty"`
  24. Put *Operation `json:"put,omitempty"`
  25. Post *Operation `json:"post,omitempty"`
  26. Delete *Operation `json:"delete,omitempty"`
  27. Options *Operation `json:"options,omitempty"`
  28. Head *Operation `json:"head,omitempty"`
  29. Patch *Operation `json:"patch,omitempty"`
  30. Parameters []Parameter `json:"parameters,omitempty"`
  31. }
  32. // PathItem describes the operations available on a single path.
  33. // A Path Item may be empty, due to [ACL constraints](http://goo.gl/8us55a#securityFiltering).
  34. // The path itself is still exposed to the documentation viewer but they will
  35. // not know which operations and parameters are available.
  36. //
  37. // For more information: http://goo.gl/8us55a#pathItemObject
  38. type PathItem struct {
  39. Refable
  40. VendorExtensible
  41. PathItemProps
  42. }
  43. // UnmarshalJSON hydrates this items instance with the data from JSON
  44. func (p *PathItem) UnmarshalJSON(data []byte) error {
  45. if internal.UseOptimizedJSONUnmarshaling {
  46. return jsonv2.Unmarshal(data, p)
  47. }
  48. if err := json.Unmarshal(data, &p.Refable); err != nil {
  49. return err
  50. }
  51. if err := json.Unmarshal(data, &p.VendorExtensible); err != nil {
  52. return err
  53. }
  54. return json.Unmarshal(data, &p.PathItemProps)
  55. }
  56. func (p *PathItem) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error {
  57. var x struct {
  58. Extensions
  59. PathItemProps
  60. }
  61. if err := opts.UnmarshalNext(dec, &x); err != nil {
  62. return err
  63. }
  64. if err := p.Refable.Ref.fromMap(x.Extensions); err != nil {
  65. return err
  66. }
  67. p.Extensions = internal.SanitizeExtensions(x.Extensions)
  68. p.PathItemProps = x.PathItemProps
  69. return nil
  70. }
  71. // MarshalJSON converts this items object to JSON
  72. func (p PathItem) MarshalJSON() ([]byte, error) {
  73. if internal.UseOptimizedJSONMarshaling {
  74. return internal.DeterministicMarshal(p)
  75. }
  76. b3, err := json.Marshal(p.Refable)
  77. if err != nil {
  78. return nil, err
  79. }
  80. b4, err := json.Marshal(p.VendorExtensible)
  81. if err != nil {
  82. return nil, err
  83. }
  84. b5, err := json.Marshal(p.PathItemProps)
  85. if err != nil {
  86. return nil, err
  87. }
  88. concated := swag.ConcatJSON(b3, b4, b5)
  89. return concated, nil
  90. }
  91. func (p PathItem) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
  92. var x struct {
  93. Ref string `json:"$ref,omitempty"`
  94. Extensions
  95. PathItemProps
  96. }
  97. x.Ref = p.Refable.Ref.String()
  98. x.Extensions = internal.SanitizeExtensions(p.Extensions)
  99. x.PathItemProps = p.PathItemProps
  100. return opts.MarshalNext(enc, x)
  101. }