Skip to content

Commit

Permalink
openapi3: fix regex replacing \u literals (#918)
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagownt committed Mar 5, 2024
1 parent 672dc32 commit 7aa9f7e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
4 changes: 2 additions & 2 deletions openapi3/schema_pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"regexp"
)

var patRewriteCodepoints = regexp.MustCompile(`[\][u]([0-9A-F]{4})`)
var patRewriteCodepoints = regexp.MustCompile(`(?P<replaced_with_slash_x>\\u)(?P<code>[0-9A-F]{4})`)

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

// NOTE: racey WRT [writes to schema.Pattern] vs [reads schema.Pattern then writes to compiledPatterns]
Expand Down
1 change: 1 addition & 0 deletions openapi3/schema_pattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ func TestPattern(t *testing.T) {
require.NoError(t, err)

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

0 comments on commit 7aa9f7e

Please sign in to comment.