Skip to content

Commit c0e8bb9

Browse files
authored
fix(test): Make serialization tests robust against key reordering (#4990)
1 parent 9f5bf1d commit c0e8bb9

File tree

2 files changed

+53
-32
lines changed

2 files changed

+53
-32
lines changed

modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/properties/PropertySerializationTest.java

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import io.swagger.v3.core.util.Json;
5+
import io.swagger.v3.core.util.JsonAssert;
56
import io.swagger.v3.oas.models.media.ArraySchema;
67
import io.swagger.v3.oas.models.media.BooleanSchema;
78
import io.swagger.v3.oas.models.media.DateSchema;
@@ -32,7 +33,7 @@ public void serializeBooleanSchema() throws IOException {
3233
final BooleanSchema p = new BooleanSchema()
3334
._default(true);
3435
final String json = "{\"type\":\"boolean\",\"default\":true}";
35-
assertEquals(m.writeValueAsString(p), json);
36+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
3637
}
3738

3839
@Test(description = "it should deserialize a BooleanSchema")
@@ -43,14 +44,14 @@ public void deserializeBooleanSchema() throws IOException {
4344
assertNull(p.getFormat());
4445
assertEquals(p.getClass(), BooleanSchema.class);
4546
assertEquals(((BooleanSchema) p).getDefault(), Boolean.FALSE);
46-
assertEquals(m.writeValueAsString(p), json);
47+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
4748
}
4849

4950
@Test(description = "it should serialize a DateProperty")
5051
public void serializeDateProperty() throws IOException {
5152
final DateSchema p = new DateSchema();
5253
final String json = "{\"type\":\"string\",\"format\":\"date\"}";
53-
assertEquals(m.writeValueAsString(p), json);
54+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
5455
}
5556

5657
@Test(description = "it should deserialize a DateProperty")
@@ -60,14 +61,14 @@ public void deserializeDateProperty() throws IOException {
6061
assertEquals(p.getType(), "string");
6162
assertEquals(p.getFormat(), "date");
6263
assertEquals(p.getClass(), DateSchema.class);
63-
assertEquals(m.writeValueAsString(p), json);
64+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
6465
}
6566

6667
@Test(description = "it should serialize a DateTimeProperty")
6768
public void serializeDateTimeProperty() throws IOException {
6869
final DateTimeSchema p = new DateTimeSchema();
6970
final String json = "{\"type\":\"string\",\"format\":\"date-time\"}";
70-
assertEquals(m.writeValueAsString(p), json);
71+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
7172
}
7273

7374
@Test(description = "it should deserialize a DateTimeProperty")
@@ -77,7 +78,7 @@ public void deserializeDateTimeProperty() throws IOException {
7778
assertEquals(p.getType(), "string");
7879
assertEquals(p.getFormat(), "date-time");
7980
assertEquals(p.getClass(), DateTimeSchema.class);
80-
assertEquals(m.writeValueAsString(p), json);
81+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
8182
}
8283

8384
@Test(description = "it should serialize a DoubleProperty")
@@ -86,7 +87,7 @@ public void serializeDoubleProperty() throws IOException {
8687
._default(new BigDecimal("3.14159"));
8788
p.format("double");
8889
final String json = "{\"type\":\"number\",\"format\":\"double\",\"default\":3.14159}";
89-
assertEquals(m.writeValueAsString(p), json);
90+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
9091
}
9192

9293
@Test(description = "it should deserialize a DoubleProperty")
@@ -96,7 +97,7 @@ public void deserializeDoubleProperty() throws IOException {
9697
assertEquals(p.getType(), "number");
9798
assertEquals(p.getFormat(), "double");
9899
assertEquals(p.getClass(), NumberSchema.class);
99-
assertEquals(m.writeValueAsString(p), json);
100+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
100101
}
101102

102103
@Test(description = "it should serialize a FloatProperty")
@@ -105,7 +106,7 @@ public void serializeFloatProperty() throws IOException {
105106
._default(new BigDecimal("1.2"));
106107
p.format("float");
107108
final String json = "{\"type\":\"number\",\"format\":\"float\",\"default\":1.2}";
108-
assertEquals(m.writeValueAsString(p), json);
109+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
109110
}
110111

111112
@Test(description = "it should deserialize a FloatProperty")
@@ -115,15 +116,15 @@ public void deserializeFloatProperty() throws IOException {
115116
assertEquals(p.getType(), "number");
116117
assertEquals(p.getFormat(), "float");
117118
assertEquals(p.getClass(), NumberSchema.class);
118-
assertEquals(m.writeValueAsString(p), json);
119+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
119120
}
120121

121122
@Test(description = "it should serialize an IntegerProperty")
122123
public void serializeIntegerProperty() throws IOException {
123124
final IntegerSchema p = new IntegerSchema()
124125
._default(32);
125126
final String json = "{\"type\":\"integer\",\"format\":\"int32\",\"default\":32}";
126-
assertEquals(m.writeValueAsString(p), json);
127+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
127128
}
128129

129130
@Test(description = "it should deserialize a IntegerProperty")
@@ -133,7 +134,7 @@ public void deserializeIntegerProperty() throws IOException {
133134
assertEquals(p.getType(), "integer");
134135
assertEquals(p.getFormat(), "int32");
135136
assertEquals(p.getClass(), IntegerSchema.class);
136-
assertEquals(m.writeValueAsString(p), json);
137+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
137138
}
138139

139140
@Test(description = "it should serialize a LongProperty")
@@ -142,7 +143,7 @@ public void serializeLongProperty() throws IOException {
142143
.format("int64")
143144
._default(8675309);
144145
final String json = "{\"type\":\"integer\",\"format\":\"int64\",\"default\":8675309}";
145-
assertEquals(m.writeValueAsString(p), json);
146+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
146147
}
147148

148149
@Test(description = "it should deserialize a LongProperty")
@@ -152,14 +153,14 @@ public void deserializeLongProperty() throws IOException {
152153
assertEquals(p.getType(), "integer");
153154
assertEquals(p.getFormat(), "int64");
154155
assertEquals(p.getClass(), IntegerSchema.class);
155-
assertEquals(m.writeValueAsString(p), json);
156+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
156157
}
157158

158159
@Test(description = "it should serialize a string MapProperty")
159160
public void serializeStringMapProperty() throws IOException {
160161
final Schema p = new MapSchema().additionalProperties(new StringSchema());
161162
final String json = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"string\"}}";
162-
assertEquals(m.writeValueAsString(p), json);
163+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
163164
}
164165

165166
@Test(description = "it should deserialize a string MapProperty")
@@ -168,14 +169,14 @@ public void deserializeStringMapProperty() throws IOException {
168169
final Schema p = m.readValue(json, Schema.class);
169170
assertEquals(p.getType(), "object");
170171
assertEquals(p.getClass(), MapSchema.class);
171-
assertEquals(m.writeValueAsString(p), json);
172+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
172173
}
173174

174175
@Test(description = "it should serialize a integer MapProperty")
175176
public void serializeIntegerMapProperty() throws IOException {
176177
final Schema p = new MapSchema().additionalProperties(new IntegerSchema());
177178
final String json = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int32\"}}";
178-
assertEquals(m.writeValueAsString(p), json);
179+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
179180
}
180181

181182
@Test(description = "it should deserialize a integer MapProperty")
@@ -184,14 +185,14 @@ public void deserializeIntegerMapProperty() throws IOException {
184185
final Schema p = m.readValue(json, Schema.class);
185186
assertEquals(p.getType(), "object");
186187
assertEquals(p.getClass(), MapSchema.class);
187-
assertEquals(m.writeValueAsString(p), json);
188+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
188189
}
189190

190191
@Test(description = "it should serialize a long MapProperty")
191192
public void serializeLongMapProperty() throws IOException {
192193
final Schema p = new MapSchema().additionalProperties(new IntegerSchema().format("int64"));
193194
final String json = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int64\"}}";
194-
assertEquals(m.writeValueAsString(p), json);
195+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
195196
}
196197

197198
@Test(description = "it should deserialize a long MapProperty")
@@ -200,30 +201,30 @@ public void deserializeLongMapProperty() throws IOException {
200201
final Schema p = m.readValue(json, Schema.class);
201202
assertEquals(p.getType(), "object");
202203
assertEquals(p.getClass(), MapSchema.class);
203-
assertEquals(m.writeValueAsString(p), json);
204+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
204205
}
205206

206207
@Test(description = "it should serialize a RefProperty")
207208
public void serializeRefProperty() throws IOException {
208209
final Schema p = new Schema().$ref("#/definitions/Dog");
209210
final String json = "{\"$ref\":\"#/definitions/Dog\"}";
210-
assertEquals(m.writeValueAsString(p), json);
211+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
211212
}
212213

213214
@Test(description = "it should deserialize a RefProperty")
214215
public void deserializeRefProperty() throws IOException {
215216
final String json = "{\"$ref\":\"#/definitions/Dog\"}";
216217
final Schema p = m.readValue(json, Schema.class);
217218
assertEquals(p.getClass(), Schema.class);
218-
assertEquals(m.writeValueAsString(p), json);
219+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
219220
}
220221

221222
@Test(description = "it should serialize a StringProperty")
222223
public void serializeStringProperty() throws IOException {
223224
final StringSchema p = new StringSchema()
224225
._default("Bob");
225226
final String json = "{\"type\":\"string\",\"default\":\"Bob\"}";
226-
assertEquals(m.writeValueAsString(p), json);
227+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
227228
}
228229

229230
@Test(description = "it should deserialize a StringProperty")
@@ -232,7 +233,7 @@ public void deserializeStringProperty() throws IOException {
232233
final Schema p = m.readValue(json, Schema.class);
233234
assertEquals(p.getType(), "string");
234235
assertEquals(p.getClass(), StringSchema.class);
235-
assertEquals(m.writeValueAsString(p), json);
236+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
236237
}
237238

238239
@Test(description = "it should serialize a StringProperty with enums")
@@ -243,7 +244,7 @@ public void serializeEnumStringProperty() throws IOException {
243244
this.add("b");
244245
}});
245246
final String json = "{\"type\":\"string\",\"enum\":[\"a\",\"b\"]}";
246-
assertEquals(m.writeValueAsString(p), json);
247+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
247248
}
248249

