From e170025de65136da8807ee673d6474f81ae48776 Mon Sep 17 00:00:00 2001 From: Kerry Kimbrough Date: Sat, 1 Aug 2020 16:20:36 -0500 Subject: [PATCH] When parameter style is invalid, return a helpful message instead of throwing an NPE --- .../v3/parser/util/OpenAPIDeserializer.java | 4 +- .../parser/util/OpenAPIDeserializerTest.java | 50 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java index 87834e1f89..80033c6f59 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java @@ -1561,7 +1561,7 @@ public Parameter getParameter(ObjectNode obj, String location, ParseResult resul Boolean explode = getBoolean("explode", obj, false, location, result); if (explode != null) { parameter.setExplode(explode); - } else if(parameter.getStyle().equals(StyleEnum.FORM)){ + } else if(StyleEnum.FORM.equals(parameter.getStyle())){ parameter.setExplode(Boolean.TRUE); } else { parameter.setExplode(Boolean.FALSE); @@ -2666,7 +2666,7 @@ public void setStyle(String value, Parameter parameter, String location, ObjectN } else if (value.equals(Parameter.StyleEnum.SPACEDELIMITED.toString())) { parameter.setStyle(Parameter.StyleEnum.SPACEDELIMITED); } else { - result.invalidType(location, "style", "string", obj); + result.invalidType(location, "style", "StyleEnum", obj); } } } diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/OpenAPIDeserializerTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/OpenAPIDeserializerTest.java index e610b552d9..5db21998ba 100644 --- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/OpenAPIDeserializerTest.java +++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/OpenAPIDeserializerTest.java @@ -1172,6 +1172,56 @@ public void testDeserializeByteString() { "attribute components.schemas.ByteString.default=`W.T.F?` is not of type `byte`")); } + @Test + public void testStyleInvalid() { + String json = + "{" + + " \"openapi\": \"3.0.0\"," + + " \"info\": {" + + " \"title\": \"realize\"," + + " \"version\": \"0.0.0\"" + + " }," + + " \"paths\": {" + + " \"/realize/{param}\": {" + + " \"post\": {" + + " \"parameters\": [" + + " {" + + " \"name\": \"param\"," + + " \"in\": \"path\"," + + "" + + " \"style\": \"DERP\"," + + " \"required\": true," + + "" + + " \"schema\": {" + + " \"type\": \"string\"," + + " \"nullable\": false," + + " \"minLength\": 1" + + " }" + + " }" + + " ]," + + " \"responses\": {" + + " \"200\": {" + + " \"description\": \"Success\"," + + " \"content\": {" + + " \"application/json\": {" + + " \"schema\": {" + + " \"type\": \"object\"" + + " }" + + " }" + + " }" + + " }" + + " }" + + " }" + + " }" + + " }" + + "}" + ; + OpenAPIV3Parser parser = new OpenAPIV3Parser(); + SwaggerParseResult result = parser.readContents(json, null, null); + assertTrue(result.getMessages().size() == 1); + assertEquals(result.getMessages().get(0), "attribute paths.'/realize/{param}'(post).parameters.[param].style is not of type `StyleEnum`"); + } + @Test public void testDeserializeWithMessages() { String yaml = "openapi: '3.0.0'\n" +