From a4ba9fb27a5bce103ea15a90d750d7e529163556 Mon Sep 17 00:00:00 2001 From: Mikhail Kopylov Date: Sat, 16 May 2020 17:21:33 +0300 Subject: [PATCH] Fix for issue #1375 Empty and blank query parameter examples were parsed to null. Now they are parsed to an empty and blank string respectively. --- .../v3/parser/util/OpenAPIDeserializer.java | 5 +---- .../v3/parser/test/OpenAPIV3ParserTest.java | 20 +++++++++++++++++++ .../parser/util/OpenAPIDeserializerTest.java | 2 +- .../test/resources/blankQueryParameter.yaml | 14 +++++++++++++ .../test/resources/emptyQueryParameter.yaml | 14 +++++++++++++ 5 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 modules/swagger-parser-v3/src/test/resources/blankQueryParameter.yaml create mode 100644 modules/swagger-parser-v3/src/test/resources/emptyQueryParameter.yaml 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 32e71d9ad5..59175dffcf 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 @@ -1727,10 +1727,7 @@ public Object getAnyExample(String nodeKey,ObjectNode node, String location, Par JsonNode example = node.get(nodeKey); if (example != null) { if (example.getNodeType().equals(JsonNodeType.STRING)) { - String value = getString(nodeKey, node, false, location, result); - if (StringUtils.isNotBlank(value)) { - return value; - } + return getString(nodeKey, node, false, location, result); } if (example.getNodeType().equals(JsonNodeType.NUMBER)) { Integer integerExample = getInteger(nodeKey, node, false, location, result); if (integerExample != null) { diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java index 6d593127e1..65f3ff5281 100644 --- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java +++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java @@ -2326,6 +2326,26 @@ public void testIssue1335() { assertNotNull(result.getOpenAPI().getComponents().getExamples().get("ex1")); } + @Test + public void testEmptyQueryParameterExample() { + final ParseOptions options = new ParseOptions(); + options.setResolve(true); + + SwaggerParseResult result = new OpenAPIV3Parser() + .readLocation("src/test/resources/emptyQueryParameter.yaml", null, options); + assertEquals("", result.getOpenAPI().getPaths().get("/foo").getGet().getParameters().get(0).getExample()); + } + + @Test + public void testBlankQueryParameterExample() { + final ParseOptions options = new ParseOptions(); + options.setResolve(true); + + SwaggerParseResult result = new OpenAPIV3Parser() + .readLocation("src/test/resources/blankQueryParameter.yaml", null, options); + assertEquals(" ", result.getOpenAPI().getPaths().get("/foo").getGet().getParameters().get(0).getExample()); + } + @Test public void testRegressionIssue1236() { final ParseOptions options = new ParseOptions(); 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 5a00d6040e..e610b552d9 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 @@ -2384,7 +2384,7 @@ public void readContentObject(JsonNode rootNode) throws Exception { Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("text/plain").getExamples().get("list").getSummary(),"List of names"); Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("text/plain").getExamples().get("list").getValue(),"Bob,Diane,Mary,Bill"); Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("text/plain").getExamples().get("empty").getSummary(),"Empty"); - Assert.assertNull(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("text/plain").getExamples().get("empty").getValue()); + Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("text/plain").getExamples().get("empty").getValue(),""); PathItem petEndpoint = paths.get("/pet"); Assert.assertNotNull(petEndpoint.getPut()); diff --git a/modules/swagger-parser-v3/src/test/resources/blankQueryParameter.yaml b/modules/swagger-parser-v3/src/test/resources/blankQueryParameter.yaml new file mode 100644 index 0000000000..b0b5cfb2cc --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/blankQueryParameter.yaml @@ -0,0 +1,14 @@ +openapi: 3.0.2 +info: + title: Example references + version: 1.0.0 +paths: + /foo: + get: + parameters: + - name: paramName + in: query + example: " " + responses: + '200': + description: success diff --git a/modules/swagger-parser-v3/src/test/resources/emptyQueryParameter.yaml b/modules/swagger-parser-v3/src/test/resources/emptyQueryParameter.yaml new file mode 100644 index 0000000000..4d9b06c86f --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/emptyQueryParameter.yaml @@ -0,0 +1,14 @@ +openapi: 3.0.2 +info: + title: Example references + version: 1.0.0 +paths: + /foo: + get: + parameters: + - name: paramName + in: query + example: "" + responses: + '200': + description: success