Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame^] | 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 | |
| 20 | "github.com/go-openapi/jsonpointer" |
| 21 | "github.com/go-openapi/swag" |
| 22 | ) |
| 23 | |
| 24 | // pathItemProps the path item specific properties |
| 25 | type PathItemProps struct { |
| 26 | Get *Operation `json:"get,omitempty"` |
| 27 | Put *Operation `json:"put,omitempty"` |
| 28 | Post *Operation `json:"post,omitempty"` |
| 29 | Delete *Operation `json:"delete,omitempty"` |
| 30 | Options *Operation `json:"options,omitempty"` |
| 31 | Head *Operation `json:"head,omitempty"` |
| 32 | Patch *Operation `json:"patch,omitempty"` |
| 33 | Parameters []Parameter `json:"parameters,omitempty"` |
| 34 | } |
| 35 | |
| 36 | // PathItem describes the operations available on a single path. |
| 37 | // A Path Item may be empty, due to [ACL constraints](http://goo.gl/8us55a#securityFiltering). |
| 38 | // The path itself is still exposed to the documentation viewer but they will |
| 39 | // not know which operations and parameters are available. |
| 40 | // |
| 41 | // For more information: http://goo.gl/8us55a#pathItemObject |
| 42 | type PathItem struct { |
| 43 | Refable |
| 44 | VendorExtensible |
| 45 | PathItemProps |
| 46 | } |
| 47 | |
| 48 | // JSONLookup look up a value by the json property name |
| 49 | func (p PathItem) JSONLookup(token string) (interface{}, error) { |
| 50 | if ex, ok := p.Extensions[token]; ok { |
| 51 | return &ex, nil |
| 52 | } |
| 53 | if token == "$ref" { |
| 54 | return &p.Ref, nil |
| 55 | } |
| 56 | r, _, err := jsonpointer.GetForToken(p.PathItemProps, token) |
| 57 | return r, err |
| 58 | } |
| 59 | |
| 60 | // UnmarshalJSON hydrates this items instance with the data from JSON |
| 61 | func (p *PathItem) UnmarshalJSON(data []byte) error { |
| 62 | if err := json.Unmarshal(data, &p.Refable); err != nil { |
| 63 | return err |
| 64 | } |
| 65 | if err := json.Unmarshal(data, &p.VendorExtensible); err != nil { |
| 66 | return err |
| 67 | } |
| 68 | if err := json.Unmarshal(data, &p.PathItemProps); err != nil { |
| 69 | return err |
| 70 | } |
| 71 | return nil |
| 72 | } |
| 73 | |
| 74 | // MarshalJSON converts this items object to JSON |
| 75 | func (p PathItem) MarshalJSON() ([]byte, error) { |
| 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 | } |