-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
openapi_operations.go
164 lines (135 loc) · 4.88 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package fuego
import (
"slices"
"github.com/getkin/kin-openapi/openapi3"
)
type ParamType string
const (
QueryParamType ParamType = "query"
HeaderParamType ParamType = "header"
CookieParamType ParamType = "cookie"
)
type OpenAPIParam struct {
Name string
Description string
OpenAPIParamOption
}
type OpenAPIParamOption struct {
Required bool
Example string
Type ParamType
}
// Overrides the description for the route.
func (r Route[ResponseBody, RequestBody]) Description(description string) Route[ResponseBody, RequestBody] {
r.Operation.Description = description
return r
}
// Overrides the summary for the route.
func (r Route[ResponseBody, RequestBody]) Summary(summary string) Route[ResponseBody, RequestBody] {
r.Operation.Summary = summary
return r
}
// Overrides the operationID for the route.
func (r Route[ResponseBody, RequestBody]) OperationID(operationID string) Route[ResponseBody, RequestBody] {
r.Operation.OperationID = operationID
return r
}
// Param registers a parameter for the route.
// The paramType can be "query", "header" or "cookie" as defined in [ParamType].
// [Cookie], [Header], [QueryParam] are shortcuts for Param.
func (r Route[ResponseBody, RequestBody]) Param(paramType ParamType, name, description string, params ...OpenAPIParamOption) Route[ResponseBody, RequestBody] {
openapiParam := openapi3.NewHeaderParameter(name)
openapiParam.Description = description
openapiParam.Schema = openapi3.NewStringSchema().NewRef()
openapiParam.In = string(paramType)
for _, param := range params {
if param.Required {
openapiParam.Required = param.Required
}
if param.Example != "" {
openapiParam.Example = param.Example
}
}
r.Operation.AddParameter(openapiParam)
return r
}
// Header registers a header parameter for the route.
func (r Route[ResponseBody, RequestBody]) Header(name, description string, params ...OpenAPIParamOption) Route[ResponseBody, RequestBody] {
r.Param(HeaderParamType, name, description, params...)
return r
}
// Cookie registers a cookie parameter for the route.
func (r Route[ResponseBody, RequestBody]) Cookie(name, description string, params ...OpenAPIParamOption) Route[ResponseBody, RequestBody] {
r.Param(CookieParamType, name, description, params...)
return r
}
// QueryParam registers a query parameter for the route.
func (r Route[ResponseBody, RequestBody]) QueryParam(name, description string, params ...OpenAPIParamOption) Route[ResponseBody, RequestBody] {
r.Param(QueryParamType, name, description, params...)
return r
}
// Replace the tags for the route.
// By default, the tag is the type of the response body.
func (r Route[ResponseBody, RequestBody]) Tags(tags ...string) Route[ResponseBody, RequestBody] {
r.Operation.Tags = tags
return r
}
// Replace the available request Content-Types for the route.
// By default, the request Content-Types are `application/json` and `application/xml`
func (r Route[ResponseBody, RequestBody]) RequestContentType(consumes ...string) Route[ResponseBody, RequestBody] {
bodyTag := schemaTagFromType(r.mainRouter, *new(RequestBody))
if bodyTag.name != "unknown-interface" {
requestBody := newRequestBody[RequestBody](bodyTag, consumes)
// set just Value as we do not want to reference
// a global requestBody
r.Operation.RequestBody = &openapi3.RequestBodyRef{
Value: requestBody,
}
}
return r
}
// AddTags adds tags to the route.
func (r Route[ResponseBody, RequestBody]) AddTags(tags ...string) Route[ResponseBody, RequestBody] {
r.Operation.Tags = append(r.Operation.Tags, tags...)
return r
}
// AddError adds an error to the route.
func (r Route[ResponseBody, RequestBody]) AddError(code int, description string, errorType ...any) Route[ResponseBody, RequestBody] {
addResponse(r.mainRouter, r.Operation, code, description, errorType...)
return r
}
func addResponse(s *Server, operation *openapi3.Operation, code int, description string, errorType ...any) {
var responseSchema schemaTag
if len(errorType) > 0 {
responseSchema = schemaTagFromType(s, errorType[0])
} else {
responseSchema = schemaTagFromType(s, HTTPError{})
}
content := openapi3.NewContentWithSchemaRef(&responseSchema.SchemaRef, []string{"application/json"})
response := openapi3.NewResponse().
WithDescription(description).
WithContent(content)
operation.AddResponse(code, response)
}
// openAPIError describes a response error in the OpenAPI spec.
type openAPIError struct {
Code int
Description string
ErrorType any
}
// RemoveTags removes tags from the route.
func (r Route[ResponseBody, RequestBody]) RemoveTags(tags ...string) Route[ResponseBody, RequestBody] {
for _, tag := range tags {
for i, t := range r.Operation.Tags {
if t == tag {
r.Operation.Tags = slices.Delete(r.Operation.Tags, i, i+1)
break
}
}
}
return r
}
func (r Route[ResponseBody, RequestBody]) Deprecated() Route[ResponseBody, RequestBody] {
r.Operation.Deprecated = true
return r
}