Skip to content

Commit

Permalink
request body is not required by default
Browse files Browse the repository at this point in the history
  • Loading branch information
emilien-puget authored and daveshanley committed Apr 28, 2024
1 parent 5b5766b commit 5e5b19b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
8 changes: 8 additions & 0 deletions requests/validate_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ func (v *requestBodyValidator) ValidateRequestBody(request *http.Request) (bool,

// extract the content type from the request
contentType := request.Header.Get(helpers.ContentTypeHeader)
required := false
if operation.RequestBody.Required != nil {
required = *operation.RequestBody.Required
}
if contentType == "" {
if !required{
// request body is not required, the validation stop there.
return true, nil
}
return false, []*errors.ValidationError{errors.RequestContentTypeNotFound(operation, request, foundPath)}
}

Expand Down
89 changes: 87 additions & 2 deletions requests/validate_body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,45 @@ import (
"github.com/stretchr/testify/assert"
)

func TestValidateBody_MissingContentType(t *testing.T) {
func TestValidateBody_NotRequiredBody(t *testing.T) {
spec := `openapi: 3.1.0
paths:
/burgers/createBurger:
post:
requestBody:
required: false
content:
application/json:
schema:
type: object
properties:
name:
type: string
patties:
type: integer
vegetarian:
type: boolean`

doc, _ := libopenapi.NewDocument([]byte(spec))

m, _ := doc.BuildV3Model()
v := NewRequestBodyValidator(&m.Model)

request, _ := http.NewRequest(http.MethodPost, "https://things.com/burgers/createBurger", http.NoBody)

valid, errors := v.ValidateRequestBody(request)

assert.True(t, valid)
assert.Len(t, errors, 0)
}

func TestValidateBody_UnknownContentType(t *testing.T) {
spec := `openapi: 3.1.0
paths:
/burgers/createBurger:
post:
requestBody:
required: true
content:
application/json:
schema:
Expand Down Expand Up @@ -252,6 +285,7 @@ paths:
/burgers/createBurger:
post:
requestBody:
required: true
content:
application/json:
schema:
Expand Down Expand Up @@ -299,6 +333,7 @@ paths:
/burgers/createBurger:
post:
requestBody:
required: true
content:
application/json:
schema:
Expand Down Expand Up @@ -338,12 +373,62 @@ paths:

}

func TestValidateBody_InvalidBasicSchema(t *testing.T) {
func TestValidateBody_InvalidBasicSchema_NotRequired(t *testing.T) {
spec := `openapi: 3.1.0
paths:
/burgers/createBurger:
post:
requestBody:
required: false
content:
application/json:
schema:
type: object
properties:
name:
type: string
patties:
type: integer
vegetarian:
type: boolean`

doc, _ := libopenapi.NewDocument([]byte(spec))

m, _ := doc.BuildV3Model()
v := NewRequestBodyValidator(&m.Model)

// mix up the primitives to fire two schema violations.
body := map[string]interface{}{
"name": "Big Mac",
"patties": false,
"vegetarian": 2,
}

bodyBytes, _ := json.Marshal(body)

request, _ := http.NewRequest(http.MethodPost, "https://things.com/burgers/createBurger",
bytes.NewBuffer(bodyBytes))
request.Header.Set("Content-Type", "application/json")

valid, errors := v.ValidateRequestBody(request)

// double-tap to hit the cache
_, _ = v.ValidateRequestBody(request)

assert.False(t, valid)
assert.Len(t, errors, 1)
assert.Len(t, errors[0].SchemaValidationErrors, 2)
assert.Equal(t, "POST request body for '/burgers/createBurger' failed to validate schema", errors[0].Message)

}

func TestValidateBody_InvalidBasicSchema_Required(t *testing.T) {
spec := `openapi: 3.1.0
paths:
/burgers/createBurger:
post:
requestBody:
required: false
content:
application/json:
schema:
Expand Down

0 comments on commit 5e5b19b

Please sign in to comment.