Skip to content

Commit

Permalink
openapi3filter: guard BodyEncoder registration behind a RW lock
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>
  • Loading branch information
fenollp committed Apr 6, 2024
1 parent b6f165a commit c5fe704
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions openapi3filter/req_resp_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,56 @@ package openapi3filter
import (
"encoding/json"
"fmt"
"sync"
)

func encodeBody(body interface{}, mediaType string) ([]byte, error) {
encoder, ok := bodyEncoders[mediaType]
if !ok {
return nil, &ParseError{
Kind: KindUnsupportedFormat,
Reason: fmt.Sprintf("%s %q", prefixUnsupportedCT, mediaType),
}
if encoder := RegisteredBodyEncoder(mediaType); encoder != nil {
return encoder(body)
}
return nil, &ParseError{
Kind: KindUnsupportedFormat,
Reason: fmt.Sprintf("%s %q", prefixUnsupportedCT, mediaType),
}
return encoder(body)
}

// BodyEncoder really is an (encoding/json).Marshaler
type BodyEncoder func(body interface{}) ([]byte, error)

var bodyEncodersM sync.RWMutex
var bodyEncoders = map[string]BodyEncoder{
"application/json": json.Marshal,
}

// RegisterBodyEncoder enables package-wide decoding of contentType values
func RegisterBodyEncoder(contentType string, encoder BodyEncoder) {
if contentType == "" {
panic("contentType is empty")
}
if encoder == nil {
panic("encoder is not defined")
}
bodyEncodersM.Lock()
bodyEncoders[contentType] = encoder
bodyEncodersM.Unlock()
}

// This call is not thread-safe: body encoders should not be created/destroyed by multiple goroutines.
// UnregisterBodyEncoder disables package-wide decoding of contentType values
func UnregisterBodyEncoder(contentType string) {
if contentType == "" {
panic("contentType is empty")
}
bodyEncodersM.Lock()
delete(bodyEncoders, contentType)
bodyEncodersM.Unlock()
}

// RegisteredBodyEncoder returns the registered body encoder for the given content type.
//
// If no encoder was registered for the given content type, nil is returned.
// This call is not thread-safe: body encoders should not be created/destroyed by multiple goroutines.
func RegisteredBodyEncoder(contentType string) BodyEncoder {
return bodyEncoders[contentType]
bodyEncodersM.RLock()
mayBE := bodyEncoders[contentType]
bodyEncodersM.RUnlock()
return mayBE
}

0 comments on commit c5fe704

Please sign in to comment.