diff --git a/modules/swagger-core/pom.xml b/modules/swagger-core/pom.xml index 5c64e4204f..2667290482 100644 --- a/modules/swagger-core/pom.xml +++ b/modules/swagger-core/pom.xml @@ -52,6 +52,16 @@ + + org.json + json + 20230227 + + + org.skyscreamer + jsonassert + 1.5.0 + jakarta.xml.bind jakarta.xml.bind-api diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/ByteConverterTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/ByteConverterTest.java index fabc821de3..d37c74c3e8 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/ByteConverterTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/ByteConverterTest.java @@ -8,6 +8,8 @@ import io.swagger.v3.oas.models.media.ByteArraySchema; import io.swagger.v3.oas.models.media.Schema; import org.testng.annotations.Test; +import org.json.JSONObject; +import org.skyscreamer.jsonassert.JSONAssert; import java.util.Map; @@ -42,14 +44,18 @@ public void testByteProperty() { Schema model = new Schema() .addProperties("byteProperty", new ByteArraySchema()); - assertEquals(Json.pretty(model), "{" + NEWLINE + + String json1 = "{" + NEWLINE + " \"properties\" : {" + NEWLINE + " \"byteProperty\" : {" + NEWLINE + " \"type\" : \"string\"," + NEWLINE + " \"format\" : \"byte\"" + NEWLINE + " }" + NEWLINE + " }" + NEWLINE + - "}"); + "}"; + String json2 = Json.pretty(model); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test @@ -73,7 +79,7 @@ public void testByteArray() { Schema model = new Schema() .addProperties("byteArray", new ArraySchema().items(new BinarySchema())); - assertEquals(Json.pretty(model), "{" + NEWLINE + + String json1 = "{" + NEWLINE + " \"properties\" : {" + NEWLINE + " \"byteArray\" : {" + NEWLINE + " \"type\" : \"array\"," + NEWLINE + @@ -83,7 +89,11 @@ public void testByteArray() { " }" + NEWLINE + " }" + NEWLINE + " }" + NEWLINE + - "}"); + "}"; + String json2 = Json.pretty(model); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test @@ -92,7 +102,7 @@ public void testReadOnlyByteArray() { .addProperties("byteArray", new ArraySchema().items(new BinarySchema()).readOnly(true)); - assertEquals(Json.pretty(model), "{" + NEWLINE + + String json1 = "{" + NEWLINE + " \"properties\" : {" + NEWLINE + " \"byteArray\" : {" + NEWLINE + " \"type\" : \"array\"," + NEWLINE + @@ -103,7 +113,11 @@ public void testReadOnlyByteArray() { " }" + NEWLINE + " }" + NEWLINE + " }" + NEWLINE + - "}"); + "}"; + String json2 = Json.pretty(model); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } class ByteConverterModel { diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/ModelConverterTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/ModelConverterTest.java index 13a0fa1220..8e92fc83c1 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/ModelConverterTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/ModelConverterTest.java @@ -132,24 +132,23 @@ public void serializeParameterizedType() { final Schema employee = (Schema) schemas.get("employee").getProperties().get("employee"); final Map props = employee.getProperties(); - final Iterator et = props.keySet().iterator(); - final Schema id = props.get(et.next()); + final Schema id = props.get("id"); assertTrue(id instanceof IntegerSchema); - final Schema firstName = props.get(et.next()); + final Schema firstName = props.get("firstName"); assertTrue(firstName instanceof StringSchema); - final Schema lastName = props.get(et.next()); + final Schema lastName = props.get("lastName"); assertTrue(lastName instanceof StringSchema); - final Schema department = props.get(et.next()); + final Schema department = props.get("department"); assertNotNull(department.get$ref()); - final Schema manager = props.get(et.next()); + final Schema manager = props.get("manager"); assertNotNull(manager.get$ref()); - final Schema team = props.get(et.next()); + final Schema team = props.get("team"); assertTrue(team instanceof ArraySchema); final ArraySchema ap = (ArraySchema) team; diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/NumericFormatTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/NumericFormatTest.java index c93dc2a7ae..75a75c2237 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/NumericFormatTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/NumericFormatTest.java @@ -14,14 +14,17 @@ import static io.swagger.v3.core.util.TestUtils.normalizeLineEnds; import static org.testng.Assert.assertEquals; +import org.json.JSONObject; +import org.skyscreamer.jsonassert.JSONAssert; + public class NumericFormatTest { @Test public void testFormatOfInteger() { final Map models = ModelConverters.getInstance().readAll(ModelWithIntegerFields.class); assertEquals(models.size(), 1); - String json = Json.pretty(models); - assertEquals(normalizeLineEnds(json), + String json1 = Json.pretty(models); + String json2 = "{\n" + " \"ModelWithIntegerFields\" : {\n" + " \"type\" : \"object\",\n" + @@ -33,7 +36,11 @@ public void testFormatOfInteger() { " }\n" + " }\n" + " }\n" + - "}"); + "}"; + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test @@ -41,8 +48,8 @@ public void testFormatOfDecimal() { final Map models = ModelConverters.getInstance().readAll(ModelWithDecimalFields.class); assertEquals(models.size(), 1); - String json = Json.pretty(models); - assertEquals(normalizeLineEnds(json), + String json1 = Json.pretty(models); + String json2 = "{\n" + " \"ModelWithDecimalFields\" : {\n" + " \"type\" : \"object\",\n" + @@ -55,7 +62,11 @@ public void testFormatOfDecimal() { " }\n" + " }\n" + " }\n" + - "}"); + "}"; + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test @@ -63,9 +74,9 @@ public void testFormatOfBigDecimal() { final Map models = ModelConverters.getInstance().readAll(ModelWithoutScientificFields.class); assertEquals(models.size(), 1); - String json = Json.pretty(models); + String json1 = Json.pretty(models); - assertEquals(normalizeLineEnds(json), + String json2 = "{\n" + " \"ModelWithoutScientificFields\" : {\n" + " \"type\" : \"object\",\n" + @@ -79,7 +90,11 @@ public void testFormatOfBigDecimal() { " }\n" + " }\n" + " }\n" + - "}"); + "}"; + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/deserialization/JsonDeserializationTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/deserialization/JsonDeserializationTest.java index b5375c982a..4b7555b6da 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/deserialization/JsonDeserializationTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/deserialization/JsonDeserializationTest.java @@ -33,6 +33,9 @@ import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; + public class JsonDeserializationTest { private final ObjectMapper m = Json.mapper(); @@ -490,40 +493,60 @@ public void testNullExampleAndValues() throws Exception { assertNull(oas.getComponents().getSchemas().get("UserStatus").getExample()); assertTrue(oas.getComponents().getSchemas().get("UserStatus").getExampleSetFlag()); - assertEquals(Yaml.pretty(oas), yamlNull); + + // Use Jackson's YAML mapper to parse both expected and actual YAML into maps + ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory()); + Map expectedMap = yamlMapper.readValue(yamlNull, Map.class); + Map actualMap = yamlMapper.readValue(Yaml.pretty(oas), Map.class); + assertEquals(expectedMap, actualMap); oas = Yaml.mapper().readValue(yamlMissing, OpenAPI.class); Yaml.prettyPrint(oas); assertNull(oas.getComponents().getSchemas().get("UserStatus").getExample()); assertFalse(oas.getComponents().getSchemas().get("UserStatus").getExampleSetFlag()); - assertEquals(Yaml.pretty(oas), yamlMissing); + + Map expectedMap2 = yamlMapper.readValue(yamlMissing, Map.class); + Map actualMap2 = yamlMapper.readValue(Yaml.pretty(oas), Map.class); + assertEquals(expectedMap2, actualMap2); oas = Yaml.mapper().readValue(yamlNotNull, OpenAPI.class); Yaml.prettyPrint(oas); assertNotNull(oas.getComponents().getSchemas().get("UserStatus").getExample()); assertTrue(oas.getComponents().getSchemas().get("UserStatus").getExampleSetFlag()); - assertEquals(Yaml.pretty(oas), yamlNotNull); + + Map expectedMap3 = yamlMapper.readValue(yamlNotNull, Map.class); + Map actualMap3 = yamlMapper.readValue(Yaml.pretty(oas), Map.class); + assertEquals(expectedMap3, actualMap3); oas = Yaml.mapper().readValue(yamlValueNull, OpenAPI.class); Yaml.prettyPrint(oas); Example ex = oas.getComponents().getExamples().get("UserStatus"); assertNull(ex.getValue()); assertTrue(ex.getValueSetFlag()); - assertEquals(Yaml.pretty(oas), yamlValueNull); + + Map expectedMap4 = yamlMapper.readValue(yamlValueNull, Map.class); + Map actualMap4 = yamlMapper.readValue(Yaml.pretty(oas), Map.class); + assertEquals(expectedMap4, actualMap4); oas = Yaml.mapper().readValue(yamlValueMissing, OpenAPI.class); Yaml.prettyPrint(oas); ex = oas.getComponents().getExamples().get("UserStatus"); assertNull(ex.getValue()); assertFalse(ex.getValueSetFlag()); - assertEquals(Yaml.pretty(oas), yamlValueMissing); + + Map expectedMap5 = yamlMapper.readValue(yamlValueMissing, Map.class); + Map actualMap5 = yamlMapper.readValue(Yaml.pretty(oas), Map.class); + assertEquals(expectedMap5, actualMap5); oas = Yaml.mapper().readValue(yamlValueNotNull, OpenAPI.class); Yaml.prettyPrint(oas); ex = oas.getComponents().getExamples().get("UserStatus"); assertNotNull(ex.getValue()); assertTrue(ex.getValueSetFlag()); - assertEquals(Yaml.pretty(oas), yamlValueNotNull); + + Map expectedMap6 = yamlMapper.readValue(yamlValueNotNull, Map.class); + Map actualMap6 = yamlMapper.readValue(Yaml.pretty(oas), Map.class); + assertEquals(expectedMap6, actualMap6); } @Test diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/deserialization/properties/MapPropertyDeserializerTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/deserialization/properties/MapPropertyDeserializerTest.java index 46192ff445..7f9e0e165b 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/deserialization/properties/MapPropertyDeserializerTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/deserialization/properties/MapPropertyDeserializerTest.java @@ -10,6 +10,8 @@ import io.swagger.v3.oas.models.responses.ApiResponse; import org.testng.Assert; import org.testng.annotations.Test; +import org.json.JSONObject; +import org.skyscreamer.jsonassert.JSONAssert; import static io.swagger.v3.core.util.TestUtils.normalizeLineEnds; import static org.testng.Assert.assertEquals; @@ -167,13 +169,17 @@ public void testBooleanAdditionalPropertiesSerialization() throws Exception { Schema responseSchema = response.getContent().get("*/*").getSchema(); Schema schema = new ObjectSchema().additionalProperties(true); - assertEquals(normalizeLineEnds(Json.pretty(schema)), "{\n" + + String json1 = "{\n" + " \"type\" : \"object\",\n" + " \"additionalProperties\" : true\n" + - "}"); + "}"; + String json2 = normalizeLineEnds(Json.pretty(schema)); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); schema = new ObjectSchema().additionalProperties(responseSchema); - assertEquals(normalizeLineEnds(Json.pretty(schema)), "{\n" + + String json3 = "{\n" + " \"type\" : \"object\",\n" + " \"additionalProperties\" : {\n" + " \"type\" : \"object\",\n" + @@ -183,7 +189,11 @@ public void testBooleanAdditionalPropertiesSerialization() throws Exception { " },\n" + " \"x-foo\" : \"vendor x\"\n" + " }\n" + - "}"); + "}"; + String json4 = normalizeLineEnds(Json.pretty(schema)); + JSONObject jsonObj3 = new JSONObject(json3); + JSONObject jsonObj4 = new JSONObject(json4); + JSONAssert.assertEquals(jsonObj3, jsonObj4, true); } @Test(description = "vendor extensions should be included with object type") diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/JsonPropertyTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/JsonPropertyTest.java index 2ff3817d5d..70516448c7 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/JsonPropertyTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/JsonPropertyTest.java @@ -6,6 +6,14 @@ import io.swagger.v3.core.resolving.resources.User2169; import org.testng.annotations.Test; +import static org.testng.Assert.assertEquals; +import java.util.Map; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; + public class JsonPropertyTest { @Test(description = "test ticket 2169") @@ -128,9 +136,9 @@ public void testTicket2169() { } @Test(description = "test ticket 2845") - public void testTicket2845() { + public void testTicket2845() throws Exception { - SerializationMatchers.assertEqualsToYaml(ModelConverters.getInstance().readAll(Ticket2845Holder.class), "Ticket2845Child:\n" + + String expectedYaml = "Ticket2845Child:\n" + " type: object\n" + " properties:\n" + " bar:\n" + @@ -141,7 +149,15 @@ public void testTicket2845() { " type: object\n" + " properties:\n" + " child:\n" + - " $ref: '#/components/schemas/Ticket2845Child'"); + " $ref: '#/components/schemas/Ticket2845Child'"; + + String actualYaml = new ObjectMapper(new YAMLFactory()) + .writeValueAsString(ModelConverters.getInstance().readAll(Ticket2845Holder.class)); + + Set expectedSet = new HashSet<>(Arrays.asList(expectedYaml.split("\n"))); + Set actualSet = new HashSet<>(Arrays.asList(actualYaml.split("\n"))); + expectedSet.remove(null); + assertEquals(expectedSet, actualSet); /* TODO: Test demonstrating annotation not being resolved when class is used/refernces elsewhere with different annotations @@ -150,9 +166,7 @@ and referenced (in the same or different class) with no or different @JsonIgnor The possible solutions are either resolve into different unrelated schemas or resolve inline (see https://github.com/swagger-api/swagger-core/issues/3366 and other related tickets) */ - SerializationMatchers.assertEqualsToYaml( - ModelConverters.getInstance().readAll(Ticket2845HolderNoAnnotationNotWorking.class), - "Ticket2845Child:\n" + + String expectedYaml2 = "Ticket2845Child:\n" + " type: object\n" + " properties:\n" + " foo:\n" + @@ -167,7 +181,14 @@ and referenced (in the same or different class) with no or different @JsonIgnor " child:\n" + " $ref: '#/components/schemas/Ticket2845Child'\n" + " childNoAnnotation:\n" + - " $ref: '#/components/schemas/Ticket2845Child'"); + " $ref: '#/components/schemas/Ticket2845Child'"; + + String actualYaml2 = new ObjectMapper(new YAMLFactory()) + .writeValueAsString(ModelConverters.getInstance().readAll(Ticket2845HolderNoAnnotationNotWorking.class)); + Set expectedSet2 = new HashSet<>(Arrays.asList(expectedYaml2.split("\n"))); + Set actualSet2 = new HashSet<>(Arrays.asList(actualYaml2.split("\n"))); + expectedSet2.remove(null); + assertEquals(expectedSet2, actualSet2); } static class Ticket2845Parent { diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/JsonSerializationTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/JsonSerializationTest.java index 29163d0cca..4769e30c1f 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/JsonSerializationTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/JsonSerializationTest.java @@ -17,6 +17,8 @@ import io.swagger.v3.oas.models.servers.Server; import org.testng.annotations.Test; import org.yaml.snakeyaml.LoaderOptions; +import org.json.JSONObject; +import org.skyscreamer.jsonassert.JSONAssert; import java.util.HashMap; import java.util.Map; @@ -68,7 +70,11 @@ public void testExtensionObjectWithProperties() throws Exception { swagger.addExtension("x-extension-with-properties", extensionObjectProps); String swaggerJson = Json.mapper().writeValueAsString(swagger); - assertEquals(swaggerJson, "{\"openapi\":\"3.0.1\",\"x-extension-with-properties\":{\"x-foo-bar\":\"foo bar\",\"x-bar-foo\":null}}"); + String json2 = "{\"openapi\":\"3.0.1\",\"x-extension-with-properties\":{\"x-foo-bar\":\"foo bar\",\"x-bar-foo\":null}}"; + JSONObject jsonObj1 = new JSONObject(swaggerJson); + JSONObject jsonObj2 = new JSONObject(json2); + + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/ModelSerializerTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/ModelSerializerTest.java index b13386269c..7f1ebddab4 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/ModelSerializerTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/ModelSerializerTest.java @@ -17,6 +17,8 @@ import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.media.StringSchema; import org.testng.annotations.Test; +import org.json.JSONObject; +import org.skyscreamer.jsonassert.JSONAssert; import java.io.IOException; import java.math.BigDecimal; @@ -110,15 +112,22 @@ public void deserializeModel() throws IOException { public void serializeArrayModel() throws IOException { final ArraySchema model = new ArraySchema(); model.setItems(new Schema().$ref("Pet")); - assertEquals(m.writeValueAsString(model), "{\"type\":\"array\",\"items\":{\"$ref\":\"#/components/schemas/Pet\"}}"); + String json1 = m.writeValueAsString(model); + String json2 = "{\"type\":\"array\",\"items\":{\"$ref\":\"#/components/schemas/Pet\"}}"; + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize an array model") public void deserializeArrayModel() throws IOException { - final String json = "{\"type\":\"array\",\"items\":{\"$ref\":\"#/definitions/Pet\"}}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"array\",\"items\":{\"$ref\":\"#/definitions/Pet\"}}"; + final Schema p = m.readValue(json1, Schema.class); + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); assertTrue(p instanceof ArraySchema); - assertEquals(m.writeValueAsString(p), json); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should not create an xml object for $ref") diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/OpenAPI3_1SerializationTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/OpenAPI3_1SerializationTest.java index d0da2377d7..601c3035ab 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/OpenAPI3_1SerializationTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/OpenAPI3_1SerializationTest.java @@ -31,6 +31,14 @@ import io.swagger.v3.oas.models.security.SecurityScheme; import org.testng.annotations.Test; import org.yaml.snakeyaml.LoaderOptions; +import org.json.JSONObject; +import org.skyscreamer.jsonassert.JSONAssert; +import java.util.Map; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; @@ -1392,45 +1400,65 @@ public void testBooleanSchemaSerialization() { System.out.println("--------- root ----------"); Json31.prettyPrint(openAPI); - assertEquals(Json31.pretty(openAPI), withJacksonSystemLineSeparator("{\n" + + String json1 = withJacksonSystemLineSeparator("{\n" + " \"openapi\" : \"3.1.0\",\n" + " \"components\" : {\n" + " \"schemas\" : {\n" + " \"test\" : true\n" + " }\n" + " }\n" + - "}")); + "}"); + String json2 = Json31.pretty(openAPI); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); + System.out.println("--------- schema ----------"); Json31.prettyPrint(openAPI.getComponents().getSchemas().get("test")); assertEquals(Json31.pretty(openAPI.getComponents().getSchemas().get("test")), "true"); System.out.println("--------- root YAML----------"); Yaml31.prettyPrint(openAPI); - assertEquals(Yaml31.pretty(openAPI), "openapi: 3.1.0\n" + + String expectedYaml = "openapi: 3.1.0\n" + "components:\n" + " schemas:\n" + - " test: true\n"); + " test: true\n"; + String actualYaml = Yaml31.pretty(openAPI); + Set expectedSet = new HashSet<>(Arrays.asList(expectedYaml.split("\n"))); + Set actualSet = new HashSet<>(Arrays.asList(actualYaml.split("\n"))); + assertEquals(expectedSet, actualSet); + System.out.println("--------- schema YAML ----------"); Yaml31.prettyPrint(openAPI.getComponents().getSchemas().get("test")); assertEquals(Yaml31.pretty(openAPI.getComponents().getSchemas().get("test")), "true\n"); System.out.println("--------- root 3.0 ----------"); Json.prettyPrint(openAPI); - assertEquals(Json.pretty(openAPI), withJacksonSystemLineSeparator("{\n" + + String json5 = withJacksonSystemLineSeparator("{\n" + " \"openapi\" : \"3.1.0\",\n" + " \"components\" : {\n" + " \"schemas\" : {\n" + " \"test\" : { }\n" + " }\n" + " }\n" + - "}")); + "}"); + String json6 = Json.pretty(openAPI); + JSONObject jsonObj5 = new JSONObject(json5); + JSONObject jsonObj6 = new JSONObject(json6); + JSONAssert.assertEquals(jsonObj5, jsonObj6, true); + System.out.println("--------- schema 3.0 ----------"); Json.prettyPrint(openAPI.getComponents().getSchemas().get("test")); assertEquals(Json.pretty(openAPI.getComponents().getSchemas().get("test")), "{ }"); System.out.println("--------- root YAML 3.0 ----------"); Yaml.prettyPrint(openAPI); - assertEquals(Yaml.pretty(openAPI), "openapi: 3.1.0\n" + + String expectedYaml2 = "openapi: 3.1.0\n" + "components:\n" + " schemas:\n" + - " test: {}\n"); + " test: {}\n"; + String actualYaml2 = Yaml.pretty(openAPI); + Set expectedSet2 = new HashSet<>(Arrays.asList(expectedYaml2.split("\n"))); + Set actualSet2 = new HashSet<>(Arrays.asList(actualYaml2.split("\n"))); + assertEquals(expectedSet2, actualSet2); + System.out.println("--------- schema YAML 3.0 ----------"); Yaml.prettyPrint(openAPI.getComponents().getSchemas().get("test")); assertEquals(Yaml.pretty(openAPI.getComponents().getSchemas().get("test")), "{}\n"); @@ -1459,20 +1487,28 @@ public void testBooleanAdditionalPropertiesSerialization() throws Exception{ OpenAPI openAPI = Json31.mapper().readValue(expectedJson, OpenAPI.class); String ser = Json31.pretty(openAPI); - assertEquals(ser, withJacksonSystemLineSeparator(expectedJson)); + JSONObject jsonObj1 = new JSONObject(withJacksonSystemLineSeparator(expectedJson)); + JSONObject jsonObj2 = new JSONObject(ser); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties())); openAPI = Json.mapper().readValue(expectedJson, OpenAPI.class); ser = Json.pretty(openAPI); - assertEquals(ser, withJacksonSystemLineSeparator(expectedJson)); + JSONObject jsonObj3 = new JSONObject(withJacksonSystemLineSeparator(expectedJson)); + JSONObject jsonObj4 = new JSONObject(ser); + JSONAssert.assertEquals(jsonObj3, jsonObj4, true); assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties())); openAPI = Yaml31.mapper().readValue(expectedYaml, OpenAPI.class); ser = Yaml31.pretty(openAPI); - assertEquals(ser, expectedYaml); + Set expectedSet5 = new HashSet<>(Arrays.asList(expectedYaml.split("\n"))); + Set actualSet5 = new HashSet<>(Arrays.asList(ser.split("\n"))); + assertEquals(expectedSet5, actualSet5); assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties())); openAPI = Yaml.mapper().readValue(expectedYaml, OpenAPI.class); ser = Yaml.pretty(openAPI); - assertEquals(ser, expectedYaml); + Set expectedSet6 = new HashSet<>(Arrays.asList(expectedYaml.split("\n"))); + Set actualSet6 = new HashSet<>(Arrays.asList(ser.split("\n"))); + assertEquals(expectedSet6, actualSet6); assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties())); expectedJson = "{\n" + @@ -1496,20 +1532,28 @@ public void testBooleanAdditionalPropertiesSerialization() throws Exception{ openAPI = Json31.mapper().readValue(expectedJson, OpenAPI.class); ser = Json31.pretty(openAPI); - assertEquals(ser, withJacksonSystemLineSeparator(expectedJson)); + JSONObject jsonObj5 = new JSONObject(withJacksonSystemLineSeparator(expectedJson)); + JSONObject jsonObj6 = new JSONObject(ser); + JSONAssert.assertEquals(jsonObj5, jsonObj6, true); assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties())); openAPI = Json.mapper().readValue(expectedJson, OpenAPI.class); ser = Json.pretty(openAPI); - assertEquals(ser, withJacksonSystemLineSeparator(expectedJson)); + JSONObject jsonObj7 = new JSONObject(withJacksonSystemLineSeparator(expectedJson)); + JSONObject jsonObj8 = new JSONObject(ser); + JSONAssert.assertEquals(jsonObj7, jsonObj8, true); assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties())); openAPI = Yaml31.mapper().readValue(expectedYaml, OpenAPI.class); ser = Yaml31.pretty(openAPI); - assertEquals(ser, expectedYaml); + Set expectedSet7 = new HashSet<>(Arrays.asList(expectedYaml.split("\n"))); + Set actualSet7 = new HashSet<>(Arrays.asList(ser.split("\n"))); + assertEquals(expectedSet7, actualSet7); assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties())); openAPI = Yaml.mapper().readValue(expectedYaml, OpenAPI.class); ser = Yaml.pretty(openAPI); - assertEquals(ser, expectedYaml); + Set expectedSet8 = new HashSet<>(Arrays.asList(expectedYaml.split("\n"))); + Set actualSet8 = new HashSet<>(Arrays.asList(ser.split("\n"))); + assertEquals(expectedSet8, actualSet8); assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties())); } diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/properties/PropertySerializationTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/properties/PropertySerializationTest.java index 719b936be1..70c195ea1e 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/properties/PropertySerializationTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/properties/PropertySerializationTest.java @@ -13,6 +13,8 @@ import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.media.StringSchema; import org.testng.annotations.Test; +import org.json.JSONObject; +import org.skyscreamer.jsonassert.JSONAssert; import java.io.IOException; import java.math.BigDecimal; @@ -66,18 +68,24 @@ public void deserializeDateProperty() throws IOException { @Test(description = "it should serialize a DateTimeProperty") public void serializeDateTimeProperty() throws IOException { final DateTimeSchema p = new DateTimeSchema(); - final String json = "{\"type\":\"string\",\"format\":\"date-time\"}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"string\",\"format\":\"date-time\"}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize a DateTimeProperty") public void deserializeDateTimeProperty() throws IOException { - final String json = "{\"type\":\"string\",\"format\":\"date-time\"}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"string\",\"format\":\"date-time\"}"; + final Schema p = m.readValue(json1, Schema.class); assertEquals(p.getType(), "string"); assertEquals(p.getFormat(), "date-time"); assertEquals(p.getClass(), DateTimeSchema.class); - assertEquals(m.writeValueAsString(p), json); + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize a DoubleProperty") @@ -85,18 +93,24 @@ public void serializeDoubleProperty() throws IOException { final NumberSchema p = new NumberSchema() ._default(new BigDecimal("3.14159")); p.format("double"); - final String json = "{\"type\":\"number\",\"format\":\"double\",\"default\":3.14159}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"number\",\"format\":\"double\",\"default\":3.14159}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize a DoubleProperty") public void deserializeDoubleProperty() throws IOException { - final String json = "{\"type\":\"number\",\"format\":\"double\"}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"number\",\"format\":\"double\"}"; + final Schema p = m.readValue(json1, Schema.class); assertEquals(p.getType(), "number"); assertEquals(p.getFormat(), "double"); assertEquals(p.getClass(), NumberSchema.class); - assertEquals(m.writeValueAsString(p), json); + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize a FloatProperty") @@ -104,36 +118,48 @@ public void serializeFloatProperty() throws IOException { final NumberSchema p = new NumberSchema() ._default(new BigDecimal("1.2")); p.format("float"); - final String json = "{\"type\":\"number\",\"format\":\"float\",\"default\":1.2}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"number\",\"format\":\"float\",\"default\":1.2}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize a FloatProperty") public void deserializeFloatProperty() throws IOException { - final String json = "{\"type\":\"number\",\"format\":\"float\"}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"number\",\"format\":\"float\"}"; + final Schema p = m.readValue(json1, Schema.class); assertEquals(p.getType(), "number"); assertEquals(p.getFormat(), "float"); assertEquals(p.getClass(), NumberSchema.class); - assertEquals(m.writeValueAsString(p), json); + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize an IntegerProperty") public void serializeIntegerProperty() throws IOException { final IntegerSchema p = new IntegerSchema() ._default(32); - final String json = "{\"type\":\"integer\",\"format\":\"int32\",\"default\":32}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"integer\",\"format\":\"int32\",\"default\":32}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize a IntegerProperty") public void deserializeIntegerProperty() throws IOException { - final String json = "{\"type\":\"integer\",\"format\":\"int32\"}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"integer\",\"format\":\"int32\"}"; + final Schema p = m.readValue(json1, Schema.class); assertEquals(p.getType(), "integer"); assertEquals(p.getFormat(), "int32"); assertEquals(p.getClass(), IntegerSchema.class); - assertEquals(m.writeValueAsString(p), json); + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize a LongProperty") @@ -141,18 +167,24 @@ public void serializeLongProperty() throws IOException { final IntegerSchema p = new IntegerSchema() .format("int64") ._default(8675309); - final String json = "{\"type\":\"integer\",\"format\":\"int64\",\"default\":8675309}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"integer\",\"format\":\"int64\",\"default\":8675309}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize a LongProperty") public void deserializeLongProperty() throws IOException { - final String json = "{\"type\":\"integer\",\"format\":\"int64\"}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"integer\",\"format\":\"int64\"}"; + final Schema p = m.readValue(json1, Schema.class); assertEquals(p.getType(), "integer"); assertEquals(p.getFormat(), "int64"); assertEquals(p.getClass(), IntegerSchema.class); - assertEquals(m.writeValueAsString(p), json); + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize a string MapProperty") @@ -174,33 +206,45 @@ public void deserializeStringMapProperty() throws IOException { @Test(description = "it should serialize a integer MapProperty") public void serializeIntegerMapProperty() throws IOException { final Schema p = new MapSchema().additionalProperties(new IntegerSchema()); - final String json = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int32\"}}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int32\"}}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize a integer MapProperty") public void deserializeIntegerMapProperty() throws IOException { - final String json = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int32\"}}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int32\"}}"; + final Schema p = m.readValue(json1, Schema.class); assertEquals(p.getType(), "object"); assertEquals(p.getClass(), MapSchema.class); - assertEquals(m.writeValueAsString(p), json); + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize a long MapProperty") public void serializeLongMapProperty() throws IOException { final Schema p = new MapSchema().additionalProperties(new IntegerSchema().format("int64")); - final String json = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int64\"}}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int64\"}}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize a long MapProperty") public void deserializeLongMapProperty() throws IOException { - final String json = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int64\"}}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int64\"}}"; + final Schema p = m.readValue(json1, Schema.class); assertEquals(p.getType(), "object"); assertEquals(p.getClass(), MapSchema.class); - assertEquals(m.writeValueAsString(p), json); + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize a RefProperty") @@ -260,45 +304,60 @@ public void deserializeEnumStringProperty() throws IOException { @Test(description = "it should deserialize an IntegerProperty with enums") public void deserializeEnumIntegerProperty() throws IOException { - final String json = "{\"type\":\"integer\",\"format\":\"int32\",\"enum\":[1,2]}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"integer\",\"format\":\"int32\",\"enum\":[1,2]}"; + final Schema p = m.readValue(json1, Schema.class); assertEquals(p.getType(), "integer"); List _enum = ((IntegerSchema) p).getEnum(); assertNotNull(_enum); assertEquals(_enum, Arrays.asList(1, 2)); assertEquals(p.getClass(), IntegerSchema.class); - assertEquals(m.writeValueAsString(p), json); + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize a string array property") public void serializeArrayStringProperty() throws IOException { final Schema p = new ArraySchema().items(new StringSchema()); - final String json = "{\"type\":\"array\",\"items\":{\"type\":\"string\"}}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"array\",\"items\":{\"type\":\"string\"}}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize a string array property") public void deserializeArrayStringProperty() throws IOException { - final String json = "{\"type\":\"array\",\"items\":{\"type\":\"string\"}}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"array\",\"items\":{\"type\":\"string\"}}"; + final Schema p = m.readValue(json1, Schema.class); + String json2 = m.writeValueAsString(p); assertEquals(p.getType(), "array"); assertEquals(p.getClass(), ArraySchema.class); - assertEquals(m.writeValueAsString(p), json); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize a string property with readOnly set") public void serializeReadOnlyStringProperty() throws IOException { final Schema p = new StringSchema().readOnly(true); - final String json = "{\"type\":\"string\",\"readOnly\":true}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"string\",\"readOnly\":true}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize a string property with readOnly unset") public void deserializeNotReadOnlyStringProperty() throws IOException { final StringSchema p = new StringSchema(); p.setReadOnly(false); - final String json = "{\"type\":\"string\",\"readOnly\":false}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"string\",\"readOnly\":false}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize an object property with required set") @@ -306,8 +365,11 @@ public void serializeObjectPropertyWithRequiredProperties() throws IOException { final Schema p = new ObjectSchema() .addProperties("stringProperty", new StringSchema()); p.required(Arrays.asList("stringProperty")); - final String json = "{\"required\":[\"stringProperty\"],\"type\":\"object\",\"properties\":{\"stringProperty\":{\"type\":\"string\"}}}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"required\":[\"stringProperty\"],\"type\":\"object\",\"properties\":{\"stringProperty\":{\"type\":\"string\"}}}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize an object property with required set")