@@ -28,6 +28,22 @@ func (v *requestBodyValidator) ValidateRequestBody(request *http.Request) (bool,
2828 return v .ValidateRequestBodyWithPathItem (request , pathItem , foundPath )
2929}
3030
31+ func generateXmlValidationError (err error , referenceObject string ) []* errors.ValidationError {
32+ return []* errors.ValidationError {{
33+ ValidationType : helpers .RequestBodyValidation ,
34+ ValidationSubType : helpers .Schema ,
35+ Message : "xml example is malformed" ,
36+ Reason : fmt .Sprintf ("failed to parse xml: %s" , err .Error ()),
37+ SchemaValidationErrors : []* errors.SchemaValidationFailure {{
38+ Reason : err .Error (),
39+ Location : "xml parsing" ,
40+ ReferenceSchema : "" ,
41+ ReferenceObject : referenceObject ,
42+ }},
43+ HowToFix : "ensure xml is well-formed and matches schema structure" ,
44+ }}
45+ }
46+
3147func (v * requestBodyValidator ) ValidateRequestBodyWithPathItem (request * http.Request , pathItem * v3.PathItem , pathValue string ) (bool , []* errors.ValidationError ) {
3248 if pathItem == nil {
3349 return false , []* errors.ValidationError {{
@@ -89,38 +105,15 @@ func (v *requestBodyValidator) ValidateRequestBodyWithPathItem(request *http.Req
89105 requestBody , _ := io .ReadAll (request .Body )
90106 _ = request .Body .Close ()
91107
92- jsonBody , err := schema_validation .TransformXMLToSchemaJSON (string (requestBody ), schema )
108+ stringedBody := string (requestBody )
109+ jsonBody , err := schema_validation .TransformXMLToSchemaJSON (stringedBody , schema )
93110 if err != nil {
94- return false , []* errors.ValidationError {{
95- ValidationType : helpers .RequestBodyValidation ,
96- ValidationSubType : helpers .Schema ,
97- Message : "xml example is malformed" ,
98- Reason : fmt .Sprintf ("failed to parse xml: %s" , err .Error ()),
99- SchemaValidationErrors : []* errors.SchemaValidationFailure {{
100- Reason : err .Error (),
101- Location : "xml parsing" ,
102- ReferenceSchema : "" ,
103- ReferenceObject : string (requestBody ),
104- }},
105- HowToFix : "ensure xml is well-formed and matches schema structure" ,
106- }}
111+ return false , generateXmlValidationError (err , stringedBody )
107112 }
108113
109114 transformedBytes , err := json .Marshal (jsonBody )
110115 if err != nil {
111- return false , []* errors.ValidationError {{
112- ValidationType : helpers .RequestBodyValidation ,
113- ValidationSubType : helpers .Schema ,
114- Message : "xml example is malformed" ,
115- Reason : fmt .Sprintf ("failed to parse converted xml to json: %s" , err .Error ()),
116- SchemaValidationErrors : []* errors.SchemaValidationFailure {{
117- Reason : err .Error (),
118- Location : "xml to json parsing" ,
119- ReferenceSchema : "" ,
120- ReferenceObject : string (requestBody ),
121- }},
122- HowToFix : "ensure xml is well-formed and matches schema structure" ,
123- }}
116+ return false , generateXmlValidationError (err , stringedBody )
124117 }
125118
126119 request .Body = io .NopCloser (bytes .NewBuffer (transformedBytes ))
0 commit comments