249250
@Test(description = "it should deserialize a StringProperty with enums")
@@ -255,7 +256,7 @@ public void deserializeEnumStringProperty() throws IOException {
255256
assertNotNull(_enum);
256257
assertEquals(_enum, Arrays.asList("a", "b"));
257258
assertEquals(p.getClass(), StringSchema.class);
258-
assertEquals(m.writeValueAsString(p), json);
259+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
259260
}
260261

261262
@Test(description = "it should deserialize an IntegerProperty with enums")
@@ -267,14 +268,14 @@ public void deserializeEnumIntegerProperty() throws IOException {
267268
assertNotNull(_enum);
268269
assertEquals(_enum, Arrays.asList(1, 2));
269270
assertEquals(p.getClass(), IntegerSchema.class);
270-
assertEquals(m.writeValueAsString(p), json);
271+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
271272
}
272273

273274
@Test(description = "it should serialize a string array property")
274275
public void serializeArrayStringProperty() throws IOException {
275276
final Schema p = new ArraySchema().items(new StringSchema());
276277
final String json = "{\"type\":\"array\",\"items\":{\"type\":\"string\"}}";
277-
assertEquals(m.writeValueAsString(p), json);
278+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
278279
}
279280

280281
@Test(description = "it should deserialize a string array property")
@@ -283,22 +284,22 @@ public void deserializeArrayStringProperty() throws IOException {
283284
final Schema p = m.readValue(json, Schema.class);
284285
assertEquals(p.getType(), "array");
285286
assertEquals(p.getClass(), ArraySchema.class);
286-
assertEquals(m.writeValueAsString(p), json);
287+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
287288
}
288289

