Skip to content

Commit

Permalink
Enable XML validation unconditionally
Browse files Browse the repository at this point in the history
Note that this makes ext-dom a hard requirement when using the XML
driver.
  • Loading branch information
greg0ire committed Jun 6, 2023
1 parent e0b7240 commit 0dad2da
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 169 deletions.
11 changes: 11 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Upgrade to 3.0

## BC BREAK: `Mapping\Driver\XmlDriver::__construct()` third argument is now a no-op

The third argument to
`Doctrine\ORM\Mapping\Driver\XmlDriver::__construct()` was introduced to
let users opt-in to XML validation, that is now always enabled, regardless of
the value of that argument.

As a consequence, the same goes for
`Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver`, and for
`Doctrine\ORM\ORMSetup::createXMLMetadataConfiguration()`.

## BC BREAK: `Mapping\Driver\AttributeDriver::__construct()` second argument is now a no-op

The second argument to
Expand Down
3 changes: 2 additions & 1 deletion docs/en/reference/xml-mapping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ XML Mapping
===========

The XML mapping driver enables you to provide the ORM metadata in
form of XML documents.
form of XML documents. It requires the ``dom`` extension in order to be
able to validate your mapping documents against its XML Schema.

The XML driver is backed by an XML Schema document that describes
the structure of a mapping document. The most recent version of the
Expand Down
14 changes: 12 additions & 2 deletions lib/Doctrine/ORM/Mapping/Driver/SimplifiedXmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace Doctrine\ORM\Mapping\Driver;

use Doctrine\Persistence\Mapping\Driver\SymfonyFileLocator;
use InvalidArgumentException;

use function sprintf;

/**
* XmlDriver that additionally looks for mapping information in a global file.
Expand All @@ -16,10 +19,17 @@ class SimplifiedXmlDriver extends XmlDriver
/**
* {@inheritDoc}
*/
public function __construct($prefixes, $fileExtension = self::DEFAULT_FILE_EXTENSION, bool $isXsdValidationEnabled = false)
public function __construct($prefixes, $fileExtension = self::DEFAULT_FILE_EXTENSION, bool $isXsdValidationEnabled = true)
{
if (! $isXsdValidationEnabled) {
throw new InvalidArgumentException(sprintf(
'The $isXsdValidationEnabled argument is no longer supported, make sure to omit it when calling %s.',
__METHOD__,
));
}

$locator = new SymfonyFileLocator((array) $prefixes, $fileExtension);

parent::__construct($locator, $fileExtension, $isXsdValidationEnabled);
parent::__construct($locator, $fileExtension);
}
}
23 changes: 8 additions & 15 deletions lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Doctrine\ORM\Mapping\Driver;

use Doctrine\Common\Collections\Criteria;
use Doctrine\Deprecations\Deprecation;
use Doctrine\ORM\Mapping\Builder\EntityListenerBuilder;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
Expand Down Expand Up @@ -44,11 +43,13 @@ class XmlDriver extends FileDriver

