-
-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support validation options specifically for disabling pattern v…
…alidation (#590)
- Loading branch information
1 parent
5a61040
commit de022f1
Showing
9 changed files
with
156 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
openapi: 3.0.3 | ||
info: | ||
description: Contains Patterns that can't be compiled by the go regexp engine | ||
title: Issue 409 | ||
version: 0.0.1 | ||
paths: | ||
/v1/apis/{apiID}: | ||
get: | ||
description: Get a list of all Apis and there versions for a given workspace | ||
operationId: getApisV1 | ||
parameters: | ||
- description: The ID of the API | ||
in: path | ||
name: apiID | ||
required: true | ||
schema: | ||
type: string | ||
pattern: ^[a-zA-Z0-9]{0,4096}$ | ||
responses: | ||
"200": | ||
description: OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package openapi3_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/getkin/kin-openapi/openapi3" | ||
) | ||
|
||
func TestIssue409PatternIgnored(t *testing.T) { | ||
l := openapi3.NewLoader() | ||
s, err := l.LoadFromFile("testdata/issue409.yml") | ||
require.NoError(t, err) | ||
|
||
err = s.Validate(l.Context, openapi3.DisableSchemaPatternValidation()) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestIssue409PatternNotIgnored(t *testing.T) { | ||
l := openapi3.NewLoader() | ||
s, err := l.LoadFromFile("testdata/issue409.yml") | ||
require.NoError(t, err) | ||
|
||
err = s.Validate(l.Context) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestIssue409HygienicUseOfCtx(t *testing.T) { | ||
l := openapi3.NewLoader() | ||
doc, err := l.LoadFromFile("testdata/issue409.yml") | ||
require.NoError(t, err) | ||
|
||
err = doc.Validate(l.Context, openapi3.DisableSchemaPatternValidation()) | ||
assert.NoError(t, err) | ||
err = doc.Validate(l.Context) | ||
assert.Error(t, err) | ||
|
||
// and the other way | ||
|
||
l = openapi3.NewLoader() | ||
doc, err = l.LoadFromFile("testdata/issue409.yml") | ||
require.NoError(t, err) | ||
|
||
err = doc.Validate(l.Context) | ||
assert.Error(t, err) | ||
err = doc.Validate(l.Context, openapi3.DisableSchemaPatternValidation()) | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package openapi3 | ||
|
||
import "context" | ||
|
||
// ValidationOption allows the modification of how the OpenAPI document is validated. | ||
type ValidationOption func(options *ValidationOptions) | ||
|
||
// ValidationOptions provide configuration for validating OpenAPI documents. | ||
type ValidationOptions struct { | ||
SchemaFormatValidationEnabled bool | ||
SchemaPatternValidationDisabled bool | ||
} | ||
|
||
type validationOptionsKey struct{} | ||
|
||
// EnableSchemaFormatValidation makes Validate not return an error when validating documents that mention schema formats that are not defined by the OpenAPIv3 specification. | ||
func EnableSchemaFormatValidation() ValidationOption { | ||
return func(options *ValidationOptions) { | ||
options.SchemaFormatValidationEnabled = true | ||
} | ||
} | ||
|
||
// DisableSchemaPatternValidation makes Validate not return an error when validating patterns that are not supported by the Go regexp engine. | ||
func DisableSchemaPatternValidation() ValidationOption { | ||
return func(options *ValidationOptions) { | ||
options.SchemaPatternValidationDisabled = true | ||
} | ||
} | ||
|
||
// WithValidationOptions allows adding validation options to a context object that can be used when validationg any OpenAPI type. | ||
func WithValidationOptions(ctx context.Context, options *ValidationOptions) context.Context { | ||
return context.WithValue(ctx, validationOptionsKey{}, options) | ||
} | ||
|
||
func getValidationOptions(ctx context.Context) *ValidationOptions { | ||
if options, ok := ctx.Value(validationOptionsKey{}).(*ValidationOptions); ok { | ||
return options | ||
} | ||
return &ValidationOptions{} | ||
} |