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

openapi3filter: guard BodyEncoder registration behind a RW lock #934

Merged
merged 2 commits into from
Apr 6, 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
8 changes: 4 additions & 4 deletions .github/docs/openapi3filter.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func RegisterBodyDecoder(contentType string, decoder BodyDecoder)
body decoders should not be created/destroyed by multiple goroutines.

func RegisterBodyEncoder(contentType string, encoder BodyEncoder)
RegisterBodyEncoder enables package-wide decoding of contentType values

func TrimJSONPrefix(data []byte) []byte
TrimJSONPrefix trims one of the possible prefixes

Expand All @@ -80,8 +82,7 @@ func UnregisterBodyDecoder(contentType string)
goroutines.

func UnregisterBodyEncoder(contentType string)
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 ValidateParameter(ctx context.Context, input *RequestValidationInput, parameter *openapi3.Parameter) error
ValidateParameter validates a parameter's value by JSON schema. The function
Expand Down Expand Up @@ -152,14 +153,13 @@ func RegisteredBodyDecoder(contentType string) BodyDecoder
by multiple goroutines.

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

func RegisteredBodyEncoder(contentType string) BodyEncoder
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.

type ContentParameterDecoder func(param *openapi3.Parameter, values []string) (interface{}, *openapi3.Schema, error)
A ContentParameterDecoder takes a parameter definition from the OpenAPI
Expand Down
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
}
Loading