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

Validate JSON body before unmarshalling into input type #203

Closed
wants to merge 3 commits into from
Closed
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 openapi/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ func (c *Collector) ProvideRequestJSONSchemas(

r := c.Refl()

err := r.WalkRequestJSONSchemas(method, cu, c.jsonSchemaCallback(validator, r), func(oc openapi.OperationContext) {
err := r.WalkRequestJSONSchemas(method, cu, c.jsonSchemaCallback(validator), func(oc openapi.OperationContext) {
fv, ok := validator.(unknownFieldsValidator)
if !ok {
return
Expand Down Expand Up @@ -549,19 +549,19 @@ func (c *Collector) ProvideResponseJSONSchemas(
}

r := c.Refl()
err := r.WalkResponseJSONSchemas(cu, c.jsonSchemaCallback(validator, r), nil)
err := r.WalkResponseJSONSchemas(cu, c.jsonSchemaCallback(validator), nil)

return err
}

func (c *Collector) jsonSchemaCallback(validator rest.JSONSchemaValidator, r openapi.Reflector) openapi.JSONSchemaCallback {
func (c *Collector) jsonSchemaCallback(validator rest.JSONSchemaValidator) openapi.JSONSchemaCallback {
return func(in openapi.In, paramName string, schema *jsonschema.SchemaOrBool, required bool) error {
loc := string(in) + "." + paramName
if loc == "body.body" {
loc = "body"
}

if schema == nil || schema.IsTrivial(r.ResolveJSONSchemaRef) {
if schema == nil {
if err := validator.AddSchema(rest.ParamIn(in), paramName, nil, required); err != nil {
return fmt.Errorf("add validation schema %s: %w", loc, err)
}
Expand Down
30 changes: 11 additions & 19 deletions request/jsonbody.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,26 @@ func decodeJSONBody(readJSON func(rd io.Reader, v interface{}) error, tolerateFo
return nil
}

var (
rd io.Reader = r.Body
b *bytes.Buffer
)
b := bufPool.Get().(*bytes.Buffer) //nolint:errcheck // bufPool is configured to provide *bytes.Buffer.
defer bufPool.Put(b)
b.Reset()

validate := validator != nil && validator.HasConstraints(rest.ParamInBody)

if validate {
b = bufPool.Get().(*bytes.Buffer) //nolint:errcheck // bufPool is configured to provide *bytes.Buffer.
defer bufPool.Put(b)

b.Reset()
rd = io.TeeReader(r.Body, b)
// Read body into buffer.
if _, err := b.ReadFrom(r.Body); err != nil {
return err
}

err := readJSON(rd, &input)
if err != nil {
return fmt.Errorf("failed to decode json: %w", err)
}
validate := validator != nil && validator.HasConstraints(rest.ParamInBody)

if validator != nil && validate {
err = validator.ValidateJSONBody(b.Bytes())
if validate {
// Perform validation before unmarshalling into input object.
err := validator.ValidateJSONBody(b.Bytes())
if err != nil {
return err
}
}

return nil
return readJSON(b, input)
}
}

Expand Down
2 changes: 1 addition & 1 deletion request/jsonbody_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func Test_decodeJSONBody_unmarshalFailed(t *testing.T) {
var i []int

err = decodeJSONBody(readJSON, false)(req, &i, nil)
assert.EqualError(t, err, "failed to decode json: json: cannot unmarshal number into Go value of type []int")
assert.EqualError(t, err, "json: cannot unmarshal number into Go value of type []int")
}

func Test_decodeJSONBody_validateFailed(t *testing.T) {
Expand Down