@@ -5,6 +5,7 @@ package responses
55
66import (
77 "fmt"
8+ "gopkg.in/yaml.v3"
89 "net/http"
910 "strconv"
1011 "strings"
@@ -146,20 +147,39 @@ func (v *responseBodyValidator) checkResponseSchema(
146147
147148 // render the schema inline and perform the intensive work of rendering and converting
148149 // this is only performed once per schema and cached in the validator.
149- schema = mediaType .Schema .Schema ()
150- renderedInline , _ = schema .RenderInline ()
151- renderedJSON , _ = utils .ConvertYAMLtoJSON (renderedInline )
152- v .schemaCache .Store (hash , & schemaCache {
153- schema : schema ,
154- renderedInline : renderedInline ,
155- renderedJSON : renderedJSON ,
156- })
150+ schemaP := mediaType .Schema
151+ marshalled , mErr := schemaP .MarshalYAMLInline ()
152+
153+ if mErr != nil {
154+ validationErrors = append (validationErrors , & errors.ValidationError {
155+ Reason : mErr .Error (),
156+ Message : fmt .Sprintf ("unable to marshal schema for %s" , contentType ),
157+ ValidationType : helpers .ResponseBodyValidation ,
158+ ValidationSubType : helpers .Schema ,
159+ SpecLine : mediaType .Schema .GetSchemaKeyNode ().Line ,
160+ SpecCol : mediaType .Schema .GetSchemaKeyNode ().Column ,
161+ RequestPath : request .URL .Path ,
162+ RequestMethod : request .Method ,
163+ HowToFix : "ensure schema is valid and does not contain circular references" ,
164+ })
165+ } else {
166+ schema = schemaP .Schema ()
167+ renderedInline , _ = yaml .Marshal (marshalled )
168+ renderedJSON , _ = utils .ConvertYAMLtoJSON (renderedInline )
169+ v .schemaCache .Store (hash , & schemaCache {
170+ schema : schema ,
171+ renderedInline : renderedInline ,
172+ renderedJSON : renderedJSON ,
173+ })
174+ }
157175 }
158176
177+ if len (renderedInline ) > 0 && len (renderedJSON ) > 0 && schema != nil {
159178 // render the schema, to be used for validation
160179 valid , vErrs := ValidateResponseSchema (request , response , schema , renderedInline , renderedJSON , config .WithRegexEngine (v .options .RegexEngine ))
161180 if ! valid {
162181 validationErrors = append (validationErrors , vErrs ... )
182+ }
163183 }
164184 }
165185 }
0 commit comments