Skip to content

Commit 6a3bd26

Browse files
saisatishkarradaveshanley
authored andcommitted
fix: validate errors within head response object
1 parent 16629f8 commit 6a3bd26

5 files changed

Lines changed: 8 additions & 6 deletions

File tree

helpers/operation_utilities.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"mime"
88
"net/http"
99

10-
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
10+
"github.com/pb33f/libopenapi/datamodel/high/v3"
1111
)
1212

1313
// ExtractOperation extracts the operation from the path item based on the request method. If there is no

helpers/operation_utilities_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"net/http"
99
"testing"
1010

11-
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
11+
"github.com/pb33f/libopenapi/datamodel/high/v3"
1212
"github.com/stretchr/testify/require"
1313
)
1414

responses/validate_body.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ func (v *responseBodyValidator) checkResponseSchema(
138138
// extract schema from media type
139139
if mediaType.Schema != nil {
140140
schema := mediaType.Schema.Schema()
141+
141142
// Validate response schema
142143
valid, vErrs := ValidateResponseSchema(&ValidateResponseSchemaInput{
143144
Request: request,

responses/validate_response.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ func ValidateResponseSchema(input *ValidateResponseSchemaInput) (bool, []*errors
156156
request := input.Request
157157
response := input.Response
158158
schema := input.Schema
159+
159160

160161
if response == nil || response.Body == http.NoBody {
161162

162163
// skip response body validation for head request after processing schema
163-
if request != nil && request.Method == http.MethodHead {
164-
return true, validationErrors
164+
if response != nil && request != nil && request.Method == http.MethodHead {
165+
return len(validationErrors) == 0, validationErrors
165166
}
166-
167167
// cannot decode the response body, so it's not valid
168168
violation := &errors.SchemaValidationFailure{
169169
Reason: "response is empty",
@@ -215,6 +215,7 @@ func ValidateResponseSchema(input *ValidateResponseSchemaInput) (bool, []*errors
215215

216216
var decodedObj interface{}
217217

218+
218219
if len(responseBody) > 0 {
219220
// Per RFC7231, a response to a HEAD request MUST NOT include a message body.
220221
if request != nil && request.Method == http.MethodHead {

responses/validate_response_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ func TestValidateResponseSchema_ResponseMissing(t *testing.T) {
295295
schema := parseSchemaFromSpec(t, `type: object
296296
properties:
297297
name:
298-
type: string`, 3.1)
298+
type: string`, 3.1)
299299

300300
// Response body missing (NoBody) for a non-HEAD request should error
301301
valid, errs := ValidateResponseSchema(&ValidateResponseSchemaInput{

0 commit comments

Comments
 (0)