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

Use sync.Map like requestBodyValidator #67

Merged
merged 1 commit into from
Apr 25, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions responses/response_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
package responses

import (
"net/http"
"sync"

"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/pb33f/libopenapi/datamodel/high/v3"
"net/http"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
)

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

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

type schemaCache struct {
Expand All @@ -48,5 +50,5 @@ type responseBodyValidator struct {
pathItem *v3.PathItem
pathValue string
errors []*errors.ValidationError
schemaCache map[[32]byte]*schemaCache
schemaCache *sync.Map
}
13 changes: 6 additions & 7 deletions responses/validate_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,11 @@ func (v *responseBodyValidator) checkResponseSchema(
// have we seen this schema before? let's hash it and check the cache.
hash := mediaType.GoLow().Schema.Value.Hash()

if cacheHit, ch := v.schemaCache[hash]; ch {

if cacheHit, ch := v.schemaCache.Load(hash); ch {
// got a hit, use cached values
schema = cacheHit.schema
renderedInline = cacheHit.renderedInline
renderedJSON = cacheHit.renderedJSON
schema = cacheHit.(*schemaCache).schema
renderedInline = cacheHit.(*schemaCache).renderedInline
renderedJSON = cacheHit.(*schemaCache).renderedJSON

} else {

Expand All @@ -140,11 +139,11 @@ func (v *responseBodyValidator) checkResponseSchema(
schema = mediaType.Schema.Schema()
renderedInline, _ = schema.RenderInline()
renderedJSON, _ = utils.ConvertYAMLtoJSON(renderedInline)
v.schemaCache[hash] = &schemaCache{
v.schemaCache.Store(hash, &schemaCache{
schema: schema,
renderedInline: renderedInline,
renderedJSON: renderedJSON,
}
})
}

// render the schema, to be used for validation
Expand Down
Loading