Skip to content

Commit

Permalink
Merge pull request #22 from veewee/spec-compliant-imports-sorting
Browse files Browse the repository at this point in the history
Make sure import tags are positioned as first elements inside the schema.
  • Loading branch information
veewee authored Aug 21, 2024
2 parents 31adf06 + 0886de1 commit 7c330e3
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Xml/Configurator/FlattenXsdImports.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
use VeeWee\Xml\Exception\RuntimeException;
use function Psl\Type\instance_of;
use function Psl\Type\nullable;
use function Psl\Vec\reverse;
use function VeeWee\Xml\Dom\Assert\assert_element;
use function VeeWee\Xml\Dom\Locator\Node\children;
use function VeeWee\Xml\Dom\Manipulator\Element\copy_named_xmlns_attributes;
use function VeeWee\Xml\Dom\Manipulator\Node\append_external_node;
use function VeeWee\Xml\Dom\Manipulator\Node\remove;
use function VeeWee\Xml\Dom\Xpath\Configurator\functions;

/**
* This class deals with xsd:import, xsd:include and xsd:redefine tags.
Expand Down Expand Up @@ -65,6 +67,8 @@ public function __invoke(DOMDocument $document): DOMDocument
}
}

$this->rearrangeImportsAsFirstElements($xml);

return $document;
}

Expand Down Expand Up @@ -219,4 +223,31 @@ private function fixRemovedDefaultXmlnsDeclarationsDuringImport(DOMElement $targ

$target->setAttribute('xmlns', $source->getAttribute('xmlns'));
}

/**
* Makes sure to rearrange the import statements on top of the flattened XSD schema.
* This makes the flattened XSD spec compliant:
*
* @see https://www.w3.org/TR/xmlschema11-1/#declare-schema
*
* <schema>
* Content: ((include | import | redefine | override | annotation)*,
* (defaultOpenContent, annotation*)?,
* ((simpleType | complexType | group | attributeGroup | element | attribute | notation), annotation*)*)
* </schema>
*
* @throws RuntimeException
*/
private function rearrangeImportsAsFirstElements(Document $xml): void
{
$xpath = $xml->xpath(new WsdlPreset($xml), functions(['array_reverse']));
$imports = $xpath
->query('//schema:import')
->expectAllOfType(DOMElement::class);

foreach (reverse($imports) as $import) {
$parentSchema = assert_element($import->parentNode);
$parentSchema->prepend($import);
}
}
}
5 changes: 5 additions & 0 deletions tests/Unit/Xml/Configurator/FlattenXsdImportsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,10 @@ public function provideTestCases()
'expected' => Document::fromXmlFile(FIXTURE_DIR.'/flattening/result/root-xmlns-import-issue-result.wsdl'),
canonicalize(),
];
yield 'rearranged-imports' => [
'wsdl' => FIXTURE_DIR.'/flattening/rearranged-imports.wsdl',
'expected' => Document::fromXmlFile(FIXTURE_DIR.'/flattening/result/rearranged-imports.wsdl'),
comparable(),
];
}
}
15 changes: 15 additions & 0 deletions tests/fixtures/flattening/rearranged-imports.wsdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<definitions name="InteropTest"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/"
targetNamespace="http://soapinterop.org/">
<types>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://soapinterop.org/xsd">
<import namespace="http://soapinterop.org/rearranged-import" schemaLocation="xsd/rearranged-import.xsd" />
<xsd:include schemaLocation="xsd/rearranged-include1.xsd" />
<xsd:include schemaLocation="xsd/rearranged-include2.xsd" />

<element name="element1" type="xsd:string" />
</schema>
</types>
</definitions>
20 changes: 20 additions & 0 deletions tests/fixtures/flattening/result/rearranged-imports.wsdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<definitions name="InteropTest"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/"
targetNamespace="http://soapinterop.org/">
<types>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://soapinterop.org/xsd">
<import namespace="http://soapinterop.org/rearranged-import" />
<import namespace="http://soapinterop.org/rearranged-import1" />
<import namespace="http://soapinterop.org/rearranged-import2" />

<element name="element1" type="xsd:string" />
<element name="includedElement1" type="xsd:string" />
<element name="includedElement2" type="xsd:string" />
</schema>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://soapinterop.org/rearranged-import"></schema>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://soapinterop.org/rearranged-import1"></schema>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://soapinterop.org/rearranged-import2"></schema>
</types>
</definitions>
1 change: 1 addition & 0 deletions tests/fixtures/flattening/xsd/rearranged-import.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://soapinterop.org/rearranged-import"></schema>
1 change: 1 addition & 0 deletions tests/fixtures/flattening/xsd/rearranged-import1.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://soapinterop.org/rearranged-import1"></schema>
1 change: 1 addition & 0 deletions tests/fixtures/flattening/xsd/rearranged-import2.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://soapinterop.org/rearranged-import2"></schema>
6 changes: 6 additions & 0 deletions tests/fixtures/flattening/xsd/rearranged-include1.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://soapinterop.org/xsd">

<import namespace="http://soapinterop.org/rearranged-import1" schemaLocation="rearranged-import1.xsd" />

<element name="includedElement1" type="xsd:string" />
</schema>
6 changes: 6 additions & 0 deletions tests/fixtures/flattening/xsd/rearranged-include2.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://soapinterop.org/xsd">

<import namespace="http://soapinterop.org/rearranged-import2" schemaLocation="rearranged-import2.xsd" />

<element name="includedElement2" type="xsd:string" />
</schema>

0 comments on commit 7c330e3

Please sign in to comment.