| 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 | |
| 15 | package spec |
| 16 | |
| 17 | import ( |
| 18 | "encoding/json" |
| 19 | "fmt" |
| 20 | "strings" |
| 21 | |
| 22 | "github.com/go-openapi/swag" |
| 23 | "k8s.io/kube-openapi/pkg/internal" |
| 24 | jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json" |
| 25 | ) |
| 26 | |
| 27 | // Paths holds the relative paths to the individual endpoints. |
| 28 | // The path is appended to the [`basePath`](http://goo.gl/8us55a#swaggerBasePath) in order |
| 29 | // to construct the full URL. |
| 30 | // The Paths may be empty, due to [ACL constraints](http://goo.gl/8us55a#securityFiltering). |
| 31 | // |
| 32 | // For more information: http://goo.gl/8us55a#pathsObject |
| 33 | type Paths struct { |
| 34 | VendorExtensible |
| 35 | Paths map[string]PathItem `json:"-"` // custom serializer to flatten this, each entry must start with "/" |
| 36 | } |
| 37 | |
| 38 | // UnmarshalJSON hydrates this items instance with the data from JSON |
| 39 | func (p *Paths) UnmarshalJSON(data []byte) error { |
| 40 | if internal.UseOptimizedJSONUnmarshaling { |
| 41 | return jsonv2.Unmarshal(data, p) |
| 42 | } |
| 43 | |
| 44 | var res map[string]json.RawMessage |
| 45 | if err := json.Unmarshal(data, &res); err != nil { |
| 46 | return err |
| 47 | } |
| 48 | for k, v := range res { |
| 49 | if strings.HasPrefix(strings.ToLower(k), "x-") { |
| 50 | if p.Extensions == nil { |
| 51 | p.Extensions = make(map[string]interface{}) |
| 52 | } |
| 53 | var d interface{} |
| 54 | if err := json.Unmarshal(v, &d); err != nil { |
| 55 | return err |
| 56 | } |
| 57 | p.Extensions[k] = d |
| 58 | } |
| 59 | if strings.HasPrefix(k, "/") { |
| 60 | if p.Paths == nil { |
| 61 | p.Paths = make(map[string]PathItem) |
| 62 | } |
| 63 | var pi PathItem |
| 64 | if err := json.Unmarshal(v, &pi); err != nil { |
| 65 | return err |
| 66 | } |
| 67 | p.Paths[k] = pi |
| 68 | } |
| 69 | } |
| 70 | return nil |
| 71 | } |
| 72 | |
| 73 | func (p *Paths) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error { |
| 74 | tok, err := dec.ReadToken() |
| 75 | if err != nil { |
| 76 | return err |
| 77 | } |
| 78 | var ext any |
| 79 | var pi PathItem |
| 80 | switch k := tok.Kind(); k { |
| 81 | case 'n': |
| 82 | return nil // noop |
| 83 | case '{': |
| 84 | for { |
| 85 | tok, err := dec.ReadToken() |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | |
| 90 | if tok.Kind() == '}' { |
| 91 | return nil |
| 92 | } |
| 93 | |
| 94 | switch k := tok.String(); { |
| 95 | case internal.IsExtensionKey(k): |
| 96 | ext = nil |
| 97 | if err := opts.UnmarshalNext(dec, &ext); err != nil { |
| 98 | return err |
| 99 | } |
| 100 | |
| 101 | if p.Extensions == nil { |
| 102 | p.Extensions = make(map[string]any) |
| 103 | } |
| 104 | p.Extensions[k] = ext |
| 105 | case len(k) > 0 && k[0] == '/': |
| 106 | pi = PathItem{} |
| 107 | if err := opts.UnmarshalNext(dec, &pi); err != nil { |
| 108 | return err |
| 109 | } |
| 110 | |
| 111 | if p.Paths == nil { |
| 112 | p.Paths = make(map[string]PathItem) |
| 113 | } |
| 114 | p.Paths[k] = pi |
| 115 | default: |
| 116 | _, err := dec.ReadValue() // skip value |
| 117 | if err != nil { |
| 118 | return err |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | default: |
| 123 | return fmt.Errorf("unknown JSON kind: %v", k) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // MarshalJSON converts this items object to JSON |
| 128 | func (p Paths) MarshalJSON() ([]byte, error) { |
| 129 | if internal.UseOptimizedJSONMarshaling { |
| 130 | return internal.DeterministicMarshal(p) |
| 131 | } |
| 132 | b1, err := json.Marshal(p.VendorExtensible) |
| 133 | if err != nil { |
| 134 | return nil, err |
| 135 | } |
| 136 | |
| 137 | pths := make(map[string]PathItem) |
| 138 | for k, v := range p.Paths { |
| 139 | if strings.HasPrefix(k, "/") { |
| 140 | pths[k] = v |
| 141 | } |
| 142 | } |
| 143 | b2, err := json.Marshal(pths) |
| 144 | if err != nil { |
| 145 | return nil, err |
| 146 | } |
| 147 | concated := swag.ConcatJSON(b1, b2) |
| 148 | return concated, nil |
| 149 | } |
| 150 | |
| 151 | func (p Paths) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error { |
| 152 | m := make(map[string]any, len(p.Extensions)+len(p.Paths)) |
| 153 | for k, v := range p.Extensions { |
| 154 | if internal.IsExtensionKey(k) { |
| 155 | m[k] = v |
| 156 | } |
| 157 | } |
| 158 | for k, v := range p.Paths { |
| 159 | if strings.HasPrefix(k, "/") { |
| 160 | m[k] = v |
| 161 | } |
| 162 | } |
| 163 | return opts.MarshalNext(enc, m) |
| 164 | } |