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.
23 "github.com/go-openapi/swag"
26 // Responses is a container for the expected responses of an operation.
27 // The container maps a HTTP response code to the expected response.
28 // It is not expected from the documentation to necessarily cover all possible HTTP response codes,
29 // since they may not be known in advance. However, it is expected from the documentation to cover
30 // a successful operation response and any known errors.
32 // The `default` can be used a default response object for all HTTP codes that are not covered
33 // individually by the specification.
35 // The `Responses Object` MUST contain at least one response code, and it SHOULD be the response
36 // for a successful operation call.
38 // For more information: http://goo.gl/8us55a#responsesObject
39 type Responses struct {
44 // JSONLookup implements an interface to customize json pointer lookup
45 func (r Responses) JSONLookup(token string) (interface{}, error) {
46 if token == "default" {
49 if ex, ok := r.Extensions[token]; ok {
52 if i, err := strconv.Atoi(token); err == nil {
53 if scr, ok := r.StatusCodeResponses[i]; ok {
57 return nil, fmt.Errorf("object has no field %q", token)
60 // UnmarshalJSON hydrates this items instance with the data from JSON
61 func (r *Responses) UnmarshalJSON(data []byte) error {
62 if err := json.Unmarshal(data, &r.ResponsesProps); err != nil {
65 if err := json.Unmarshal(data, &r.VendorExtensible); err != nil {
68 if reflect.DeepEqual(ResponsesProps{}, r.ResponsesProps) {
69 r.ResponsesProps = ResponsesProps{}
74 // MarshalJSON converts this items object to JSON
75 func (r Responses) MarshalJSON() ([]byte, error) {
76 b1, err := json.Marshal(r.ResponsesProps)
80 b2, err := json.Marshal(r.VendorExtensible)
84 concated := swag.ConcatJSON(b1, b2)
88 // ResponsesProps describes all responses for an operation.
89 // It tells what is the default response and maps all responses with a
91 type ResponsesProps struct {
93 StatusCodeResponses map[int]Response
96 // MarshalJSON marshals responses as JSON
97 func (r ResponsesProps) MarshalJSON() ([]byte, error) {
98 toser := map[string]Response{}
100 toser["default"] = *r.Default
102 for k, v := range r.StatusCodeResponses {
103 toser[strconv.Itoa(k)] = v
105 return json.Marshal(toser)
108 // UnmarshalJSON unmarshals responses from JSON
109 func (r *ResponsesProps) UnmarshalJSON(data []byte) error {
110 var res map[string]Response
111 if err := json.Unmarshal(data, &res); err != nil {
114 if v, ok := res["default"]; ok {
116 delete(res, "default")
118 for k, v := range res {
119 if nk, err := strconv.Atoi(k); err == nil {
120 if r.StatusCodeResponses == nil {
121 r.StatusCodeResponses = map[int]Response{}
123 r.StatusCodeResponses[nk] = v