@@ -37,7 +37,51 @@ func ValidateResponseSchema(
3737
3838 var validationErrors []* errors.ValidationError
3939
40- responseBody , _ := io .ReadAll (response .Body )
40+ if response == nil {
41+ // cannot decode the response body, so it's not valid
42+ violation := & errors.SchemaValidationFailure {
43+ Reason : "response is empty" ,
44+ Location : "unavailable" ,
45+ ReferenceSchema : string (renderedSchema ),
46+ }
47+ validationErrors = append (validationErrors , & errors.ValidationError {
48+ ValidationType : "response" ,
49+ ValidationSubType : "object" ,
50+ Message : fmt .Sprintf ("%s response object is missing for '%s'" ,
51+ request .Method , request .URL .Path ),
52+ Reason : fmt .Sprintf ("The response object is completely missing" ),
53+ SpecLine : 1 ,
54+ SpecCol : 0 ,
55+ SchemaValidationErrors : []* errors.SchemaValidationFailure {violation },
56+ HowToFix : "ensure response object has been set" ,
57+ Context : string (renderedSchema ), // attach the rendered schema to the error
58+ })
59+ return false , validationErrors
60+ }
61+
62+ responseBody , ioErr := io .ReadAll (response .Body )
63+ if ioErr != nil {
64+ // cannot decode the response body, so it's not valid
65+ violation := & errors.SchemaValidationFailure {
66+ Reason : ioErr .Error (),
67+ Location : "unavailable" ,
68+ ReferenceSchema : string (renderedSchema ),
69+ ReferenceObject : string (responseBody ),
70+ }
71+ validationErrors = append (validationErrors , & errors.ValidationError {
72+ ValidationType : helpers .ResponseBodyValidation ,
73+ ValidationSubType : helpers .Schema ,
74+ Message : fmt .Sprintf ("%s response body for '%s' cannot be read, it's empty or malformed" ,
75+ request .Method , request .URL .Path ),
76+ Reason : fmt .Sprintf ("The response body cannot be decoded: %s" , ioErr .Error ()),
77+ SpecLine : 1 ,
78+ SpecCol : 0 ,
79+ SchemaValidationErrors : []* errors.SchemaValidationFailure {violation },
80+ HowToFix : "ensure body is not empty" ,
81+ Context : string (renderedSchema ), // attach the rendered schema to the error
82+ })
83+ return false , validationErrors
84+ }
4185
4286 // close the request body, so it can be re-read later by another player in the chain
4387 _ = response .Body .Close ()
0 commit comments