Skip to content

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

Open
@AlexMinaev19

Description

@AlexMinaev19

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions