Skip to content

Commit 3091109

Browse files
committed
SWS-575 - CommonsXsdSchemaCollection: Combination of xs:includes and xs:imports in a schema result in incorrect/overcomplete schema in wsdl generated by DefaultWsdl11Definition
1 parent 3eeb8b2 commit 3091109

File tree

6 files changed

+105
-8
lines changed

6 files changed

+105
-8
lines changed

xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollection.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,23 +183,24 @@ public XmlValidator createValidator() throws IOException {
183183

184184
private void inlineIncludes(XmlSchema schema, Set processedIncludes, Set processedImports) {
185185
processedIncludes.add(schema);
186-
XmlSchemaObjectCollection includes = schema.getIncludes();
187-
for (int i = 0; i < includes.getCount(); i++) {
188-
XmlSchemaExternal external = (XmlSchemaExternal) includes.getItem(i);
189-
if (external instanceof XmlSchemaInclude) {
190-
XmlSchema includedSchema = external.getSchema();
191-
XmlSchemaObjectCollection items = schema.getItems();
186+
187+
XmlSchemaObjectCollection schemaItems = schema.getItems();
188+
for (int i = 0; i < schemaItems.getCount(); i++) {
189+
XmlSchemaObject schemaObject = schemaItems.getItem(i);
190+
if (schemaObject instanceof XmlSchemaInclude) {
191+
XmlSchema includedSchema = ((XmlSchemaInclude) schemaObject).getSchema();
192192
if (!processedIncludes.contains(includedSchema)) {
193193
inlineIncludes(includedSchema, processedIncludes, processedImports);
194194
findImports(includedSchema, processedImports, processedIncludes);
195195
XmlSchemaObjectCollection includeItems = includedSchema.getItems();
196196
for (int j = 0; j < includeItems.getCount(); j++) {
197197
XmlSchemaObject includedItem = includeItems.getItem(j);
198-
items.add(includedItem);
198+
schemaItems.add(includedItem);
199199
}
200200
}
201201
// remove the <include/>
202-
items.remove(external);
202+
schemaItems.removeAt(i);
203+
i--;
203204
}
204205
}
205206
}

xml/src/test/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollectionTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,29 @@ public void testInvalidSchema() throws Exception {
120120
// expected
121121
}
122122
}
123+
124+
public void testIncludesAndImports() throws Exception {
125+
Resource hr = new ClassPathResource("hr.xsd", getClass());
126+
collection.setXsds(new Resource[]{hr});
127+
collection.setInline(true);
128+
collection.afterPropertiesSet();
129+
130+
XsdSchema[] schemas = collection.getXsdSchemas();
131+
assertEquals("Invalid amount of XSDs loaded", 2, schemas.length);
132+
133+
assertEquals("Invalid target namespace", "http://mycompany.com/hr/schemas", schemas[0].getTargetNamespace());
134+
Resource hr_employee = new ClassPathResource("hr_employee.xsd", getClass());
135+
Document expected = documentBuilder.parse(SaxUtils.createInputSource(hr_employee));
136+
DOMResult domResult = new DOMResult();
137+
transformer.transform(schemas[0].getSource(), domResult);
138+
assertXMLEqual("Invalid XSD generated", expected, (Document) domResult.getNode());
139+
140+
assertEquals("Invalid target namespace", "http://mycompany.com/hr/schemas/holiday", schemas[1].getTargetNamespace());
141+
Resource holiday = new ClassPathResource("holiday.xsd", getClass());
142+
expected = documentBuilder.parse(SaxUtils.createInputSource(holiday));
143+
domResult = new DOMResult();
144+
transformer.transform(schemas[1].getSource(), domResult);
145+
assertXMLEqual("Invalid XSD generated", expected, (Document) domResult.getNode());
146+
147+
}
123148
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
4+
xmlns:hr="http://mycompany.com/hr/schemas"
5+
elementFormDefault="qualified"
6+
targetNamespace="http://mycompany.com/hr/schemas">
7+
<xs:complexType name="EmployeeType">
8+
<xs:sequence>
9+
<xs:element name="Number" type="xs:integer"/>
10+
<xs:element name="FirstName" type="xs:string"/>
11+
<xs:element name="LastName" type="xs:string"/>
12+
</xs:sequence>
13+
</xs:complexType>
14+
</xs:schema>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
4+
xmlns:hrh="http://mycompany.com/hr/schemas/holiday"
5+
attributeFormDefault="unqualified"
6+
elementFormDefault="qualified"
7+
targetNamespace="http://mycompany.com/hr/schemas/holiday">
8+
<xs:complexType name="HolidayType">
9+
<xs:sequence>
10+
<xs:element name="StartDate" type="xs:date"/>
11+
<xs:element name="EndDate" type="xs:date"/>
12+
</xs:sequence>
13+
</xs:complexType>
14+
</xs:schema>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
3+
xmlns:hr="http://mycompany.com/hr/schemas"
4+
xmlns:hrh="http://mycompany.com/hr/schemas/holiday"
5+
elementFormDefault="qualified"
6+
targetNamespace="http://mycompany.com/hr/schemas">
7+
8+
<xs:import namespace="http://mycompany.com/hr/schemas/holiday" schemaLocation="holiday.xsd"/>
9+
<xs:include schemaLocation="employee.xsd"/>
10+
<xs:element name="HolidayRequest">
11+
<xs:complexType>
12+
<xs:all>
13+
<xs:element name="Holiday" type="hrh:HolidayType"/>
14+
<xs:element name="Employee" type="hr:EmployeeType"/>
15+
</xs:all>
16+
</xs:complexType>
17+
</xs:element>
18+
</xs:schema>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
3+
xmlns:hr="http://mycompany.com/hr/schemas"
4+
xmlns:hrh="http://mycompany.com/hr/schemas/holiday"
5+
attributeFormDefault="unqualified"
6+
elementFormDefault="qualified"
7+
targetNamespace="http://mycompany.com/hr/schemas">
8+
9+
<xs:import namespace="http://mycompany.com/hr/schemas/holiday"/>
10+
<xs:element name="HolidayRequest">
11+
<xs:complexType>
12+
<xs:all>
13+
<xs:element name="Holiday" type="hrh:HolidayType"/>
14+
<xs:element name="Employee" type="hr:EmployeeType"/>
15+
</xs:all>
16+
</xs:complexType>
17+
</xs:element>
18+
<xs:complexType name="EmployeeType">
19+
<xs:sequence>
20+
<xs:element name="Number" type="xs:integer"/>
21+
<xs:element name="FirstName" type="xs:string"/>
22+
<xs:element name="LastName" type="xs:string"/>
23+
</xs:sequence>
24+
</xs:complexType>
25+
</xs:schema>

0 commit comments

Comments
 (0)