Skip to content

Commit 4f92af0

Browse files
ySnoopyDogydaveshanley
authored andcommitted
better coverage lines
1 parent 3c4fb49 commit 4f92af0

2 files changed

Lines changed: 40 additions & 54 deletions

File tree

requests/validate_body.go

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
3147
func (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))

responses/validate_body.go

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ func (v *responseBodyValidator) ValidateResponseBodyWithPathItem(request *http.R
127127
return true, nil
128128
}
129129

130+
func generateXmlValidationError(err error, referenceObject string) []*errors.ValidationError {
131+
return []*errors.ValidationError{{
132+
ValidationType: helpers.RequestBodyValidation,
133+
ValidationSubType: helpers.Schema,
134+
Message: "xml response is malformed",
135+
Reason: fmt.Sprintf("failed to parse xml: %s", err.Error()),
136+
SchemaValidationErrors: []*errors.SchemaValidationFailure{{
137+
Reason: err.Error(),
138+
Location: "xml parsing",
139+
ReferenceSchema: "",
140+
ReferenceObject: referenceObject,
141+
}},
142+
HowToFix: "ensure xml is well-formed and matches schema structure",
143+
}}
144+
}
145+
130146
func (v *responseBodyValidator) checkResponseSchema(
131147
request *http.Request,
132148
response *http.Response,
@@ -156,38 +172,15 @@ func (v *responseBodyValidator) checkResponseSchema(
156172
responseBody, _ := io.ReadAll(response.Body)
157173
_ = response.Body.Close()
158174

159-
jsonBody, err := schema_validation.TransformXMLToSchemaJSON(string(responseBody), schema)
175+
stringedBody := string(responseBody)
176+
jsonBody, err := schema_validation.TransformXMLToSchemaJSON(stringedBody, schema)
160177
if err != nil {
161-
return []*errors.ValidationError{{
162-
ValidationType: helpers.RequestBodyValidation,
163-
ValidationSubType: helpers.Schema,
164-
Message: "xml response is malformed",
165-
Reason: fmt.Sprintf("failed to parse xml: %s", err.Error()),
166-
SchemaValidationErrors: []*errors.SchemaValidationFailure{{
167-
Reason: err.Error(),
168-
Location: "xml parsing",
169-
ReferenceSchema: "",
170-
ReferenceObject: string(responseBody),
171-
}},
172-
HowToFix: "ensure xml is well-formed and matches schema structure",
173-
}}
178+
return generateXmlValidationError(err, stringedBody)
174179
}
175180

176181
transformedBytes, err := json.Marshal(jsonBody)
177182
if err != nil {
178-
return []*errors.ValidationError{{
179-
ValidationType: helpers.RequestBodyValidation,
180-
ValidationSubType: helpers.Schema,
181-
Message: "xml example is malformed",
182-
Reason: fmt.Sprintf("failed to parse converted xml to json: %s", err.Error()),
183-
SchemaValidationErrors: []*errors.SchemaValidationFailure{{
184-
Reason: err.Error(),
185-
Location: "xml to json parsing",
186-
ReferenceSchema: "",
187-
ReferenceObject: string(responseBody),
188-
}},
189-
HowToFix: "ensure xml is well-formed and matches schema structure",
190-
}}
183+
return generateXmlValidationError(err, stringedBody)
191184
}
192185

193186
response.Body = io.NopCloser(bytes.NewBuffer(transformedBytes))

0 commit comments

Comments
 (0)