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