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

258 lines
4.2 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"
"github.com/go-openapi/jsonpointer"
"github.com/go-openapi/swag"
)
// ResponseProps properties specific to a response
2024-05-14 13:07:09 +00:00
type ResponseProps struct {
Description string `json:"description"`
Schema *Schema `json:"schema,omitempty"`
Headers map[string]Header `json:"headers,omitempty"`
Examples map[string]interface{} `json:"examples,omitempty"`
2024-05-14 13:07:09 +00:00
}
// Response describes a single response from an API Operation.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// For more information: http://goo.gl/8us55a#responseObject
2024-05-14 13:07:09 +00:00
type Response struct {
Refable
2024-05-14 13:07:09 +00:00
ResponseProps
2024-05-14 13:07:09 +00:00
VendorExtensible
}
// JSONLookup look up a value by the json property name
2024-05-14 13:07:09 +00:00
func (r Response) JSONLookup(token string) (interface{}, error) {
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 token == "$ref" {
2024-05-14 13:07:09 +00:00
return &r.Ref, nil
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
ptr, _, err := jsonpointer.GetForToken(r.ResponseProps, token)
2024-05-14 13:07:09 +00:00
return ptr, err
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 *Response) UnmarshalJSON(data []byte) error {
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(data, &r.ResponseProps); 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 err := json.Unmarshal(data, &r.Refable); 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
return json.Unmarshal(data, &r.VendorExtensible)
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 Response) MarshalJSON() ([]byte, error) {
2024-05-14 13:07:09 +00:00
var (
b1 []byte
2024-05-14 13:07:09 +00:00
err error
)
if r.Ref.String() == "" {
2024-05-14 13:07:09 +00:00
// when there is no $ref, empty description is rendered as an empty string
2024-05-14 13:07:09 +00:00
b1, err = json.Marshal(r.ResponseProps)
2024-05-14 13:07:09 +00:00
} else {
2024-05-14 13:07:09 +00:00
// when there is $ref inside the schema, description should be omitempty-ied
2024-05-14 13:07:09 +00:00
b1, err = json.Marshal(struct {
Description string `json:"description,omitempty"`
Schema *Schema `json:"schema,omitempty"`
Headers map[string]Header `json:"headers,omitempty"`
Examples map[string]interface{} `json:"examples,omitempty"`
2024-05-14 13:07:09 +00:00
}{
2024-05-14 13:07:09 +00:00
Description: r.ResponseProps.Description,
Schema: r.ResponseProps.Schema,
Examples: r.ResponseProps.Examples,
2024-05-14 13:07:09 +00:00
})
2024-05-14 13:07:09 +00:00
}
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
}
b2, err := json.Marshal(r.Refable)
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
b3, 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
return swag.ConcatJSON(b1, b2, b3), nil
2024-05-14 13:07:09 +00:00
}
// NewResponse creates a new response instance
2024-05-14 13:07:09 +00:00
func NewResponse() *Response {
2024-05-14 13:07:09 +00:00
return new(Response)
2024-05-14 13:07:09 +00:00
}
// ResponseRef creates a response as a json reference
2024-05-14 13:07:09 +00:00
func ResponseRef(url string) *Response {
2024-05-14 13:07:09 +00:00
resp := NewResponse()
2024-05-14 13:07:09 +00:00
resp.Ref = MustCreateRef(url)
2024-05-14 13:07:09 +00:00
return resp
2024-05-14 13:07:09 +00:00
}
// WithDescription sets the description on this response, allows for chaining
2024-05-14 13:07:09 +00:00
func (r *Response) WithDescription(description string) *Response {
2024-05-14 13:07:09 +00:00
r.Description = description
2024-05-14 13:07:09 +00:00
return r
2024-05-14 13:07:09 +00:00
}
// WithSchema sets the schema on this response, allows for chaining.
2024-05-14 13:07:09 +00:00
// Passing a nil argument removes the schema from this response
2024-05-14 13:07:09 +00:00
func (r *Response) WithSchema(schema *Schema) *Response {
2024-05-14 13:07:09 +00:00
r.Schema = schema
2024-05-14 13:07:09 +00:00
return r
2024-05-14 13:07:09 +00:00
}
// AddHeader adds a header to this response
2024-05-14 13:07:09 +00:00
func (r *Response) AddHeader(name string, header *Header) *Response {
2024-05-14 13:07:09 +00:00
if header == nil {
2024-05-14 13:07:09 +00:00
return r.RemoveHeader(name)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if r.Headers == nil {
2024-05-14 13:07:09 +00:00
r.Headers = make(map[string]Header)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
r.Headers[name] = *header
2024-05-14 13:07:09 +00:00
return r
2024-05-14 13:07:09 +00:00
}
// RemoveHeader removes a header from this response
2024-05-14 13:07:09 +00:00
func (r *Response) RemoveHeader(name string) *Response {
2024-05-14 13:07:09 +00:00
delete(r.Headers, name)
2024-05-14 13:07:09 +00:00
return r
2024-05-14 13:07:09 +00:00
}
// AddExample adds an example to this response
2024-05-14 13:07:09 +00:00
func (r *Response) AddExample(mediaType string, example interface{}) *Response {
2024-05-14 13:07:09 +00:00
if r.Examples == nil {
2024-05-14 13:07:09 +00:00
r.Examples = make(map[string]interface{})
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
r.Examples[mediaType] = example
2024-05-14 13:07:09 +00:00
return r
2024-05-14 13:07:09 +00:00
}