40
40
import com .eviware .x .form .XFormDialogBuilder ;
41
41
import com .eviware .x .form .XFormFactory ;
42
42
import com .fasterxml .jackson .databind .JsonNode ;
43
+ import com .fasterxml .jackson .databind .ObjectMapper ;
44
+ import com .fasterxml .jackson .dataformat .xml .XmlMapper ;
43
45
import com .github .fge .jsonschema .core .exceptions .ProcessingException ;
44
46
import com .github .fge .jsonschema .core .load .configuration .LoadingConfiguration ;
45
47
import com .github .fge .jsonschema .main .JsonSchema ;
@@ -192,7 +194,7 @@ private boolean validateMessage(HttpMessageExchange messageExchange, SubmitConte
192
194
Operation operation = findOperation (swagger .getPath (swaggerPath ), method );
193
195
if (operation != null ) {
194
196
validateOperation (swagger , operation , String .valueOf (messageExchange .getResponseStatusCode ()),
195
- messageExchange .getResponseContent ()
197
+ messageExchange .getResponseContent (), messageExchange . getResponseContentType ()
196
198
);
197
199
198
200
return true ;
@@ -228,31 +230,31 @@ private Operation findOperation(Path path, RestRequestInterface.HttpMethod metho
228
230
return null ;
229
231
}
230
232
231
- void validateOperation (Swagger swagger , Operation operation , String responseCode , String contentAsString ) throws AssertionException {
233
+ void validateOperation (Swagger swagger , Operation operation , String responseCode , String contentAsString , String contentType ) throws AssertionException {
232
234
233
235
Response responseSchema = operation .getResponses ().get (responseCode );
234
236
if (responseSchema == null ) {
235
237
responseSchema = operation .getResponses ().get ("default" );
236
238
}
237
239
238
240
if (responseSchema != null ) {
239
- validateResponse (contentAsString , swagger , responseSchema );
241
+ validateResponse (contentAsString , contentType , swagger , responseSchema );
240
242
} else if (strictMode ) {
241
243
throw new AssertionException (new AssertionError (
242
244
"Missing response definition for " + responseCode + " response in operation " + operation .getOperationId ()));
243
245
}
244
246
}
245
247
246
- void validateResponse (String contentAsString , Swagger swagger , Response responseSchema ) throws AssertionException {
248
+ void validateResponse (String contentAsString , String contentType , Swagger swagger , Response responseSchema ) throws AssertionException {
247
249
if (responseSchema .getSchema () != null ) {
248
250
Property schema = responseSchema .getSchema ();
249
251
if (schema instanceof RefProperty ) {
250
252
Model model = swagger .getDefinitions ().get (((RefProperty ) schema ).getSimpleRef ());
251
253
if (model != null ) {
252
- validatePayload (contentAsString , null );
254
+ validatePayload (contentAsString , null , contentType );
253
255
}
254
256
} else {
255
- validatePayload (contentAsString , Json .pretty (schema ));
257
+ validatePayload (contentAsString , Json .pretty (schema ), contentType );
256
258
}
257
259
}
258
260
}
@@ -309,7 +311,7 @@ private Swagger parseFileContent() throws AssertionException {
309
311
}
310
312
}
311
313
312
- public void validatePayload (String payload , String schema ) throws AssertionException {
314
+ public void validatePayload (String payload , String schema , String contentType ) throws AssertionException {
313
315
try {
314
316
JsonSchema jsonSchema ;
315
317
@@ -328,7 +330,16 @@ public void validatePayload(String payload, String schema) throws AssertionExcep
328
330
jsonSchema = getSwaggerSchema ();
329
331
}
330
332
331
- JsonNode contentObject = Json .mapper ().readTree (payload );
333
+ JsonNode contentObject ;
334
+
335
+ if (contentType .equalsIgnoreCase ("application/json" )) {
336
+ contentObject = Json .mapper ().readTree (payload );
337
+ } else if (contentType .equalsIgnoreCase ("application/xml" )) {
338
+ final XmlMapper xmlMapper = new XmlMapper ();
339
+ contentObject = xmlMapper .readTree (payload );
340
+ } else {
341
+ throw new AssertionException (new AssertionError ("Swagger Compliance testing failed. Invalid content type: " + contentType ));
342
+ }
332
343
333
344
ValidationSupport .validateMessage (jsonSchema , contentObject );
334
345
} catch (AssertionException e ) {
0 commit comments