Skip to content

Commit

Permalink
Addressed issue #48
Browse files Browse the repository at this point in the history
Regression intriduced with a satefy check, regression has been undone.

Signed-off-by: Dave Shanley <[email protected]>
  • Loading branch information
daveshanley committed Jan 27, 2024
1 parent 78620cd commit 657229e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
26 changes: 14 additions & 12 deletions responses/validate_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,22 @@ func (v *responseBodyValidator) ValidateResponseBody(

// check if the response code is in the contract
foundResponse := operation.Responses.FindResponseByCode(httpCode)
if foundResponse != nil && foundResponse.Content != nil {
// check content type has been defined in the contract
if mediaType, ok := foundResponse.Content.Get(mediaTypeSting); ok {
validationErrors = append(validationErrors,
v.checkResponseSchema(request, response, mediaTypeSting, mediaType)...)
} else {
// check that the operation *actually* returns a body. (i.e. a 204 response)
if foundResponse.Content != nil && orderedmap.Len(foundResponse.Content) > 0 {

// content type not found in the contract
codeStr := strconv.Itoa(httpCode)
if foundResponse != nil {
if foundResponse.Content != nil { // only validate if we have content types.
// check content type has been defined in the contract
if mediaType, ok := foundResponse.Content.Get(mediaTypeSting); ok {
validationErrors = append(validationErrors,
errors.ResponseContentTypeNotFound(operation, request, response, codeStr, false))
v.checkResponseSchema(request, response, mediaTypeSting, mediaType)...)
} else {
// check that the operation *actually* returns a body. (i.e. a 204 response)
if foundResponse.Content != nil && orderedmap.Len(foundResponse.Content) > 0 {

// content type not found in the contract
codeStr := strconv.Itoa(httpCode)
validationErrors = append(validationErrors,
errors.ResponseContentTypeNotFound(operation, request, response, codeStr, false))

}
}
}
} else {
Expand Down
42 changes: 42 additions & 0 deletions responses/validate_body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1042,3 +1042,45 @@ paths:
assert.Equal(t, "invalid character '}' looking for beginning of object key string", errors[0].SchemaValidationErrors[0].Reason)

}

func TestValidateBody_NoContentType_Valid(t *testing.T) {
spec := `openapi: "3.0.0"
info:
title: Healthcheck
version: '0.1.0'
paths:
/health:
get:
responses:
'200':
description: pet response`

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

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

// build a request
request, _ := http.NewRequest(http.MethodGet, "https://things.com/health", nil)

// simulate a request/response
res := httptest.NewRecorder()
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(helpers.ContentTypeHeader, helpers.JSONContentType)
w.WriteHeader(http.StatusOK)
_, _ = w.Write(nil)
}

// fire the request
handler(res, request)

// record response
response := res.Result()

// validate!
valid, errors := v.ValidateResponseBody(request, response)

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

}

0 comments on commit 657229e

Please sign in to comment.