From bb441cbda13a3f5849a41438d657717f3d680331 Mon Sep 17 00:00:00 2001 From: "mateusz.poliwoczak" Date: Thu, 14 Apr 2022 14:07:15 +0200 Subject: [PATCH 1/2] Enum values parsing problem test, DeserializationUtils converting enum (Yes/No/On/Off) values to boolean --- .../parser/util/DeserializationUtilsTest.java | 28 +++++++++++ .../src/test/resources/EnumYesNoOnOff.yaml | 46 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 modules/swagger-parser/src/test/java/io/swagger/parser/util/DeserializationUtilsTest.java create mode 100644 modules/swagger-parser/src/test/resources/EnumYesNoOnOff.yaml diff --git a/modules/swagger-parser/src/test/java/io/swagger/parser/util/DeserializationUtilsTest.java b/modules/swagger-parser/src/test/java/io/swagger/parser/util/DeserializationUtilsTest.java new file mode 100644 index 0000000000..b5bb02871f --- /dev/null +++ b/modules/swagger-parser/src/test/java/io/swagger/parser/util/DeserializationUtilsTest.java @@ -0,0 +1,28 @@ +package io.swagger.parser.util; + +import com.fasterxml.jackson.databind.JsonNode; +import org.testng.annotations.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; + +import static org.testng.AssertJUnit.assertFalse; + +public class DeserializationUtilsTest { + + @Test + public void testEnumValuesAreNotConvertedToBooleans() throws IOException { + String yaml = readFile("src/test/resources/EnumYesNoOnOff.yaml"); + + JsonNode jsonNode = DeserializationUtils.readYamlTree(yaml, null); + + jsonNode.findPath("EnumNoQuotes").get("enum").elements() + .forEachRemaining(element -> assertFalse(element.isBoolean())); + } + + private String readFile(String name) throws IOException { + return new String(Files.readAllBytes(new File(name).toPath()), StandardCharsets.UTF_8); + } +} diff --git a/modules/swagger-parser/src/test/resources/EnumYesNoOnOff.yaml b/modules/swagger-parser/src/test/resources/EnumYesNoOnOff.yaml new file mode 100644 index 0000000000..2596d549f8 --- /dev/null +++ b/modules/swagger-parser/src/test/resources/EnumYesNoOnOff.yaml @@ -0,0 +1,46 @@ +swagger: '2.0' +info: + title: Yes No On Off string test + version: 1.0.0 +paths: {} +definitions: + EnumNoQuotes: + type: string + enum: + - some_string + - y + - Y + - yes + - Yes + - YES + - n + - N + - no + - No + - NO + - on + - On + - ON + - off + - Off + - OFF + EnumWithQuotes: + type: string + enum: + - some_string + - 'y' + - 'Y' + - 'yes' + - 'Yes' + - 'YES' + - 'n' + - 'N' + - 'no' + - 'No' + - 'NO' + - 'on' + - 'On' + - 'ON' + - 'off' + - 'Off' + - 'OFF' \ No newline at end of file From 7570ba072a34d97c6c7d04845d0de57b1b456fa3 Mon Sep 17 00:00:00 2001 From: "mateusz.poliwoczak" Date: Fri, 15 Apr 2022 09:05:40 +0200 Subject: [PATCH 2/2] Updated tests with OAS3, added additional assertions for true/false strings. --- .../parser/util/DeserializationUtilsTest.java | 38 +++++++++++++-- ...esNoOnOff.yaml => EnumYesNoOnOffOAS2.yaml} | 0 .../test/resources/EnumYesNoOnOffOAS3.yaml | 47 +++++++++++++++++++ 3 files changed, 82 insertions(+), 3 deletions(-) rename modules/swagger-parser/src/test/resources/{EnumYesNoOnOff.yaml => EnumYesNoOnOffOAS2.yaml} (100%) create mode 100644 modules/swagger-parser/src/test/resources/EnumYesNoOnOffOAS3.yaml diff --git a/modules/swagger-parser/src/test/java/io/swagger/parser/util/DeserializationUtilsTest.java b/modules/swagger-parser/src/test/java/io/swagger/parser/util/DeserializationUtilsTest.java index b5bb02871f..a83428f5b4 100644 --- a/modules/swagger-parser/src/test/java/io/swagger/parser/util/DeserializationUtilsTest.java +++ b/modules/swagger-parser/src/test/java/io/swagger/parser/util/DeserializationUtilsTest.java @@ -9,19 +9,51 @@ import java.nio.file.Files; import static org.testng.AssertJUnit.assertFalse; +import static org.testng.AssertJUnit.assertNotSame; public class DeserializationUtilsTest { @Test - public void testEnumValuesAreNotConvertedToBooleans() throws IOException { - String yaml = readFile("src/test/resources/EnumYesNoOnOff.yaml"); + public void testEnumValuesAreNotConvertedToBooleansOAS2() throws IOException { + String yaml = readFile("src/test/resources/EnumYesNoOnOffOAS2.yaml"); JsonNode jsonNode = DeserializationUtils.readYamlTree(yaml, null); + jsonNode.findPath("EnumWithQuotes").get("enum").elements() + .forEachRemaining(element -> { + assertFalse(element.isBoolean()); + assertNotSame(element.textValue(), "true"); + assertNotSame(element.textValue(), "false"); + }); jsonNode.findPath("EnumNoQuotes").get("enum").elements() - .forEachRemaining(element -> assertFalse(element.isBoolean())); + .forEachRemaining(element -> { + assertFalse(element.isBoolean()); + assertNotSame(element.textValue(), "true"); + assertNotSame(element.textValue(), "false"); + }); } + @Test + public void testEnumValuesAreNotConvertedToBooleansOAS3() throws IOException { + String yaml = readFile("src/test/resources/EnumYesNoOnOffOAS3.yaml"); + + JsonNode jsonNode = DeserializationUtils.readYamlTree(yaml, null); + + jsonNode.findPath("EnumWithQuotes").get("enum").elements() + .forEachRemaining(element -> { + assertFalse(element.isBoolean()); + assertNotSame(element.textValue(), "true"); + assertNotSame(element.textValue(), "false"); + }); + jsonNode.findPath("EnumNoQuotes").get("enum").elements() + .forEachRemaining(element -> { + assertFalse(element.isBoolean()); + assertNotSame(element.textValue(), "true"); + assertNotSame(element.textValue(), "false"); + }); + } + + private String readFile(String name) throws IOException { return new String(Files.readAllBytes(new File(name).toPath()), StandardCharsets.UTF_8); } diff --git a/modules/swagger-parser/src/test/resources/EnumYesNoOnOff.yaml b/modules/swagger-parser/src/test/resources/EnumYesNoOnOffOAS2.yaml similarity index 100% rename from modules/swagger-parser/src/test/resources/EnumYesNoOnOff.yaml rename to modules/swagger-parser/src/test/resources/EnumYesNoOnOffOAS2.yaml diff --git a/modules/swagger-parser/src/test/resources/EnumYesNoOnOffOAS3.yaml b/modules/swagger-parser/src/test/resources/EnumYesNoOnOffOAS3.yaml new file mode 100644 index 0000000000..ce17825826 --- /dev/null +++ b/modules/swagger-parser/src/test/resources/EnumYesNoOnOffOAS3.yaml @@ -0,0 +1,47 @@ +openapi: 3.0.0 +info: + title: Yes No On Off string test + version: 1.0.0 +paths: {} +components: + schemas: + EnumNoQuotes: + type: string + enum: + - some_string + - y + - Y + - yes + - Yes + - YES + - n + - N + - no + - No + - NO + - on + - On + - ON + - off + - Off + - OFF + EnumWithQuotes: + type: string + enum: + - some_string + - 'y' + - 'Y' + - 'yes' + - 'Yes' + - 'YES' + - 'n' + - 'N' + - 'no' + - 'No' + - 'NO' + - 'on' + - 'On' + - 'ON' + - 'off' + - 'Off' + - 'OFF' \ No newline at end of file