response.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. package spec
  15. import (
  16. "encoding/json"
  17. "github.com/go-openapi/swag"
  18. )
  19. // ResponseProps properties specific to a response
  20. type ResponseProps struct {
  21. Description string `json:"description,omitempty"`
  22. Schema *Schema `json:"schema,omitempty"`
  23. Headers map[string]Header `json:"headers,omitempty"`
  24. Examples map[string]interface{} `json:"examples,omitempty"`
  25. }
  26. // Response describes a single response from an API Operation.
  27. //
  28. // For more information: http://goo.gl/8us55a#responseObject
  29. type Response struct {
  30. Refable
  31. ResponseProps
  32. }
  33. // UnmarshalJSON hydrates this items instance with the data from JSON
  34. func (r *Response) UnmarshalJSON(data []byte) error {
  35. if err := json.Unmarshal(data, &r.ResponseProps); err != nil {
  36. return err
  37. }
  38. if err := json.Unmarshal(data, &r.Refable); err != nil {
  39. return err
  40. }
  41. return nil
  42. }
  43. // MarshalJSON converts this items object to JSON
  44. func (r Response) MarshalJSON() ([]byte, error) {
  45. b1, err := json.Marshal(r.ResponseProps)
  46. if err != nil {
  47. return nil, err
  48. }
  49. b2, err := json.Marshal(r.Refable)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return swag.ConcatJSON(b1, b2), nil
  54. }
  55. // NewResponse creates a new response instance
  56. func NewResponse() *Response {
  57. return new(Response)
  58. }
  59. // ResponseRef creates a response as a json reference
  60. func ResponseRef(url string) *Response {
  61. resp := NewResponse()
  62. resp.Ref = MustCreateRef(url)
  63. return resp
  64. }
  65. // WithDescription sets the description on this response, allows for chaining
  66. func (r *Response) WithDescription(description string) *Response {
  67. r.Description = description
  68. return r
  69. }
  70. // WithSchema sets the schema on this response, allows for chaining.
  71. // Passing a nil argument removes the schema from this response
  72. func (r *Response) WithSchema(schema *Schema) *Response {
  73. r.Schema = schema
  74. return r
  75. }
  76. // AddHeader adds a header to this response
  77. func (r *Response) AddHeader(name string, header *Header) *Response {
  78. if header == nil {
  79. return r.RemoveHeader(name)
  80. }
  81. if r.Headers == nil {
  82. r.Headers = make(map[string]Header)
  83. }
  84. r.Headers[name] = *header
  85. return r
  86. }
  87. // RemoveHeader removes a header from this response
  88. func (r *Response) RemoveHeader(name string) *Response {
  89. delete(r.Headers, name)
  90. return r
  91. }
  92. // AddExample adds an example to this response
  93. func (r *Response) AddExample(mediaType string, example interface{}) *Response {
  94. if r.Examples == nil {
  95. r.Examples = make(map[string]interface{})
  96. }
  97. r.Examples[mediaType] = example
  98. return r
  99. }