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

request body is not required by default #71

Merged
merged 1 commit into from
Apr 28, 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: 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
Loading