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 1 commit
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
9 changes: 8 additions & 1 deletion routers/legacy/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ 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 {
return NewRouterWithValidationOptions(doc)
}

// NewRouterWithValidationOptions creates a new router with specific validation options before creation.
//
// Does the same thing as NewRouter, but when calling `doc.Validate()`, the validation options `opts` will be provided.
func NewRouterWithValidationOptions(doc *openapi3.T, opts ...openapi3.ValidationOption) (routers.Router, error) {
yclybouw marked this conversation as resolved.
Show resolved Hide resolved
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 = NewRouterWithValidationOptions(doc, openapi3.DisableExamplesValidation())
require.NoError(t, err)
}