Skip to content

Commit 657229e

Browse files
committed
Addressed issue #48
Regression intriduced with a satefy check, regression has been undone. Signed-off-by: Dave Shanley <[email protected]>
1 parent 78620cd commit 657229e

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

responses/validate_body.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,22 @@ func (v *responseBodyValidator) ValidateResponseBody(
4646

4747
// check if the response code is in the contract
4848
foundResponse := operation.Responses.FindResponseByCode(httpCode)
49-
if foundResponse != nil && foundResponse.Content != nil {
50-
// check content type has been defined in the contract
51-
if mediaType, ok := foundResponse.Content.Get(mediaTypeSting); ok {
52-
validationErrors = append(validationErrors,
53-
v.checkResponseSchema(request, response, mediaTypeSting, mediaType)...)
54-
} else {
55-
// check that the operation *actually* returns a body. (i.e. a 204 response)
56-
if foundResponse.Content != nil && orderedmap.Len(foundResponse.Content) > 0 {
57-
58-
// content type not found in the contract
59-
codeStr := strconv.Itoa(httpCode)
49+
if foundResponse != nil {
50+
if foundResponse.Content != nil { // only validate if we have content types.
51+
// check content type has been defined in the contract
52+
if mediaType, ok := foundResponse.Content.Get(mediaTypeSting); ok {
6053
validationErrors = append(validationErrors,
61-
errors.ResponseContentTypeNotFound(operation, request, response, codeStr, false))
54+
v.checkResponseSchema(request, response, mediaTypeSting, mediaType)...)
55+
} else {
56+
// check that the operation *actually* returns a body. (i.e. a 204 response)
57+
if foundResponse.Content != nil && orderedmap.Len(foundResponse.Content) > 0 {
6258

59+
// content type not found in the contract
60+
codeStr := strconv.Itoa(httpCode)
61+
validationErrors = append(validationErrors,
62+
errors.ResponseContentTypeNotFound(operation, request, response, codeStr, false))
63+
64+
}
6365
}
6466
}
6567
} else {

responses/validate_body_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,3 +1042,45 @@ paths:
10421042
assert.Equal(t, "invalid character '}' looking for beginning of object key string", errors[0].SchemaValidationErrors[0].Reason)
10431043

10441044
}
1045+
1046+
func TestValidateBody_NoContentType_Valid(t *testing.T) {
1047+
spec := `openapi: "3.0.0"
1048+
info:
1049+
title: Healthcheck
1050+
version: '0.1.0'
1051+
paths:
1052+
/health:
1053+
get:
1054+
responses:
1055+
'200':
1056+
description: pet response`
1057+
1058+
doc, _ := libopenapi.NewDocument([]byte(spec))
1059+
1060+
m, _ := doc.BuildV3Model()
1061+
v := NewResponseBodyValidator(&m.Model)
1062+
1063+
// build a request
1064+
request, _ := http.NewRequest(http.MethodGet, "https://things.com/health", nil)
1065+
1066+
// simulate a request/response
1067+
res := httptest.NewRecorder()
1068+
handler := func(w http.ResponseWriter, r *http.Request) {
1069+
w.Header().Set(helpers.ContentTypeHeader, helpers.JSONContentType)
1070+
w.WriteHeader(http.StatusOK)
1071+
_, _ = w.Write(nil)
1072+
}
1073+
1074+
// fire the request
1075+
handler(res, request)
1076+
1077+
// record response
1078+
response := res.Result()
1079+
1080+
// validate!
1081+
valid, errors := v.ValidateResponseBody(request, response)
1082+
1083+
assert.True(t, valid)
1084+
assert.Len(t, errors, 0)
1085+
1086+
}

0 commit comments

Comments
 (0)