1 // Copyright 2015 go-swagger maintainers
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
22 "github.com/go-openapi/swag"
25 // Paths holds the relative paths to the individual endpoints.
26 // The path is appended to the [`basePath`](http://goo.gl/8us55a#swaggerBasePath) in order
27 // to construct the full URL.
28 // The Paths may be empty, due to [ACL constraints](http://goo.gl/8us55a#securityFiltering).
30 // For more information: http://goo.gl/8us55a#pathsObject
33 Paths map[string]PathItem `json:"-"` // custom serializer to flatten this, each entry must start with "/"
36 // JSONLookup look up a value by the json property name
37 func (p Paths) JSONLookup(token string) (interface{}, error) {
38 if pi, ok := p.Paths[token]; ok {
41 if ex, ok := p.Extensions[token]; ok {
44 return nil, fmt.Errorf("object has no field %q", token)
47 // UnmarshalJSON hydrates this items instance with the data from JSON
48 func (p *Paths) UnmarshalJSON(data []byte) error {
49 var res map[string]json.RawMessage
50 if err := json.Unmarshal(data, &res); err != nil {
53 for k, v := range res {
54 if strings.HasPrefix(strings.ToLower(k), "x-") {
55 if p.Extensions == nil {
56 p.Extensions = make(map[string]interface{})
59 if err := json.Unmarshal(v, &d); err != nil {
64 if strings.HasPrefix(k, "/") {
66 p.Paths = make(map[string]PathItem)
69 if err := json.Unmarshal(v, &pi); err != nil {
78 // MarshalJSON converts this items object to JSON
79 func (p Paths) MarshalJSON() ([]byte, error) {
80 b1, err := json.Marshal(p.VendorExtensible)
85 pths := make(map[string]PathItem)
86 for k, v := range p.Paths {
87 if strings.HasPrefix(k, "/") {
91 b2, err := json.Marshal(pths)
95 concated := swag.ConcatJSON(b1, b2)