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.
20 "github.com/go-openapi/jsonpointer"
21 "github.com/go-openapi/swag"
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"`
32 // Response describes a single response from an API Operation.
34 // For more information: http://goo.gl/8us55a#responseObject
35 type Response struct {
41 // JSONLookup look up a value by the json property name
42 func (r Response) JSONLookup(token string) (interface{}, error) {
43 if ex, ok := r.Extensions[token]; ok {
49 ptr, _, err := jsonpointer.GetForToken(r.ResponseProps, token)
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 {
58 if err := json.Unmarshal(data, &r.Refable); err != nil {
61 return json.Unmarshal(data, &r.VendorExtensible)
64 // MarshalJSON converts this items object to JSON
65 func (r Response) MarshalJSON() ([]byte, error) {
66 b1, err := json.Marshal(r.ResponseProps)
70 b2, err := json.Marshal(r.Refable)
74 b3, err := json.Marshal(r.VendorExtensible)
78 return swag.ConcatJSON(b1, b2, b3), nil
81 // NewResponse creates a new response instance
82 func NewResponse() *Response {
86 // ResponseRef creates a response as a json reference
87 func ResponseRef(url string) *Response {
89 resp.Ref = MustCreateRef(url)
93 // WithDescription sets the description on this response, allows for chaining
94 func (r *Response) WithDescription(description string) *Response {
95 r.Description = description
99 // WithSchema sets the schema on this response, allows for chaining.
100 // Passing a nil argument removes the schema from this response
101 func (r *Response) WithSchema(schema *Schema) *Response {
106 // AddHeader adds a header to this response
107 func (r *Response) AddHeader(name string, header *Header) *Response {
109 return r.RemoveHeader(name)
111 if r.Headers == nil {
112 r.Headers = make(map[string]Header)
114 r.Headers[name] = *header
118 // RemoveHeader removes a header from this response
119 func (r *Response) RemoveHeader(name string) *Response {
120 delete(r.Headers, name)
124 // AddExample adds an example to this response
125 func (r *Response) AddExample(mediaType string, example interface{}) *Response {
126 if r.Examples == nil {
127 r.Examples = make(map[string]interface{})
129 r.Examples[mediaType] = example