You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2026/09/15 21:15:29 UTC

(poi-xmlbeans) branch trunk updated: Fix PROPERTY_QSET indexing for types with several multi-name properties (#120)

This is an automated email from the ASF dual-hosted git repository.

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi-xmlbeans.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 77682fc0 Fix PROPERTY_QSET indexing for types with several multi-name properties (#120)
77682fc0 is described below

commit 77682fc029c3f50a5052c7b90921fd9d7f69544b
Author: PJ Fanning <pj...@users.noreply.github.com>
AuthorDate: Tue Sep 15 22:15:24 2026 +0100

    Fix PROPERTY_QSET indexing for types with several multi-name properties (#120)
    
    * Moving iterator declaration out of "for" loop.
    
    While this iterator is within the loop, all generated references to
    PROPERTY_QSET[] will point to index 0 instead of being incremented.
    
    * Add regression test for PROPERTY_QSET indexing
    
    Since 5.2.1 the per-property index in printStaticFields was reset on
    every loop iteration, so every property with multiple accepted names
    referenced PROPERTY_QSET[0]. The test compiles a type with two
    substitution-group properties and checks each accessor uses its own slot.
    
    Co-Authored-By: Claude Fable 5.1 <no...@anthropic.com>
    
    ---------
    
    Co-authored-by: dwarven-cleric <jo...@gmail.com>
    Co-authored-by: Claude Fable 5.1 <no...@anthropic.com>
---
 .../impl/schema/SchemaTypeCodePrinter.java         |   2 +-
 .../impl/schema/PropertyQSetIndexTest.java         | 112 +++++++++++++++++++++
 2 files changed, 113 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java b/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java
index 15178d5d..1d9f383b 100644
--- a/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java
+++ b/src/main/java/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java
@@ -1442,9 +1442,9 @@ public final class SchemaTypeCodePrinter implements SchemaCodePrinter {
 
         if (countQSet > 1) {
             emit("private static final QNameSet[] PROPERTY_QSET = {");
+            int index = 0;
             for (SchemaProperty prop : properties) {
                 final QName[] qnames = prop.acceptedNames();
-                int index = 0;
                 if (qnames != null && qnames.length > 1) {
                     propMap.get(prop).setSetIndex(index++);
                     emit("QNameSet.forArray( new QName[] { ");
diff --git a/src/test/java/org/apache/xmlbeans/impl/schema/PropertyQSetIndexTest.java b/src/test/java/org/apache/xmlbeans/impl/schema/PropertyQSetIndexTest.java
new file mode 100644
index 00000000..5682b750
--- /dev/null
+++ b/src/test/java/org/apache/xmlbeans/impl/schema/PropertyQSetIndexTest.java
@@ -0,0 +1,112 @@
+/*   Copyright 2026 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ */
+package org.apache.xmlbeans.impl.schema;
+
+import org.apache.xmlbeans.Filer;
+import org.apache.xmlbeans.SchemaTypeSystem;
+import org.apache.xmlbeans.XmlBeans;
+import org.apache.xmlbeans.XmlObject;
+import org.apache.xmlbeans.XmlOptions;
+import org.junit.jupiter.api.Test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * When a type has two or more element properties that each accept several
+ * QNames (for example two substitution group heads), the generated impl class
+ * shares one {@code PROPERTY_QSET} array between them. Each property must
+ * reference its own slot in that array; a regression in 5.2.1 made every
+ * property reference slot 0, so the second property matched the first
+ * property's element names.
+ */
+public class PropertyQSetIndexTest {
+
+    private static final String SCHEMA =
+        "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'" +
+        "  targetNamespace='urn:qset' xmlns='urn:qset' elementFormDefault='qualified'>" +
+        "  <xs:element name='headA' type='xs:string'/>" +
+        "  <xs:element name='subA' type='xs:string' substitutionGroup='headA'/>" +
+        "  <xs:element name='headB' type='xs:string'/>" +
+        "  <xs:element name='subB' type='xs:string' substitutionGroup='headB'/>" +
+        "  <xs:complexType name='Holder'>" +
+        "    <xs:sequence>" +
+        "      <xs:element ref='headA' maxOccurs='unbounded'/>" +
+        "      <xs:element ref='headB' maxOccurs='unbounded'/>" +
+        "    </xs:sequence>" +
+        "  </xs:complexType>" +
+        "</xs:schema>";
+
+    private static class InMemoryFiler implements Filer {
+        final Map<String, StringWriter> sources = new HashMap<>();
+
+        @Override
+        public OutputStream createBinaryFile(String typename) {
+            return new ByteArrayOutputStream();
+        }
+
+        @Override
+        public Writer createSourceFile(String typename, String sourceCodeEncoding) {
+            StringWriter sw = new StringWriter();
+            sources.put(typename, sw);
+            return sw;
+        }
+    }
+
+    @Test
+    void eachMultiNamePropertyGetsItsOwnQSetSlot() throws Exception {
+        XmlObject xsd = XmlObject.Factory.parse(SCHEMA);
+        InMemoryFiler filer = new InMemoryFiler();
+        // supplying a Filer makes the compiler assign Java names and emit the sources
+        SchemaTypeSystem sts = XmlBeans.compileXmlBeans(null, null, new XmlObject[]{xsd}, null, null, filer, new XmlOptions());
+        assertNotNull(sts);
+
+        String impl = filer.sources.entrySet().stream()
+            .filter(e -> e.getKey().endsWith("HolderImpl"))
+            .map(e -> e.getValue().toString())
+            .findFirst().orElse(null);
+        assertNotNull(impl, "HolderImpl source not generated: " + filer.sources.keySet());
+
+        // both properties accept two QNames, so both must be backed by a QNameSet slot
+        assertTrue(impl.contains("PROPERTY_QSET[0]"), "generated source lacks PROPERTY_QSET[0]");
+        assertTrue(impl.contains("PROPERTY_QSET[1]"), "generated source lacks PROPERTY_QSET[1]");
+
+        // headA is the first property and headB the second: check the accessor
+        // for headB uses slot 1 rather than slot 0
+        assertEquals(0, qsetSlotFor(impl, "getHeadAArray"), "headA slot");
+        assertEquals(1, qsetSlotFor(impl, "getHeadBArray"), "headB slot");
+    }
+
+    /**
+     * Finds the {@code PROPERTY_QSET[n]} slot referenced in the body of the
+     * given no-arg accessor method.
+     */
+    private static int qsetSlotFor(String source, String methodName) {
+        Pattern p = Pattern.compile(methodName + "\\(\\)\\s*\\{.*?PROPERTY_QSET\\[(\\d+)\\]", Pattern.DOTALL);
+        Matcher m = p.matcher(source);
+        assertTrue(m.find(), "no PROPERTY_QSET reference found in " + methodName);
+        return Integer.parseInt(m.group(1));
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org