forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/go-openapi/spec/responses.go

246 lines
4.0 KiB
Go
Raw Normal View History

2024-05-14 13:07:09 +00:00
// Copyright 2015 go-swagger maintainers
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
2024-05-14 13:07:09 +00:00
// you may not use this file except in compliance with the License.
2024-05-14 13:07:09 +00:00
// You may obtain a copy of the License at
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Unless required by applicable law or agreed to in writing, software
2024-05-14 13:07:09 +00:00
// distributed under the License is distributed on an "AS IS" BASIS,
2024-05-14 13:07:09 +00:00
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2024-05-14 13:07:09 +00:00
// See the License for the specific language governing permissions and
2024-05-14 13:07:09 +00:00
// limitations under the License.
package spec
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/go-openapi/swag"
)
// Responses is a container for the expected responses of an operation.
2024-05-14 13:07:09 +00:00
// The container maps a HTTP response code to the expected response.
2024-05-14 13:07:09 +00:00
// It is not expected from the documentation to necessarily cover all possible HTTP response codes,
2024-05-14 13:07:09 +00:00
// since they may not be known in advance. However, it is expected from the documentation to cover
2024-05-14 13:07:09 +00:00
// a successful operation response and any known errors.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// The `default` can be used a default response object for all HTTP codes that are not covered
2024-05-14 13:07:09 +00:00
// individually by the specification.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// The `Responses Object` MUST contain at least one response code, and it SHOULD be the response
2024-05-14 13:07:09 +00:00
// for a successful operation call.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// For more information: http://goo.gl/8us55a#responsesObject
2024-05-14 13:07:09 +00:00
type Responses struct {
VendorExtensible
2024-05-14 13:07:09 +00:00
ResponsesProps
}
// JSONLookup implements an interface to customize json pointer lookup
2024-05-14 13:07:09 +00:00
func (r Responses) JSONLookup(token string) (interface{}, error) {
2024-05-14 13:07:09 +00:00
if token == "default" {
2024-05-14 13:07:09 +00:00
return r.Default, nil
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if ex, ok := r.Extensions[token]; ok {
2024-05-14 13:07:09 +00:00
return &ex, nil
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if i, err := strconv.Atoi(token); err == nil {
2024-05-14 13:07:09 +00:00
if scr, ok := r.StatusCodeResponses[i]; ok {
2024-05-14 13:07:09 +00:00
return scr, nil
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return nil, fmt.Errorf("object has no field %q", token)
2024-05-14 13:07:09 +00:00
}
// UnmarshalJSON hydrates this items instance with the data from JSON
2024-05-14 13:07:09 +00:00
func (r *Responses) UnmarshalJSON(data []byte) error {
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(data, &r.ResponsesProps); err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
if err := json.Unmarshal(data, &r.VendorExtensible); err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if reflect.DeepEqual(ResponsesProps{}, r.ResponsesProps) {
2024-05-14 13:07:09 +00:00
r.ResponsesProps = ResponsesProps{}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
// MarshalJSON converts this items object to JSON
2024-05-14 13:07:09 +00:00
func (r Responses) MarshalJSON() ([]byte, error) {
2024-05-14 13:07:09 +00:00
b1, err := json.Marshal(r.ResponsesProps)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
b2, err := json.Marshal(r.VendorExtensible)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
concated := swag.ConcatJSON(b1, b2)
2024-05-14 13:07:09 +00:00
return concated, nil
2024-05-14 13:07:09 +00:00
}
// ResponsesProps describes all responses for an operation.
2024-05-14 13:07:09 +00:00
// It tells what is the default response and maps all responses with a
2024-05-14 13:07:09 +00:00
// HTTP status code.
2024-05-14 13:07:09 +00:00
type ResponsesProps struct {
Default *Response
2024-05-14 13:07:09 +00:00
StatusCodeResponses map[int]Response
}
// MarshalJSON marshals responses as JSON
2024-05-14 13:07:09 +00:00
func (r ResponsesProps) MarshalJSON() ([]byte, error) {
2024-05-14 13:07:09 +00:00
toser := map[string]Response{}
2024-05-14 13:07:09 +00:00
if r.Default != nil {
2024-05-14 13:07:09 +00:00
toser["default"] = *r.Default
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
for k, v := range r.StatusCodeResponses {
2024-05-14 13:07:09 +00:00
toser[strconv.Itoa(k)] = v
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return json.Marshal(toser)
2024-05-14 13:07:09 +00:00
}
// UnmarshalJSON unmarshals responses from JSON
2024-05-14 13:07:09 +00:00
func (r *ResponsesProps) UnmarshalJSON(data []byte) error {
2024-05-14 13:07:09 +00:00
var res map[string]json.RawMessage
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(data, &res); err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
if v, ok := res["default"]; ok {
2024-05-14 13:07:09 +00:00
var defaultRes Response
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(v, &defaultRes); err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
r.Default = &defaultRes
2024-05-14 13:07:09 +00:00
delete(res, "default")
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
for k, v := range res {
2024-05-14 13:07:09 +00:00
if !strings.HasPrefix(k, "x-") {
2024-05-14 13:07:09 +00:00
var statusCodeResp Response
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(v, &statusCodeResp); err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if nk, err := strconv.Atoi(k); err == nil {
2024-05-14 13:07:09 +00:00
if r.StatusCodeResponses == nil {
2024-05-14 13:07:09 +00:00
r.StatusCodeResponses = map[int]Response{}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
r.StatusCodeResponses[nk] = statusCodeResp
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}