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

Fix JSON mapping linting against subset of builtin types #11076

Merged
merged 1 commit into from
Dec 2, 2023
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
9 changes: 8 additions & 1 deletion lib/Doctrine/ORM/Tools/SchemaValidator.php
Original file line number Diff line number Diff line change
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)
norkunas marked this conversation as resolved.
Show resolved Hide resolved
) {
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
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);
}
}