Skip to content

Commit

Permalink
Fix JSON mapping linting against subset of builtin types (#11076)
Browse files Browse the repository at this point in the history
  • Loading branch information
norkunas committed Dec 2, 2023
1 parent 23d36c0 commit 44e943e
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/Doctrine/ORM/Tools/SchemaValidator.php
Expand Up @@ -371,7 +371,7 @@ function (array $fieldMapping) use ($class): ?string {
}

// If the property type is not a named type, we cannot check it
if (! ($propertyType instanceof ReflectionNamedType)) {
if (! ($propertyType instanceof ReflectionNamedType) || $propertyType->getName() === 'mixed') {
return null;
}

Expand Down Expand Up @@ -406,6 +406,13 @@ function (array $fieldMapping) use ($class): ?string {
);
}

if (
$fieldMapping['type'] === 'json'
&& in_array($propertyType, ['string', 'int', 'float', 'bool', 'true', 'false', 'null'], true)
) {
return null;
}

return sprintf(
"The field '%s#%s' has the property type '%s' that differs from the metadata field type '%s' returned by the '%s' DBAL type.",
$class->name,
Expand Down
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11072;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;

/**
* @Entity
*/
class GH11072EntityAdvanced extends GH11072EntityBasic
{
/** @Column(type="json") */
public mixed $anything;

/** @Column(type="json") */
public true $alwaysTrue = true;

/** @Column(type="json") */
public false $alwaysFalse = false;

/** @Column(type="json") */
public null $alwaysNull = null;
}
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11072;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;

/**
* @Entity
*/
class GH11072EntityBasic
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue
* @var int
*/
public $id;

/** @Column(type="json") */
public string $jsonString = 'test';

/** @Column(type="json") */
public int $age = 99;

/** @Column(type="json") */
public float $score = 0.0;

/** @Column(type="json", nullable=true) */
public ?bool $trinary = null;

/** @Column(type="json") */
public array $metadata = [];
}
42 changes: 42 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH11072/GH11072Test.php
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11072;

use Doctrine\ORM\Tools\SchemaValidator;
use Doctrine\Tests\OrmFunctionalTestCase;

/**
* @requires PHP >= 7.4
*/
final class GH11072Test extends OrmFunctionalTestCase
{
/** @var SchemaValidator */
private $validator;

protected function setUp(): void
{
$this->_em = $this->getTestEntityManager();
$this->validator = new SchemaValidator($this->_em);
}

public function testAcceptsSubsetOfBuiltinTypesWithoutErrors(): void
{
$class = $this->_em->getClassMetadata(GH11072EntityBasic::class);
$ce = $this->validator->validateClass($class);

self::assertSame([], $ce);
}

/**
* @requires PHP >= 8.2
*/
public function testAcceptsAdvancedSubsetOfBuiltinTypesWithoutErrors(): void
{
$class = $this->_em->getClassMetadata(GH11072EntityAdvanced::class);
$ce = $this->validator->validateClass($class);

self::assertSame([], $ce);
}
}

0 comments on commit 44e943e

Please sign in to comment.