Skip to content

Commit 2540a40

Browse files
k1LoWdaveshanley
authored andcommitted
Use sync.Map like requestBodyValidator
1 parent 6035624 commit 2540a40

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

responses/response_body.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
package responses
55

66
import (
7+
"net/http"
8+
"sync"
9+
710
"github.com/pb33f/libopenapi-validator/errors"
811
"github.com/pb33f/libopenapi/datamodel/high/base"
9-
"github.com/pb33f/libopenapi/datamodel/high/v3"
10-
"net/http"
12+
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
1113
)
1214

1315
// ResponseBodyValidator is an interface that defines the methods for validating response bodies for Operations.
@@ -34,7 +36,7 @@ func (v *responseBodyValidator) SetPathItem(path *v3.PathItem, pathValue string)
3436

3537
// NewResponseBodyValidator will create a new ResponseBodyValidator from an OpenAPI 3+ document
3638
func NewResponseBodyValidator(document *v3.Document) ResponseBodyValidator {
37-
return &responseBodyValidator{document: document, schemaCache: make(map[[32]byte]*schemaCache)}
39+
return &responseBodyValidator{document: document, schemaCache: &sync.Map{}}
3840
}
3941

4042
type schemaCache struct {
@@ -48,5 +50,5 @@ type responseBodyValidator struct {
4850
pathItem *v3.PathItem
4951
pathValue string
5052
errors []*errors.ValidationError
51-
schemaCache map[[32]byte]*schemaCache
53+
schemaCache *sync.Map
5254
}

responses/validate_body.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,11 @@ func (v *responseBodyValidator) checkResponseSchema(
126126
// have we seen this schema before? let's hash it and check the cache.
127127
hash := mediaType.GoLow().Schema.Value.Hash()
128128

129-
if cacheHit, ch := v.schemaCache[hash]; ch {
130-
129+
if cacheHit, ch := v.schemaCache.Load(hash); ch {
131130
// got a hit, use cached values
132-
schema = cacheHit.schema
133-
renderedInline = cacheHit.renderedInline
134-
renderedJSON = cacheHit.renderedJSON
131+
schema = cacheHit.(*schemaCache).schema
132+
renderedInline = cacheHit.(*schemaCache).renderedInline
133+
renderedJSON = cacheHit.(*schemaCache).renderedJSON
135134

136135
} else {
137136

@@ -140,11 +139,11 @@ func (v *responseBodyValidator) checkResponseSchema(
140139
schema = mediaType.Schema.Schema()
141140
renderedInline, _ = schema.RenderInline()
142141
renderedJSON, _ = utils.ConvertYAMLtoJSON(renderedInline)
143-
v.schemaCache[hash] = &schemaCache{
142+
v.schemaCache.Store(hash, &schemaCache{
144143
schema: schema,
145144
renderedInline: renderedInline,
146145
renderedJSON: renderedJSON,
147-
}
146+
})
148147
}
149148

150149
// render the schema, to be used for validation

0 commit comments

Comments
 (0)