Skip to content

Commit cd72a25

Browse files
authored
Fix json schema test (#35597)
* fix json schema test * keep order validation for top-level * grammar
1 parent e8d4395 commit cd72a25

1 file changed

Lines changed: 35 additions & 18 deletions

File tree

sdks/java/core/src/test/java/org/apache/beam/sdk/schemas/JsonSchemaConversionTest.java

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
import static org.hamcrest.Matchers.containsString;
2323
import static org.junit.Assert.assertEquals;
2424
import static org.junit.Assert.assertThrows;
25+
import static org.junit.Assert.assertTrue;
2526

2627
import java.io.IOException;
2728
import java.io.InputStream;
29+
import java.util.Arrays;
2830
import java.util.stream.Collectors;
2931
import org.apache.beam.sdk.schemas.Schema.FieldType;
3032
import org.apache.beam.sdk.schemas.utils.JsonUtils;
33+
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Iterables;
3134
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.io.ByteStreams;
3235
import org.everit.json.schema.ValidationException;
3336
import org.json.JSONArray;
@@ -285,24 +288,38 @@ public void testArrayWithNestedRefsBeamSchema() throws IOException {
285288
String stringJsonSchema = new String(ByteStreams.toByteArray(inputStream), "UTF-8");
286289
Schema parsedSchema = JsonUtils.beamSchemaFromJsonSchema(stringJsonSchema);
287290

288-
assertThat(parsedSchema.getFieldNames(), containsInAnyOrder("vegetables"));
289-
assertThat(
290-
parsedSchema.getFields().stream().map(Schema.Field::getType).collect(Collectors.toList()),
291-
containsInAnyOrder(
292-
Schema.FieldType.array(
293-
Schema.FieldType.row(
294-
Schema.of(
295-
Schema.Field.of("veggieName", Schema.FieldType.STRING),
296-
Schema.Field.of("veggieLike", Schema.FieldType.BOOLEAN),
297-
Schema.Field.nullable(
298-
"origin",
299-
Schema.FieldType.row(
300-
Schema.of(
301-
Schema.Field.nullable("country", Schema.FieldType.STRING),
302-
Schema.Field.nullable("town", Schema.FieldType.STRING),
303-
Schema.Field.nullable(
304-
"region", Schema.FieldType.STRING)))))))
305-
.withNullable(true)));
291+
assertEquals("vegetables", Iterables.getOnlyElement(parsedSchema.getFieldNames()));
292+
Schema.Field field = parsedSchema.getField("vegetables");
293+
294+
// Top-level fields include only one nullable field, so ordering should be preserved on that
295+
// level.
296+
assertEquals(
297+
Arrays.asList("veggieName", "veggieLike", "origin"),
298+
field.getType().getCollectionElementType().getRowSchema().getFieldNames());
299+
// Inner schema contains multiple nullable fields, which can be out of order. Test the
300+
// remaining using
301+
// equivalency instead.
302+
assertTrue(
303+
field
304+
.getType()
305+
.equivalent(
306+
Schema.FieldType.array(
307+
Schema.FieldType.row(
308+
Schema.of(
309+
Schema.Field.of("veggieName", Schema.FieldType.STRING),
310+
Schema.Field.of("veggieLike", Schema.FieldType.BOOLEAN),
311+
Schema.Field.nullable(
312+
"origin",
313+
Schema.FieldType.row(
314+
Schema.of(
315+
Schema.Field.nullable(
316+
"country", Schema.FieldType.STRING),
317+
Schema.Field.nullable(
318+
"town", Schema.FieldType.STRING),
319+
Schema.Field.nullable(
320+
"region", Schema.FieldType.STRING)))))))
321+
.withNullable(true),
322+
Schema.EquivalenceNullablePolicy.SAME));
306323
}
307324
}
308325

0 commit comments

Comments
 (0)