Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow validations options when creating legace Router #614

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion routers/issue356_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ paths:
require.NoError(t, err)
err = doc.Validate(context.Background())
require.NoError(t, err)
gorillamuxNewRouterWrapped := func(doc *openapi3.T, opts ...openapi3.ValidationOption) (routers.Router, error) {
return gorillamux.NewRouter(doc)
}

for i, newRouter := range []func(*openapi3.T) (routers.Router, error){gorillamux.NewRouter, legacy.NewRouter} {
for i, newRouter := range []func(*openapi3.T, ...openapi3.ValidationOption) (routers.Router, error){gorillamuxNewRouterWrapped, legacy.NewRouter} {
t.Logf("using NewRouter from %s", map[int]string{0: "gorillamux", 1: "legacy"}[i])
router, err := newRouter(doc)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions routers/legacy/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ type Router struct {
//
// If the given OpenAPIv3 document has servers, router will use them.
// All operations of the document will be added to the router.
func NewRouter(doc *openapi3.T) (routers.Router, error) {
if err := doc.Validate(context.Background()); err != nil {
func NewRouter(doc *openapi3.T, opts ...openapi3.ValidationOption) (routers.Router, error) {
if err := doc.Validate(context.Background(), opts...); err != nil {
return nil, fmt.Errorf("validating OpenAPI failed: %w", err)
}
router := &Router{doc: doc}
Expand Down
17 changes: 17 additions & 0 deletions routers/legacy/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,21 @@ func TestRouter(t *testing.T) {
require.Nil(t, route)
require.Nil(t, pathParams)
}

schema := &openapi3.Schema{
Type: "string",
Example: 3,
}
content := openapi3.NewContentWithJSONSchema(schema)
responses := openapi3.NewResponses()
responses["default"].Value.Content = content
doc.Paths["/withExamples"] = &openapi3.PathItem{
Get: &openapi3.Operation{Responses: responses},
}
err = doc.Validate(context.Background())
require.Error(t, err)
r, err = NewRouter(doc)
require.Error(t, err)
r, err = NewRouter(doc, openapi3.DisableExamplesValidation())
require.NoError(t, err)
}