From 6e2a32712e972a3b25e937e81d9fd36be7398fd4 Mon Sep 17 00:00:00 2001 From: bsneha Date: Mon, 31 Jul 2017 03:56:23 +0100 Subject: [PATCH] Adding coditions to handle when response, http method is incorrect --- src/main/java/com/swagger/parser/Constants.java | 4 ++-- .../com/swagger/parser/SwaggerSchemaParser.java | 6 ++++-- .../swagger/parser/SwaggerSchemaResponseParser.java | 13 +++++++++---- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/swagger/parser/Constants.java b/src/main/java/com/swagger/parser/Constants.java index d1ec807..f105f77 100644 --- a/src/main/java/com/swagger/parser/Constants.java +++ b/src/main/java/com/swagger/parser/Constants.java @@ -6,6 +6,6 @@ public class Constants { public static final String INCORRECT_PATH = "Path not found in specification"; - - public static final String INCORRECT_HTTP_MTHHOD ="HTTP method is inccorect" ; + public static final String INCORRECT_HTTP_MTHHOD ="HTTP method is incorrect" ; + public static final String INCORRECT_RESPONSE_TYPE ="Response type is incorrect" ; } diff --git a/src/main/java/com/swagger/parser/SwaggerSchemaParser.java b/src/main/java/com/swagger/parser/SwaggerSchemaParser.java index 8cdcaec..dc11cdc 100644 --- a/src/main/java/com/swagger/parser/SwaggerSchemaParser.java +++ b/src/main/java/com/swagger/parser/SwaggerSchemaParser.java @@ -18,12 +18,14 @@ public class SwaggerSchemaParser { private Swagger swagger; private SwaggerParser swaggerParser; private HashMap parsedSchema; - private HashMap> parsedSchemaPerResponseType = new HashMap<>(); - ObjectMapper mapper = new ObjectMapper(); + private HashMap> parsedSchemaPerResponseType; + ObjectMapper mapper; SwaggerParserHelper swaggerParserHelper; String swaggerJsonUrl; public void initializeParser(String swaggerJsonUrl) { + parsedSchemaPerResponseType = new HashMap<>(); + mapper = new ObjectMapper(); this.swaggerJsonUrl = swaggerJsonUrl; swaggerParser = new SwaggerParser(); swagger = swaggerParser.read(swaggerJsonUrl); diff --git a/src/main/java/com/swagger/parser/SwaggerSchemaResponseParser.java b/src/main/java/com/swagger/parser/SwaggerSchemaResponseParser.java index 42b6f83..32d1555 100644 --- a/src/main/java/com/swagger/parser/SwaggerSchemaResponseParser.java +++ b/src/main/java/com/swagger/parser/SwaggerSchemaResponseParser.java @@ -39,16 +39,16 @@ public SwaggerResponseSchema parseReponseForGivenPathHTTPMethodAndResponseType(S HashMap swaggerStructurePerReponseType = new HashMap<>(); Path swaggerPath = swagger.getPaths().get(path); SwaggerResponseSchema swaggerResponseSchema = new SwaggerResponseSchema(); - if(swaggerPath==null){ + if (swaggerPath == null) { swaggerResponseSchema.setErrorMessage(Constants.INCORRECT_PATH); return swaggerResponseSchema; } Operation httpOperation = swaggerPath.getOperationMap().get(httpMethod); - if(httpOperation ==null){ + if (httpOperation == null) { swaggerResponseSchema.setErrorMessage(Constants.INCORRECT_HTTP_MTHHOD); return swaggerResponseSchema; } - Map responses = httpOperation.getResponses(); + Map responses = httpOperation.getResponses(); if (ResponseType.All == responseType) { responses.forEach((responseCode, response) -> { SwaggerSchema swaggerStructureForReponse = null; @@ -63,7 +63,12 @@ public SwaggerResponseSchema parseReponseForGivenPathHTTPMethodAndResponseType(S String responseCode = responseType.getCodeValue(); SwaggerSchema swaggerStructureForReponse = null; try { - swaggerStructureForReponse = getSchemaStructureFromResponse(responses.get(responseCode)); + Response response = responses.get(responseCode); + if (response == null) { + swaggerResponseSchema.setErrorMessage(Constants.INCORRECT_RESPONSE_TYPE); + return swaggerResponseSchema; + } + swaggerStructureForReponse = getSchemaStructureFromResponse(response); } catch (IOException e) { e.printStackTrace(); }