-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopenapi.go
253 lines (212 loc) · 6.2 KB
/
openapi.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package openapi
import (
"context"
"errors"
"fmt"
"net/http"
"sort"
"strings"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3filter"
"github.com/getkin/kin-openapi/routers"
"github.com/getkin/kin-openapi/routers/gorillamux"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
type Config struct {
// Skipper defines a function to skip middleware.
Skipper middleware.Skipper
// Schema defines the OpenAPI that will be loaded and
// that the requests and responses will be validated against.
// Required.
Schema string
// SchemaBytes allows loading the OpenAPI specification directly
// from a byte slice ([]byte). This is useful for embedding the
// OpenAPI spec in the binary using Go's embed package, or if the
// spec is obtained dynamically at runtime.
// Required unless Schema is provided.
//
// If both Schema and SchemaBytes are provided, SchemaBytes takes precedence.
SchemaBytes []byte
// ContextKey defines the key that will be used to store the validator
// on the echo.Context when the request is successfully validated.
// Optional. Defaults to "validator".
ContextKey string
// ExemptRoutes defines routes and methods that don't require validation.
// Optional.
ExemptRoutes map[string][]string
}
var DefaultConfig = Config{
Skipper: middleware.DefaultSkipper,
ContextKey: "validator",
}
func OpenAPI(file string) echo.MiddlewareFunc {
c := DefaultConfig
c.Schema = file
return OpenAPIWithConfig(c)
}
func OpenAPIFromBytes(schemaBytes []byte) echo.MiddlewareFunc {
c := DefaultConfig
c.SchemaBytes = schemaBytes
return OpenAPIWithConfig(c)
}
func OpenAPIWithConfig(config Config) echo.MiddlewareFunc {
if config.Skipper == nil {
config.Skipper = DefaultConfig.Skipper
}
if config.Schema == "" && len(config.SchemaBytes) == 0 {
panic("either schema or schemaBytes is required")
}
if config.ContextKey == "" {
config.ContextKey = DefaultConfig.ContextKey
}
ctx := context.Background()
loader := &openapi3.Loader{Context: ctx, IsExternalRefsAllowed: true}
var schema *openapi3.T
var err error
if len(config.SchemaBytes) > 0 {
schema, err = loader.LoadFromData(config.SchemaBytes)
} else {
schema, err = loader.LoadFromFile(config.Schema)
}
if err != nil {
panic(fmt.Sprintf("failed loading schema file: %v", err))
}
err = schema.Validate(ctx)
if err != nil {
panic(fmt.Sprintf("failed validating schema: %v", err))
}
router, err := gorillamux.NewRouter(schema)
if err != nil {
panic(fmt.Sprintf("failed creating router: %v", err))
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if config.Skipper(c) {
return next(c)
}
if check(c.Path(), c.Request().Method, config.ExemptRoutes) {
return next(c)
}
route, pathParams, err := router.FindRoute(c.Request())
if err != nil {
c.Logger().Debugf(
"error finding route for %s %s: %v",
c.Request().Method, c.Request().URL.String(), err,
)
if errors.Is(err, routers.ErrPathNotFound) {
return echo.NewHTTPError(http.StatusNotFound, "Path not found")
}
if errors.Is(err, routers.ErrMethodNotAllowed) {
return echo.NewHTTPError(http.StatusMethodNotAllowed, "Method not allowed")
}
return err
}
requestValidationInput := &openapi3filter.RequestValidationInput{
Request: c.Request(),
PathParams: pathParams,
Route: route,
Options: &openapi3filter.Options{
MultiError: true,
AuthenticationFunc: openapi3filter.NoopAuthenticationFunc,
},
}
err = openapi3filter.ValidateRequest(ctx, requestValidationInput)
switch err := err.(type) {
case nil:
case openapi3.MultiError:
issues := convertError(err)
names := make([]string, 0, len(issues))
if val, ok := issues["body"]; ok {
return JSONValidationError(c, http.StatusBadRequest, "Request error", val)
}
for k := range issues {
names = append(names, k)
}
sort.Strings(names)
var errs []string
for _, k := range names {
msgs := issues[k]
for _, msg := range msgs {
errs = append(errs, msg)
}
}
return JSONValidationError(c, http.StatusUnprocessableEntity, "Validation error", errs)
default:
return err
}
c.Set(config.ContextKey, requestValidationInput)
return next(c)
}
}
}
func convertError(me openapi3.MultiError) map[string][]string {
issues := make(map[string][]string)
for _, err := range me {
switch err := err.(type) {
case *openapi3.SchemaError:
var field string
if path := err.JSONPointer(); len(path) > 0 {
field = strings.Join(path, ".")
}
var msg string
if len(field) > 0 {
msg = fmt.Sprintf("%s: %s", field, err.Reason)
} else {
msg = fmt.Sprintf("%s", err.Reason)
}
msg = strings.ReplaceAll(msg, "\"", "'")
issues[field] = append(issues[field], msg)
case *openapi3filter.RequestError: // possible there were multiple issues that failed validation
// check if invalid HTTP parameter
if err.Parameter != nil {
prefix := err.Parameter.In
name := fmt.Sprintf("%s.%s", prefix, err.Parameter.Name)
split := strings.Split(err.Err.Error(), "\n")
msg := fmt.Sprintf("parameter '%s' in %s has an error: %s", err.Parameter.Name, prefix, split[0])
issues[name] = append(issues[name], msg)
continue
}
if err, ok := err.Err.(openapi3.MultiError); ok {
for k, v := range convertError(err) {
issues[k] = append(issues[k], v...)
}
continue
}
// check if requestBody
if err.RequestBody != nil {
issues["body"] = append(issues["body"], err.Error())
continue
}
default:
const unknown = "unknown"
issues[unknown] = append(issues[unknown], err.Error())
}
}
return issues
}
func check(path string, method string, m map[string][]string) bool {
for k, v := range m {
if k == path {
for _, i := range v {
if method == i {
return true
}
}
}
}
return false
}
type ValidationError struct {
echo.HTTPError
Errors []string `json:"errors,omitempty"`
}
func JSONValidationError(c echo.Context, status int, msg string, errors []string) error {
return c.JSON(status, ValidationError{
echo.HTTPError{
Code: status,
Message: msg,
},
errors,
})
}