Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure import tags are positioned as first elements inside the schema. #22

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>