]> source.dussan.org Git - poi.git/commitdiff
[bug-66675] fix issue with writing xlsx (core properties being added twice)
authorPJ Fanning <fanningpj@apache.org>
Wed, 12 Jul 2023 12:25:01 +0000 (12:25 +0000)
committerPJ Fanning <fanningpj@apache.org>
Wed, 12 Jul 2023 12:25:01 +0000 (12:25 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1910949 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java
poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
test-data/spreadsheet/bug66675.xlsx [new file with mode: 0644]

index 11467117054957a5c030daa3483e54eb54ebfc09..35f3c10c019c17be055dccf940df4f6316e7da0a 100644 (file)
@@ -940,7 +940,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
             throw new IllegalArgumentException("part");
         }
 
-        if (partList.containsKey(part._partName)) {
+        if (hasPackagePart(part)) {
             if (!partList.get(part._partName).isDeleted()) {
                 throw new InvalidOperationException(
                         "A part with the name '"
@@ -958,6 +958,10 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
         return part;
     }
 
+    protected boolean hasPackagePart(PackagePart part) {
+        return partList.containsKey(part._partName);
+    }
+
     /**
      * Remove the specified part in this package. If this part is relationship
      * part, then delete all relationships in the source part.
index 3567e6607471f27cf0bf10c4ef2c905e595f1d89..dc176b441b906ae49088255074375cf6161a82a2 100644 (file)
@@ -542,7 +542,9 @@ public final class ZipPackage extends OPCPackage {
                 // Ensure that core properties are added if missing
                 getPackageProperties();
                 // Add core properties to part list ...
-                addPackagePart(this.packageProperties);
+                if (!hasPackagePart(this.packageProperties)) {
+                    addPackagePart(this.packageProperties);
+                }
                 // ... and to add its relationship ...
                 this.relationships.addRelationship(this.packageProperties
                         .getPartName().getURI(), TargetMode.INTERNAL,
index d8d36726d073e0c287a97fc1c13c5c50d6199d76..fe3ac973cd0a96ebb51a1282c944989f3fbb0519 100644 (file)
@@ -3864,6 +3864,23 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
         }
     }
 
+    @Test
+    void testBug66675() throws Exception {
+        try (XSSFWorkbook wb = openSampleWorkbook("bug66675.xlsx")) {
+            POIXMLProperties.CoreProperties coreProperties = wb.getProperties().getCoreProperties();
+            assertNotNull(coreProperties);
+            wb.removeSheetAt(0);
+            try (UnsynchronizedByteArrayOutputStream bos = UnsynchronizedByteArrayOutputStream.builder().get()) {
+                wb.write(bos);
+                try (XSSFWorkbook wb2 = new XSSFWorkbook(bos.toInputStream())) {
+                    XSSFSheet sheet = wb2.getSheetAt(0);
+                    assertNotNull(sheet);
+                    assertNotNull(wb2.getProperties().getCoreProperties());
+                }
+            }
+        }
+    }
+
     private static void readByCommonsCompress(File temp_excel_poi) throws IOException {
         /* read by commons-compress*/
         try (ZipFile zipFile = new ZipFile(temp_excel_poi)) {
diff --git a/test-data/spreadsheet/bug66675.xlsx b/test-data/spreadsheet/bug66675.xlsx
new file mode 100644 (file)
index 0000000..61f0dca
Binary files /dev/null and b/test-data/spreadsheet/bug66675.xlsx differ