289290
@Test(description = "it should serialize a string property with readOnly set")
290291
public void serializeReadOnlyStringProperty() throws IOException {
291292
final Schema p = new StringSchema().readOnly(true);
292293
final String json = "{\"type\":\"string\",\"readOnly\":true}";
293-
assertEquals(m.writeValueAsString(p), json);
294+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
294295
}
295296

296297
@Test(description = "it should serialize a string property with readOnly unset")
297298
public void deserializeNotReadOnlyStringProperty() throws IOException {
298299
final StringSchema p = new StringSchema();
299300
p.setReadOnly(false);
300301
final String json = "{\"type\":\"string\",\"readOnly\":false}";
301-
assertEquals(m.writeValueAsString(p), json);
302+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
302303
}
303304

304305
@Test(description = "it should serialize an object property with required set")
@@ -307,7 +308,7 @@ public void serializeObjectPropertyWithRequiredProperties() throws IOException {
307308
.addProperties("stringProperty", new StringSchema());
308309
p.required(Arrays.asList("stringProperty"));
309310
final String json = "{\"required\":[\"stringProperty\"],\"type\":\"object\",\"properties\":{\"stringProperty\":{\"type\":\"string\"}}}";
310-
assertEquals(m.writeValueAsString(p), json);
311+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
311312
}
312313

313314
@Test(description = "it should deserialize an object property with required set")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.swagger.v3.core.util;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import static org.testng.Assert.assertTrue;
6+
7+
public final class JsonAssert {
8+
private JsonAssert() {
9+
}
10+
11+
public static void assertJsonEquals(ObjectMapper mapper, String expectedJson, String actualJson) {
12+
try {
13+
JsonNode expectedNode = mapper.readTree(expectedJson);
14+
JsonNode actualNode = mapper.readTree(actualJson);
15+
assertTrue(expectedNode.equals(actualNode));
16+
} catch (Exception e) {
17+
throw new RuntimeException(e);
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)