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

coheigea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ws-xmlschema.git


The following commit(s) were added to refs/heads/master by this push:
     new 3204f92c [WSCOMMONS-578] StackOverflowError occurs in 
XmlSchema.getAllSchemas() (#59)
3204f92c is described below

commit 3204f92c1a0f8314d9dbbec7e0e8a13084ab9cde
Author: pavelsalamon <[email protected]>
AuthorDate: Tue Oct 21 07:33:24 2025 +0200

    [WSCOMMONS-578] StackOverflowError occurs in XmlSchema.getAllSchemas() (#59)
---
 .../apache/ws/commons/schema/XmlSchemaSerializer.java    | 16 +++++++++++++++-
 .../src/test/java/tests/CircularSchemaTest.java          | 15 +++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git 
a/xmlschema-core/src/main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java
 
b/xmlschema-core/src/main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java
index dae86a98..a6b43102 100644
--- 
a/xmlschema-core/src/main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java
+++ 
b/xmlschema-core/src/main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java
@@ -23,8 +23,10 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import javax.xml.XMLConstants;
 import javax.xml.namespace.QName;
@@ -72,6 +74,11 @@ public class XmlSchemaSerializer {
     public static final String XSD_NAMESPACE = 
XMLConstants.W3C_XML_SCHEMA_NS_URI;
     private static final String XMLNS_NAMESPACE_URI = 
"http://www.w3.org/2000/xmlns/";;
 
+    /**
+     * Keeps track of already serialized schemas.
+     */
+    private static ThreadLocal<Set<XmlSchema>> serializedSchemaElements = new 
ThreadLocal<>();
+
     String xsdPrefix = "xs";
     List<Document> docs;
     Element schemaElement;
@@ -129,7 +136,10 @@ public class XmlSchemaSerializer {
      */
     public Document[] serializeSchema(XmlSchema schemaObj, boolean 
serializeIncluded)
         throws XmlSchemaSerializerException {
-        return serializeSchemaElement(schemaObj, serializeIncluded);
+        serializedSchemaElements.set(new HashSet<>());
+        Document[] result = serializeSchemaElement(schemaObj, 
serializeIncluded);
+        serializedSchemaElements.remove();
+        return result;
     }
 
     /**
@@ -1543,6 +1553,10 @@ public class XmlSchemaSerializer {
 
     Document[] serializeSchemaElement(XmlSchema schemaObj,
                                       boolean serializeIncluded) throws 
XmlSchemaSerializerException {
+        // skip already serialized schemas
+        if (!serializedSchemaElements.get().add(schemaObj)) {
+            return new Document[0];
+        }
 
         List<XmlSchemaObject> items = schemaObj.getItems();
         Document serializedSchemaDocs;
diff --git a/xmlschema-core/src/test/java/tests/CircularSchemaTest.java 
b/xmlschema-core/src/test/java/tests/CircularSchemaTest.java
index 980be010..f981113f 100644
--- a/xmlschema-core/src/test/java/tests/CircularSchemaTest.java
+++ b/xmlschema-core/src/test/java/tests/CircularSchemaTest.java
@@ -28,6 +28,7 @@ import org.apache.ws.commons.schema.XmlSchemaCollection;
 
 import org.junit.Assert;
 import org.junit.Test;
+import org.w3c.dom.Document;
 
 public class CircularSchemaTest extends Assert {
     @Test
@@ -43,4 +44,18 @@ public class CircularSchemaTest extends Assert {
         assertNotNull(xmlSchemas);
         assertEquals(3, xmlSchemas.length);
     }
+    
+    @Test
+    public void testCircularSerialization() throws Exception {
+        XmlSchemaCollection schemas = new XmlSchemaCollection();
+        File file = new File(Resources.asURI("circular/a.xsd"));
+        InputSource source = new InputSource(new FileInputStream(file));
+        source.setSystemId(file.toURI().toURL().toString());
+
+        XmlSchema xmlSchema = schemas.read(source);
+        
+        Document[] allSchemas = xmlSchema.getAllSchemas();
+        assertNotNull(allSchemas);
+        assertEquals(2, allSchemas.length);
+    }
 }

Reply via email to