Skip to content

Commit 9f0c402

Browse files
Reading from JSON Array data now works differently
1 parent e65e8a2 commit 9f0c402

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

src/main/java/io/frictionlessdata/tableschema/Table.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ private void writeCsv(Writer out, CSVFormat format, String[] sortedHeaders) {
461461
String[] headers = getHeaders();
462462
Map<Integer, Integer> mapping
463463
= TableSchemaUtil.createSchemaHeaderMapping(headers, sortedHeaders, dataSource.hasReliableHeaders());
464-
if (null != schema) {
464+
if ((null != schema)) {
465465
writeCSVData(mapping, schema, csvPrinter);
466466
} else {
467467
writeCSVData(mapping, csvPrinter);

src/main/java/io/frictionlessdata/tableschema/tabledatasource/JsonArrayTableDataSource.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,17 @@ public JsonArrayTableDataSource (String json){
4343

4444
@Override
4545
public boolean hasReliableHeaders() {
46-
return false;
46+
if (dataSource == null) {
47+
return false;
48+
}
49+
Iterator iter = dataSource.elements();
50+
if (!iter.hasNext()) {
51+
return false;
52+
}
53+
JsonNode first = (JsonNode) iter.next();
54+
if (first instanceof ObjectNode) {
55+
return false;
56+
} else return first instanceof ArrayNode;
4757
}
4858

4959

src/main/java/io/frictionlessdata/tableschema/util/TableSchemaUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static Map<Integer, Integer> createSchemaHeaderMapping(
2525
// - if no header row in CSV data (throw)
2626
// - with JSON Arrays of JSON objects as they will not have keys for null values (accept, but don't map)
2727
if (!mapping.containsKey(i)) {
28-
if (reliableHeaders) {
28+
if (reliableHeaders ) {
2929
throw new TableValidationException("Field '" + sortedHeaders[i] + "' not found in table headers or table has no headers.");
3030
} else {
3131
mapping.put(i, null);

src/test/java/io/frictionlessdata/tableschema/fk/ForeignKeyTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.frictionlessdata.tableschema.Table;
55
import io.frictionlessdata.tableschema.TestHelper;
66
import io.frictionlessdata.tableschema.exception.ForeignKeyException;
7+
import io.frictionlessdata.tableschema.exception.ValidationException;
78
import io.frictionlessdata.tableschema.schema.Schema;
89
import io.frictionlessdata.tableschema.util.JsonUtil;
910
import org.apache.commons.csv.CSVFormat;
@@ -57,6 +58,32 @@ public void testInvalidFkReference() throws Exception {
5758
fke.getMessage());
5859
}
5960

61+
@Test
62+
@DisplayName("data validation. Check Schema with ForeignKey against not matching data -> must throw")
63+
public void testInvalidFkReferenceFromSchema() throws Exception {
64+
File testDataDir = TestHelper.getTestDataDirectory();
65+
File source = TestHelper.getResourceFile("/fixtures/schema/population_schema_for_fk_check.json");
66+
67+
Schema schema = Schema.fromJson(source, true);
68+
// get path of test CSV file
69+
File file = new File("data/population_for_fk_check_invalid.csv");
70+
Table table = Table.fromSource(
71+
file,
72+
testDataDir,
73+
schema,
74+
CSVFormat.DEFAULT.builder().setHeader().get());
75+
ValidationException valE = assertThrows(ValidationException.class, ()
76+
-> schema.validate(table));
77+
Assertions.assertFalse(valE.getWrappedExceptions().isEmpty());
78+
Exception fke = valE.getWrappedExceptions().get(0);
79+
Assertions.assertInstanceOf(ForeignKeyException.class, fke);
80+
Assertions.assertEquals("Foreign key [check_year-> year] violation : expected: 2018 found: 2017",
81+
fke.getMessage());
82+
Assertions.assertEquals("Validation failed with ForeignKeyException: Foreign key [check_year-> year] violation : expected: 2018 found: 2017",
83+
valE.getMessage());
84+
85+
}
86+
6087
@Test
6188
@DisplayName("formal validation, both fk fields and reference fields are strings")
6289
public void testValidStringFields() throws ForeignKeyException {

0 commit comments

Comments
 (0)