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

Metadata field type validation against Entity property type does not detect nullability #11620

Open
AlexMinaev19 opened this issue Oct 2, 2024 · 0 comments

Comments

@AlexMinaev19
Copy link

Bug Report

In feature request there was added an ability to validate field type against Entity property type.

Schema validation tool does not detect mapping problems when a typed property of the entity defined as nullable, but there is no nullable: true in the column metadata definition.

Q A
BC Break no
Version 2.19.7

Summary

For example, we have the following entity:

class User
{
    // ...

    #[ORM\Column(type: Types::STRING, length: 64)]
    private ?string $name;

    // ...
}

Schema validation tool should report, that mapping is invalid due to property is type string|null, but metadata mapping expecting string due to nullable named property of ORM\Column is false by default. The error should gone when manually will be specified nullable: true of ORM\Column.

Current behavior

There are no errors for provided example class, but should be.

How to reproduce

Create entity:

namespace App\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table('user')]
class User
{
    #[ORM\Column(type: Types::STRING, length: 64)]
    private ?string $name;

    public function __construct(?string $name)
    {
         $this->name = $name;
    }

    public function getName(): ?string
    {
         return $this->name;
    }
}

Create migration based on this entity. The table definition in migration will have a non-nullable column name. After that, please go ahead and execute this migration. And then run orm:validate-schema.

Expected behavior

The command should report mapping error:

Mapping
-------

 [FAIL] The entity-class App\Entity\User mapping is invalid:
 * The field 'App\Entity\User#name' has the property type 'string|null' that differs from the metadata field type 'string' returned by the 'string' DBAL type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant