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

openapi3: support \uC4FE codepoint syntax in Schema.Pattern #873

Merged
merged 1 commit into from
Nov 26, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 0 additions & 16 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2011,22 +2011,6 @@ func (schema *Schema) expectedType(settings *schemaValidationSettings, value int
}
}

// NOTE: racey WRT [writes to schema.Pattern] vs [reads schema.Pattern then writes to compiledPatterns]
func (schema *Schema) compilePattern() (cp *regexp.Regexp, err error) {
pattern := schema.Pattern
if cp, err = regexp.Compile(pattern); err != nil {
err = &SchemaError{
Schema: schema,
SchemaField: "pattern",
Origin: err,
Reason: fmt.Sprintf("cannot compile pattern %q: %v", pattern, err),
}
return
}
var _ bool = compiledPatterns.CompareAndSwap(pattern, nil, cp)
return
}

// SchemaError is an error that occurs during schema validation.
type SchemaError struct {
// Value is the value that failed validation.
Expand Down
29 changes: 29 additions & 0 deletions openapi3/schema_pattern.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package openapi3

import (
"fmt"
"regexp"
)

var patRewriteCodepoints = regexp.MustCompile(`[\][u]([0-9A-F]{4})`)

// See https://pkg.go.dev/regexp/syntax
func intoGoRegexp(re string) string {
return patRewriteCodepoints.ReplaceAllString(re, `x{$1}`)
}

// NOTE: racey WRT [writes to schema.Pattern] vs [reads schema.Pattern then writes to compiledPatterns]
func (schema *Schema) compilePattern() (cp *regexp.Regexp, err error) {
pattern := schema.Pattern
if cp, err = regexp.Compile(intoGoRegexp(pattern)); err != nil {
err = &SchemaError{
Schema: schema,
SchemaField: "pattern",
Origin: err,
Reason: fmt.Sprintf("cannot compile pattern %q: %v", pattern, err),
}
return
}
var _ bool = compiledPatterns.CompareAndSwap(pattern, nil, cp)
return
}
18 changes: 18 additions & 0 deletions openapi3/schema_pattern_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package openapi3

import (
"regexp"
"testing"

"github.com/stretchr/testify/require"
)

func TestPattern(t *testing.T) {
_, err := regexp.Compile("^[a-zA-Z\\u0080-\\u024F\\s\\/\\-\\)\\(\\`\\.\\\"\\']+$")
require.EqualError(t, err, "error parsing regexp: invalid escape sequence: `\\u`")

_, err = regexp.Compile(`^[a-zA-Z\x{0080}-\x{024F}]+$`)
require.NoError(t, err)

require.Equal(t, `^[a-zA-Z\x{0080}-\x{024F}]+$`, intoGoRegexp(`^[a-zA-Z\u0080-\u024F]+$`))
}