Skip to content

Commit 885faf2

Browse files
Fixed bug with non-tabular resources in ZIP files; added test
1 parent 1094c66 commit 885faf2

File tree

5 files changed

+40
-16
lines changed

5 files changed

+40
-16
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>io.frictionlessdata</groupId>
66
<artifactId>datapackage-java</artifactId>
7-
<version>0.6.10-SNAPSHOT</version>
7+
<version>0.6.11-SNAPSHOT</version>
88
<packaging>jar</packaging>
99
<issueManagement>
1010
<url>https://github.com/frictionlessdata/datapackage-java/issues</url>
@@ -22,7 +22,7 @@
2222
<java.version>8</java.version>
2323
<maven.compiler.source>${java.version}</maven.compiler.source>
2424
<maven.compiler.target>${java.version}</maven.compiler.target>
25-
<tableschema-java-version>0.6.10</tableschema-java-version>
25+
<tableschema-java-version>0.6.11</tableschema-java-version>
2626
<hamcrest.version>1.3</hamcrest.version>
2727
<junit.version>5.9.1</junit.version>
2828
<slf4j-simple.version>2.0.5</slf4j-simple.version>

src/main/java/io/frictionlessdata/datapackage/Package.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -666,11 +666,11 @@ protected ObjectNode getJsonNode(){
666666
Set<String> datafileNames = resource.getDatafileNamesForWriting();
667667
Set<String> outPaths = datafileNames.stream().map((r) -> r+"."+resource.getSerializationFormat()).collect(Collectors.toSet());
668668
if (outPaths.size() == 1) {
669-
obj.put("path", outPaths.iterator().next());
669+
obj.put(JSON_KEY_PATH, outPaths.iterator().next());
670670
} else {
671-
obj.set("path", JsonUtil.getInstance().createArrayNode(outPaths));
671+
obj.set(JSON_KEY_PATH, JsonUtil.getInstance().createArrayNode(outPaths));
672672
}
673-
obj.put("format", resource.getSerializationFormat());
673+
obj.put(JSON_KEY_FORMAT, resource.getSerializationFormat());
674674
}
675675
obj.remove("originalReferences");
676676
resourcesJsonArray.add(obj);
@@ -848,11 +848,17 @@ private void addResource(Resource resource, boolean validate)
848848
DataPackageException dpe = null;
849849
if (resource.getName() == null){
850850
dpe = new DataPackageValidationException("Invalid Resource, it does not have a name property.");
851+
if (validate)
852+
validate(dpe);
851853
}
852854
if (resource instanceof AbstractDataResource)
853855
addResource((AbstractDataResource) resource, validate);
854856
else if (resource instanceof AbstractReferencebasedResource)
855857
addResource((AbstractReferencebasedResource) resource, validate);
858+
else {
859+
dpe = checkDuplicates(resource);
860+
}
861+
this.resources.add(resource);
856862
if (validate)
857863
validate(dpe);
858864
}
@@ -868,8 +874,6 @@ private void addResource(AbstractDataResource resource, boolean validate)
868874
}
869875
if (validate)
870876
validate(dpe);
871-
872-
this.resources.add(resource);
873877
}
874878

875879
private void addResource(AbstractReferencebasedResource resource, boolean validate)
@@ -882,8 +886,6 @@ private void addResource(AbstractReferencebasedResource resource, boolean valida
882886
}
883887
if (validate)
884888
validate(dpe);
885-
886-
this.resources.add(resource);
887889
}
888890

889891
private void validate(DataPackageException dpe) throws IOException {
@@ -898,7 +900,6 @@ private void validate(DataPackageException dpe) throws IOException {
898900
}
899901
}
900902

901-
// Validate.
902903
this.validate();
903904
}
904905

src/main/java/io/frictionlessdata/datapackage/resource/FilebasedResource.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,20 @@ public File getBasePath() {
9696

9797
@Override
9898
byte[] getRawData(File input) throws IOException {
99-
File f = new File(this.basePath, input.getPath());
100-
try (InputStream inputStream = Files.newInputStream(f.toPath())) {
101-
return getRawData(inputStream);
99+
if (this.isInArchive) {
100+
String fileName = input.getPath().replaceAll("\\\\", "/");
101+
return getZipFileContentAsString (basePath.toPath(), fileName).getBytes();
102+
} else {
103+
File file = new File(this.basePath, input.getPath());
104+
try (InputStream inputStream = Files.newInputStream(file.toPath())) {
105+
return getRawData(inputStream);
106+
}
102107
}
108+
103109
}
104110

105111
@Override
106-
Table createTable(File reference) throws Exception {
112+
Table createTable(File reference) {
107113
return Table.fromSource(reference, basePath, schema, getCsvFormat());
108114
}
109115

@@ -125,7 +131,7 @@ List<Table> readData () throws Exception{
125131
return tables;
126132
}
127133

128-
private List<Table> readfromZipFile() throws Exception {
134+
private List<Table> readfromZipFile() throws IOException {
129135
List<Table> tables = new ArrayList<>();
130136
for (File file : paths) {
131137
String fileName = file.getPath().replaceAll("\\\\", "/");
@@ -135,7 +141,7 @@ private List<Table> readfromZipFile() throws Exception {
135141
}
136142
return tables;
137143
}
138-
private List<Table> readfromOrdinaryFile() throws Exception {
144+
private List<Table> readfromOrdinaryFile() throws IOException {
139145
List<Table> tables = new ArrayList<>();
140146
for (File file : paths) {
141147
/* from the spec: "SECURITY: / (absolute path) and ../ (relative parent path)

src/test/java/io/frictionlessdata/datapackage/PackageTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,23 @@ public void testNonTabularPackage() throws Exception{
384384
Assertions.assertEquals(t, s);
385385
}
386386

387+
@Test
388+
@DisplayName("Test getting resource data from a non-tabular datapackage, ZIP based")
389+
public void testNonTabularPackageFromZip() throws Exception{
390+
String pathName = "/fixtures/zip/non-tabular.zip";
391+
Path resourcePath = TestUtil.getResourcePath(pathName);
392+
Package dp = new Package(resourcePath, true);
393+
394+
Resource<?,?> resource = dp.getResource("logo-svg");
395+
Assertions.assertTrue(resource instanceof FilebasedResource);
396+
byte[] rawData = (byte[])resource.getRawData();
397+
String s = new String (rawData).replaceAll("[\n\r]+", "\n");
398+
399+
byte[] testData = TestUtil.getResourceContent("/fixtures/files/frictionless-color-full-logo.svg");
400+
String t = new String (testData).replaceAll("[\n\r]+", "\n");
401+
Assertions.assertEquals(t, s);
402+
}
403+
387404

388405
@Test
389406
@DisplayName("Test getting resource data from a non-tabular datapackage, URL based")
4.73 KB
Binary file not shown.

0 commit comments

Comments
 (0)