From 41cb2de894ebf34f2efc6771ce3fe904e4f9cfa7 Mon Sep 17 00:00:00 2001 From: Martins Irbe Date: Fri, 26 Apr 2024 17:16:51 +0100 Subject: [PATCH] Add test case causing panic on request body validation --- requests/validate_request_test.go | 53 +++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 requests/validate_request_test.go diff --git a/requests/validate_request_test.go b/requests/validate_request_test.go new file mode 100644 index 0000000..26ca070 --- /dev/null +++ b/requests/validate_request_test.go @@ -0,0 +1,53 @@ +package requests + +import ( + "io" + "net/http" + "strings" + "testing" + + "github.com/pb33f/libopenapi/datamodel/high/base" + "github.com/stretchr/testify/assert" +) + +func TestValidateRequestSchema(t *testing.T) { + for name, tc := range map[string]struct { + request *http.Request + schema *base.Schema + renderedSchema, jsonSchema []byte + assertValidRequestSchema assert.BoolAssertionFunc + expectedErrorsCount int + }{ + "FailRequestBodyValidation": { + // KeywordLocation: /allOf/1/$ref/properties/properties/additionalProperties/$dynamicRef/allOf/3/$ref/properties/exclusiveMinimum/type + // Message: expected number, but got boolean + request: &http.Request{ + Method: http.MethodPost, + Body: io.NopCloser(strings.NewReader(`{"exclusiveNumber": 13}`)), + }, + schema: &base.Schema{ + Type: []string{"object"}, + }, + renderedSchema: []byte(`type: object +properties: + exclusiveNumber: + type: number + description: This number starts its journey where most numbers are too scared to begin! + exclusiveMinimum: true + minimum: !!float 10`), + jsonSchema: []byte(`{"properties":{"exclusiveNumber":{"description":"This number starts its journey where most numbers are too scared to begin!","exclusiveMinimum":true,"minimum":10,"type":"number"}},"type":"object"}`), + assertValidRequestSchema: assert.False, + expectedErrorsCount: 1, + }, + } { + tc := tc + t.Run(name, func(t *testing.T) { + t.Parallel() + + valid, errors := ValidateRequestSchema(tc.request, tc.schema, tc.renderedSchema, tc.jsonSchema) + + tc.assertValidRequestSchema(t, valid) + assert.Len(t, errors, tc.expectedErrorsCount) + }) + } +}