-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
openapi_operations.go
51 lines (40 loc) · 1.4 KB
/
openapi_operations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package fuego
import (
"strconv"
"github.com/getkin/kin-openapi/openapi3"
"github.com/go-fuego/fuego/internal"
)
type ParamType = internal.ParamType // Query, Header, Cookie
const (
PathParamType ParamType = "path"
QueryParamType ParamType = "query"
HeaderParamType ParamType = "header"
CookieParamType ParamType = "cookie"
)
type OpenAPIParam = internal.OpenAPIParam
// Registers a response for the route, only if error for this code is not already set.
func addResponseIfNotSet(openapi *OpenAPI, operation *openapi3.Operation, code int, description string, response Response) {
if operation.Responses.Value(strconv.Itoa(code)) != nil {
return
}
operation.AddResponse(code, openapi.buildOpenapi3Response(description, response))
}
func (o *OpenAPI) buildOpenapi3Response(description string, response Response) *openapi3.Response {
if response.Type == nil {
panic("Type in Response cannot be nil")
}
responseSchema := SchemaTagFromType(o, response.Type)
if len(response.ContentTypes) == 0 {
response.ContentTypes = []string{"application/json", "application/xml"}
}
content := openapi3.NewContentWithSchemaRef(&responseSchema.SchemaRef, response.ContentTypes)
return openapi3.NewResponse().
WithDescription(description).
WithContent(content)
}
// openAPIResponse describes a response error in the OpenAPI spec.
type openAPIResponse struct {
Response
Code int
Description string
}