...

Source file src/github.com/go-openapi/spec/response.go

Documentation: github.com/go-openapi/spec

     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"`
    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 (r Response) JSONLookup(token string) (interface{}, error) {
    43  	if ex, ok := r.Extensions[token]; ok {
    44  		return &ex, nil
    45  	}
    46  	if token == "$ref" {
    47  		return &r.Ref, nil
    48  	}
    49  	ptr, _, err := jsonpointer.GetForToken(r.ResponseProps, token)
    50  	return ptr, 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  	return json.Unmarshal(data, &r.VendorExtensible)
    62  }
    63  
    64  // MarshalJSON converts this items object to JSON
    65  func (r Response) MarshalJSON() ([]byte, error) {
    66  	var (
    67  		b1  []byte
    68  		err error
    69  	)
    70  
    71  	if r.Ref.String() == "" {
    72  		// when there is no $ref, empty description is rendered as an empty string
    73  		b1, err = json.Marshal(r.ResponseProps)
    74  	} else {
    75  		// when there is $ref inside the schema, description should be omitempty-ied
    76  		b1, err = json.Marshal(struct {
    77  			Description string                 `json:"description,omitempty"`
    78  			Schema      *Schema                `json:"schema,omitempty"`
    79  			Headers     map[string]Header      `json:"headers,omitempty"`
    80  			Examples    map[string]interface{} `json:"examples,omitempty"`
    81  		}{
    82  			Description: r.ResponseProps.Description,
    83  			Schema:      r.ResponseProps.Schema,
    84  			Examples:    r.ResponseProps.Examples,
    85  		})
    86  	}
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	b2, err := json.Marshal(r.Refable)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	b3, err := json.Marshal(r.VendorExtensible)
    96  	if err != nil {
    97  		return nil, err
    98  	}
    99  	return swag.ConcatJSON(b1, b2, b3), nil
   100  }
   101  
   102  // NewResponse creates a new response instance
   103  func NewResponse() *Response {
   104  	return new(Response)
   105  }
   106  
   107  // ResponseRef creates a response as a json reference
   108  func ResponseRef(url string) *Response {
   109  	resp := NewResponse()
   110  	resp.Ref = MustCreateRef(url)
   111  	return resp
   112  }
   113  
   114  // WithDescription sets the description on this response, allows for chaining
   115  func (r *Response) WithDescription(description string) *Response {
   116  	r.Description = description
   117  	return r
   118  }
   119  
   120  // WithSchema sets the schema on this response, allows for chaining.
   121  // Passing a nil argument removes the schema from this response
   122  func (r *Response) WithSchema(schema *Schema) *Response {
   123  	r.Schema = schema
   124  	return r
   125  }
   126  
   127  // AddHeader adds a header to this response
   128  func (r *Response) AddHeader(name string, header *Header) *Response {
   129  	if header == nil {
   130  		return r.RemoveHeader(name)
   131  	}
   132  	if r.Headers == nil {
   133  		r.Headers = make(map[string]Header)
   134  	}
   135  	r.Headers[name] = *header
   136  	return r
   137  }
   138  
   139  // RemoveHeader removes a header from this response
   140  func (r *Response) RemoveHeader(name string) *Response {
   141  	delete(r.Headers, name)
   142  	return r
   143  }
   144  
   145  // AddExample adds an example to this response
   146  func (r *Response) AddExample(mediaType string, example interface{}) *Response {
   147  	if r.Examples == nil {
   148  		r.Examples = make(map[string]interface{})
   149  	}
   150  	r.Examples[mediaType] = example
   151  	return r
   152  }
   153  

View as plain text