|
Packit Service |
3a6627 |
package openapi3
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
import (
|
|
Packit Service |
3a6627 |
"context"
|
|
Packit Service |
3a6627 |
"errors"
|
|
Packit Service |
3a6627 |
"strconv"
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
"github.com/getkin/kin-openapi/jsoninfo"
|
|
Packit Service |
3a6627 |
)
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
// Responses is specified by OpenAPI/Swagger 3.0 standard.
|
|
Packit Service |
3a6627 |
type Responses map[string]*ResponseRef
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
func NewResponses() Responses {
|
|
Packit Service |
3a6627 |
r := make(Responses)
|
|
Packit Service |
3a6627 |
r["default"] = &ResponseRef{Value: NewResponse().WithDescription("")}
|
|
Packit Service |
3a6627 |
return r
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
func (responses Responses) Default() *ResponseRef {
|
|
Packit Service |
3a6627 |
return responses["default"]
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
func (responses Responses) Get(status int) *ResponseRef {
|
|
Packit Service |
3a6627 |
return responses[strconv.FormatInt(int64(status), 10)]
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
func (responses Responses) Validate(c context.Context) error {
|
|
Packit Service |
3a6627 |
if len(responses) == 0 {
|
|
Packit Service |
3a6627 |
return errors.New("the responses object MUST contain at least one response code")
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
for _, v := range responses {
|
|
Packit Service |
3a6627 |
if err := v.Validate(c); err != nil {
|
|
Packit Service |
3a6627 |
return err
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
return nil
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
// Response is specified by OpenAPI/Swagger 3.0 standard.
|
|
Packit Service |
3a6627 |
type Response struct {
|
|
Packit Service |
3a6627 |
ExtensionProps
|
|
Packit Service |
3a6627 |
Description *string `json:"description,omitempty" yaml:"description,omitempty"`
|
|
Packit Service |
3a6627 |
Headers map[string]*HeaderRef `json:"headers,omitempty" yaml:"headers,omitempty"`
|
|
Packit Service |
3a6627 |
Content Content `json:"content,omitempty" yaml:"content,omitempty"`
|
|
Packit Service |
3a6627 |
Links map[string]*LinkRef `json:"links,omitempty" yaml:"links,omitempty"`
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
func NewResponse() *Response {
|
|
Packit Service |
3a6627 |
return &Response{}
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
func (response *Response) WithDescription(value string) *Response {
|
|
Packit Service |
3a6627 |
response.Description = &value
|
|
Packit Service |
3a6627 |
return response
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
func (response *Response) WithContent(content Content) *Response {
|
|
Packit Service |
3a6627 |
response.Content = content
|
|
Packit Service |
3a6627 |
return response
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
func (response *Response) WithJSONSchema(schema *Schema) *Response {
|
|
Packit Service |
3a6627 |
response.Content = NewContentWithJSONSchema(schema)
|
|
Packit Service |
3a6627 |
return response
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
func (response *Response) WithJSONSchemaRef(schema *SchemaRef) *Response {
|
|
Packit Service |
3a6627 |
response.Content = NewContentWithJSONSchemaRef(schema)
|
|
Packit Service |
3a6627 |
return response
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
func (response *Response) MarshalJSON() ([]byte, error) {
|
|
Packit Service |
3a6627 |
return jsoninfo.MarshalStrictStruct(response)
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
func (response *Response) UnmarshalJSON(data []byte) error {
|
|
Packit Service |
3a6627 |
return jsoninfo.UnmarshalStrictStruct(data, response)
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
func (response *Response) Validate(c context.Context) error {
|
|
Packit Service |
3a6627 |
if response.Description == nil {
|
|
Packit Service |
3a6627 |
return errors.New("a short description of the response is required")
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
|
|
Packit Service |
3a6627 |
if content := response.Content; content != nil {
|
|
Packit Service |
3a6627 |
if err := content.Validate(c); err != nil {
|
|
Packit Service |
3a6627 |
return err
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
}
|
|
Packit Service |
3a6627 |
return nil
|
|
Packit Service |
3a6627 |
}
|