-
-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
openapi3: support \uC4FE codepoint syntax in Schema.Pattern (#873)
- Loading branch information
Showing
3 changed files
with
47 additions
and
16 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
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 | ||
} |
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,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]+$`)) | ||
} |