Skip to content

Commit 783206b

Browse files
authored
openapi3: fix ineffectual caching of compiled regexps (#1076)
* Fix openapi regex caching. * Remove stray line
1 parent 90fb641 commit 783206b

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

openapi3/schema_pattern.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ func (schema *Schema) compilePattern(c RegexCompilerFunc) (cp RegexMatcher, err
3030
return
3131
}
3232

33-
var _ bool = compiledPatterns.CompareAndSwap(pattern, nil, cp)
33+
compiledPatterns.Store(pattern, cp)
3434
return
3535
}

openapi3/schema_pattern_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,17 @@ func TestPattern(t *testing.T) {
1717
require.Equal(t, `^[a-zA-Z\x{0080}-\x{024F}]+$`, intoGoRegexp(`^[a-zA-Z\u0080-\u024F]+$`))
1818
require.Equal(t, `^[6789a-zA-Z\x{0080}-\x{024F}]+$`, intoGoRegexp(`^[6789a-zA-Z\u0080-\u024F]+$`))
1919
}
20+
21+
func TestSchemaPatternCache(t *testing.T) {
22+
var schema Schema
23+
24+
schema.Pattern = `^[a-zA-Z\x{0080}-\x{024F}]+$`
25+
cp, err := schema.compilePattern(nil)
26+
require.NoError(t, err)
27+
require.NotNil(t, cp)
28+
29+
// The compiled pattern should be cached
30+
v, ok := compiledPatterns.Load(schema.Pattern)
31+
require.True(t, ok)
32+
require.Equal(t, cp, v)
33+
}

0 commit comments

Comments
 (0)