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 | // ResponseProps properties specific to a response |
| 25 | type ResponseProps struct { |
| 26 | Description string `json:"description,omitempty"` |
| 27 | Schema *Schema `json:"schema,omitempty"` |
| 28 | Headers map[string]Header `json:"headers,omitempty"` |
| 29 | Examples map[string]interface{} `json:"examples,omitempty"` |
| 30 | } |
| 31 | |
| 32 | // Response describes a single response from an API Operation. |
| 33 | // |
| 34 | // For more information: http://goo.gl/8us55a#responseObject |
| 35 | type Response struct { |
| 36 | Refable |
| 37 | ResponseProps |
| 38 | VendorExtensible |
| 39 | } |
| 40 | |
| 41 | // JSONLookup look up a value by the json property name |
| 42 | func (p Response) JSONLookup(token string) (interface{}, error) { |
| 43 | if ex, ok := p.Extensions[token]; ok { |
| 44 | return &ex, nil |
| 45 | } |
| 46 | if token == "$ref" { |
| 47 | return &p.Ref, nil |
| 48 | } |
| 49 | r, _, err := jsonpointer.GetForToken(p.ResponseProps, token) |
| 50 | return r, err |
| 51 | } |
| 52 | |
| 53 | // UnmarshalJSON hydrates this items instance with the data from JSON |
| 54 | func (r *Response) UnmarshalJSON(data []byte) error { |
| 55 | if err := json.Unmarshal(data, &r.ResponseProps); err != nil { |
| 56 | return err |
| 57 | } |
| 58 | if err := json.Unmarshal(data, &r.Refable); err != nil { |
| 59 | return err |
| 60 | } |
| 61 | if err := json.Unmarshal(data, &r.VendorExtensible); err != nil { |
| 62 | return err |
| 63 | } |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | // MarshalJSON converts this items object to JSON |
| 68 | func (r Response) MarshalJSON() ([]byte, error) { |
| 69 | b1, err := json.Marshal(r.ResponseProps) |
| 70 | if err != nil { |
| 71 | return nil, err |
| 72 | } |
| 73 | b2, err := json.Marshal(r.Refable) |
| 74 | if err != nil { |
| 75 | return nil, err |
| 76 | } |
| 77 | b3, err := json.Marshal(r.VendorExtensible) |
| 78 | if err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | return swag.ConcatJSON(b1, b2, b3), nil |
| 82 | } |
| 83 | |
| 84 | // NewResponse creates a new response instance |
| 85 | func NewResponse() *Response { |
| 86 | return new(Response) |
| 87 | } |
| 88 | |
| 89 | // ResponseRef creates a response as a json reference |
| 90 | func ResponseRef(url string) *Response { |
| 91 | resp := NewResponse() |
| 92 | resp.Ref = MustCreateRef(url) |
| 93 | return resp |
| 94 | } |
| 95 | |
| 96 | // WithDescription sets the description on this response, allows for chaining |
| 97 | func (r *Response) WithDescription(description string) *Response { |
| 98 | r.Description = description |
| 99 | return r |
| 100 | } |
| 101 | |
| 102 | // WithSchema sets the schema on this response, allows for chaining. |
| 103 | // Passing a nil argument removes the schema from this response |
| 104 | func (r *Response) WithSchema(schema *Schema) *Response { |
| 105 | r.Schema = schema |
| 106 | return r |
| 107 | } |
| 108 | |
| 109 | // AddHeader adds a header to this response |
| 110 | func (r *Response) AddHeader(name string, header *Header) *Response { |
| 111 | if header == nil { |
| 112 | return r.RemoveHeader(name) |
| 113 | } |
| 114 | if r.Headers == nil { |
| 115 | r.Headers = make(map[string]Header) |
| 116 | } |
| 117 | r.Headers[name] = *header |
| 118 | return r |
| 119 | } |
| 120 | |
| 121 | // RemoveHeader removes a header from this response |
| 122 | func (r *Response) RemoveHeader(name string) *Response { |
| 123 | delete(r.Headers, name) |
| 124 | return r |
| 125 | } |
| 126 | |
| 127 | // AddExample adds an example to this response |
| 128 | func (r *Response) AddExample(mediaType string, example interface{}) *Response { |
| 129 | if r.Examples == nil { |
| 130 | r.Examples = make(map[string]interface{}) |
| 131 | } |
| 132 | r.Examples[mediaType] = example |
| 133 | return r |
| 134 | } |