Skip to content

Commit

Permalink
Allow validations options when creating legace Router (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
yclybouw authored Oct 7, 2022
1 parent 8588ab8 commit d4c06af
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
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)
}

0 comments on commit d4c06af

Please sign in to comment.