path.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. "strings"
  17. "k8s.io/kube-openapi/pkg/validation/spec"
  18. "github.com/go-openapi/swag"
  19. )
  20. // Paths describes the available paths and operations for the API, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#pathsObject
  21. type Paths struct {
  22. Paths map[string]*Path
  23. spec.VendorExtensible
  24. }
  25. // MarshalJSON is a custom marshal function that knows how to encode Paths as JSON
  26. func (p *Paths) MarshalJSON() ([]byte, error) {
  27. b1, err := json.Marshal(p.Paths)
  28. if err != nil {
  29. return nil, err
  30. }
  31. b2, err := json.Marshal(p.VendorExtensible)
  32. if err != nil {
  33. return nil, err
  34. }
  35. return swag.ConcatJSON(b1, b2), nil
  36. }
  37. // UnmarshalJSON hydrates this items instance with the data from JSON
  38. func (p *Paths) UnmarshalJSON(data []byte) error {
  39. var res map[string]json.RawMessage
  40. if err := json.Unmarshal(data, &res); err != nil {
  41. return err
  42. }
  43. for k, v := range res {
  44. if strings.HasPrefix(strings.ToLower(k), "x-") {
  45. if p.Extensions == nil {
  46. p.Extensions = make(map[string]interface{})
  47. }
  48. var d interface{}
  49. if err := json.Unmarshal(v, &d); err != nil {
  50. return err
  51. }
  52. p.Extensions[k] = d
  53. }
  54. if strings.HasPrefix(k, "/") {
  55. if p.Paths == nil {
  56. p.Paths = make(map[string]*Path)
  57. }
  58. var pi *Path
  59. if err := json.Unmarshal(v, &pi); err != nil {
  60. return err
  61. }
  62. p.Paths[k] = pi
  63. }
  64. }
  65. return nil
  66. }
  67. // Path describes the operations available on a single path, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#pathItemObject
  68. //
  69. // Note that this struct is actually a thin wrapper around PathProps to make it referable and extensible
  70. type Path struct {
  71. spec.Refable
  72. PathProps
  73. spec.VendorExtensible
  74. }
  75. // MarshalJSON is a custom marshal function that knows how to encode Path as JSON
  76. func (p *Path) MarshalJSON() ([]byte, error) {
  77. b1, err := json.Marshal(p.Refable)
  78. if err != nil {
  79. return nil, err
  80. }
  81. b2, err := json.Marshal(p.PathProps)
  82. if err != nil {
  83. return nil, err
  84. }
  85. b3, err := json.Marshal(p.VendorExtensible)
  86. if err != nil {
  87. return nil, err
  88. }
  89. return swag.ConcatJSON(b1, b2, b3), nil
  90. }
  91. func (p *Path) UnmarshalJSON(data []byte) error {
  92. if err := json.Unmarshal(data, &p.Refable); err != nil {
  93. return err
  94. }
  95. if err := json.Unmarshal(data, &p.PathProps); err != nil {
  96. return err
  97. }
  98. if err := json.Unmarshal(data, &p.VendorExtensible); err != nil {
  99. return err
  100. }
  101. return nil
  102. }
  103. // PathProps describes the operations available on a single path, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#pathItemObject
  104. type PathProps struct {
  105. // Summary holds a summary for all operations in this path
  106. Summary string `json:"summary,omitempty"`
  107. // Description holds a description for all operations in this path
  108. Description string `json:"description,omitempty"`
  109. // Get defines GET operation
  110. Get *Operation `json:"get,omitempty"`
  111. // Put defines PUT operation
  112. Put *Operation `json:"put,omitempty"`
  113. // Post defines POST operation
  114. Post *Operation `json:"post,omitempty"`
  115. // Delete defines DELETE operation
  116. Delete *Operation `json:"delete,omitempty"`
  117. // Options defines OPTIONS operation
  118. Options *Operation `json:"options,omitempty"`
  119. // Head defines HEAD operation
  120. Head *Operation `json:"head,omitempty"`
  121. // Patch defines PATCH operation
  122. Patch *Operation `json:"patch,omitempty"`
  123. // Trace defines TRACE operation
  124. Trace *Operation `json:"trace,omitempty"`
  125. // Servers is an alternative server array to service all operations in this path
  126. Servers []*Server `json:"servers,omitempty"`
  127. // Parameters a list of parameters that are applicable for this operation
  128. Parameters []*Parameter `json:"parameters,omitempty"`
  129. }