/**
* {@inheritDoc}
*
* @param true $isXsdValidationEnabled no-op, will be removed in 4.0
*/
public function __construct(
string|array|FileLocator $locator,
string $fileExtension = self::DEFAULT_FILE_EXTENSION,
private readonly bool $isXsdValidationEnabled = false,
bool $isXsdValidationEnabled = true,
) {
if (! extension_loaded('simplexml')) {
throw new LogicException(sprintf(
Expand All @@ -58,17 +59,13 @@ public function __construct(
}

if (! $isXsdValidationEnabled) {
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/6728',
sprintf(
'Using XML mapping driver with XSD validation disabled is deprecated'
. ' and will not be supported in Doctrine ORM 3.0.',
),
);
throw new InvalidArgumentException(sprintf(
'The $isXsdValidationEnabled argument is no longer supported, make sure to omit it when calling %s.',
__METHOD__,
));
}

if ($isXsdValidationEnabled && ! extension_loaded('dom')) {
if (! extension_loaded('dom')) {
throw new LogicException(sprintf(
'XSD validation cannot be enabled because the DOM extension is missing.',
));
Expand Down Expand Up @@ -947,10 +944,6 @@ protected function loadMappingFile($file)

private function validateMapping(string $file): void
{
if (! $this->isXsdValidationEnabled) {
return;
}

$backedUpErrorSetting = libxml_use_internal_errors(true);

try {
Expand Down
12 changes: 11 additions & 1 deletion lib/Doctrine/ORM/ORMSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Mapping\Driver\XmlDriver;
use InvalidArgumentException;
use Psr\Cache\CacheItemPoolInterface;
use Redis;
use RuntimeException;
Expand All @@ -18,6 +19,7 @@
use function class_exists;
use function extension_loaded;
use function md5;
use function sprintf;
use function sys_get_temp_dir;

final class ORMSetup
Expand All @@ -43,14 +45,22 @@ public static function createAttributeMetadataConfiguration(
* Creates a configuration with an XML metadata driver.
*
* @param string[] $paths
* @param true $isXsdValidationEnabled
*/
public static function createXMLMetadataConfiguration(
array $paths,
bool $isDevMode = false,
string|null $proxyDir = null,
CacheItemPoolInterface|null $cache = null,
bool $isXsdValidationEnabled = false,
bool $isXsdValidationEnabled = true,
): Configuration {
if (! $isXsdValidationEnabled) {
throw new InvalidArgumentException(sprintf(
'The $isXsdValidationEnabled argument is no longer supported, make sure to omit it when calling %s.',
__METHOD__,
));
}

$config = self::createConfiguration($isDevMode, $proxyDir, $cache);
$config->setMetadataDriverImpl(new XmlDriver($paths, XmlDriver::DEFAULT_FILE_EXTENSION, $isXsdValidationEnabled));

Expand Down
6 changes: 5 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ parameters:
# To be removed in 4.0
-
message: '#Negated boolean expression is always false\.#'
path: lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php
paths:
- lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php
- lib/Doctrine/ORM/Mapping/Driver/SimplifiedXmlDriver.php
- lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
- lib/Doctrine/ORM/ORMSetup.php
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
<file name="lib/Doctrine/ORM/PersistentCollection.php"/>
<!-- Remove on 4.0.x -->
<file name="lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php"/>
<file name="lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php"/>
<file name="lib/Doctrine/ORM/ORMSetup.php"/>
</errorLevel>
</DocblockTypeContradiction>
<InvalidArgument>
Expand Down
11 changes: 0 additions & 11 deletions tests/Doctrine/Tests/Models/OrnementalOrphanRemoval/Person.php

This file was deleted.

This file was deleted.

94 changes: 0 additions & 94 deletions tests/Doctrine/Tests/ORM/Functional/ManyToOneOrphanRemovalTest.php

This file was deleted.

This file was deleted.

This file was deleted.

12 changes: 12 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/XmlMappingDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Doctrine\Tests\Models\Project\ProjectName;
use Doctrine\Tests\Models\ValueObjects\Name;
use Doctrine\Tests\Models\ValueObjects\Person;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;

Expand Down Expand Up @@ -299,6 +300,17 @@ public function testClassNameInFieldOrId(): void
self::assertEquals(ProjectId::class, $id['type']);
self::assertEquals(ProjectName::class, $name['type']);
}

public function testDisablingXmlValidationIsNotPossible(): void
{
$this->expectException(InvalidArgumentException::class);

new XmlDriver(
__DIR__ . DIRECTORY_SEPARATOR . 'xml',
XmlDriver::DEFAULT_FILE_EXTENSION,
false,
);
}
}

class CTI
Expand Down
8 changes: 8 additions & 0 deletions tests/Doctrine/Tests/ORM/ORMSetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Mapping\Driver\XmlDriver;
use Doctrine\ORM\ORMSetup;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -39,6 +40,13 @@ public function testXMLConfiguration(): void
self::assertInstanceOf(XmlDriver::class, $config->getMetadataDriverImpl());
}

public function testDisablingXmlValidationIsNotPossible(): void
{
$this->expectException(InvalidArgumentException::class);

ORMSetup::createXMLMetadataConfiguration(paths: [], isXsdValidationEnabled: false);
}

#[RequiresPhpExtension('apcu')]
public function testCacheNamespaceShouldBeGeneratedForApcu(): void
{
Expand Down

0 comments on commit 0dad2da

Please sign in to comment.