From b27489348658cd718d18005de37b94f7f8561467 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Mon, 15 Apr 2024 15:11:10 +0200 Subject: [PATCH] Fix BIGINT validation (#11414) --- src/Tools/SchemaValidator.php | 40 ++++++++++--------- .../Tests/Models/BigIntegers/BigIntegers.php | 27 +++++++++++++ tests/Tests/ORM/Tools/SchemaValidatorTest.php | 14 +++++++ 3 files changed, 62 insertions(+), 19 deletions(-) create mode 100644 tests/Tests/Models/BigIntegers/BigIntegers.php diff --git a/src/Tools/SchemaValidator.php b/src/Tools/SchemaValidator.php index 1ea5e688fab..212ad019846 100644 --- a/src/Tools/SchemaValidator.php +++ b/src/Tools/SchemaValidator.php @@ -64,18 +64,18 @@ class SchemaValidator * It maps built-in Doctrine types to PHP types */ private const BUILTIN_TYPES_MAP = [ - AsciiStringType::class => 'string', - BigIntType::class => 'string', - BooleanType::class => 'bool', - DecimalType::class => 'string', - FloatType::class => 'float', - GuidType::class => 'string', - IntegerType::class => 'int', - JsonType::class => 'array', - SimpleArrayType::class => 'array', - SmallIntType::class => 'int', - StringType::class => 'string', - TextType::class => 'string', + AsciiStringType::class => ['string'], + BigIntType::class => ['int', 'string'], + BooleanType::class => ['bool'], + DecimalType::class => ['string'], + FloatType::class => ['float'], + GuidType::class => ['string'], + IntegerType::class => ['int'], + JsonType::class => ['array'], + SimpleArrayType::class => ['array'], + SmallIntType::class => ['int'], + StringType::class => ['string'], + TextType::class => ['string'], ]; public function __construct(EntityManagerInterface $em, bool $validatePropertyTypes = true) @@ -390,21 +390,21 @@ function (array $fieldMapping) use ($class): ?string { $propertyType = $propertyType->getName(); // If the property type is the same as the metadata field type, we are ok - if ($propertyType === $metadataFieldType) { + if (in_array($propertyType, $metadataFieldType, true)) { return null; } if (is_a($propertyType, BackedEnum::class, true)) { $backingType = (string) (new ReflectionEnum($propertyType))->getBackingType(); - if ($metadataFieldType !== $backingType) { + if (! in_array($backingType, $metadataFieldType, true)) { return sprintf( "The field '%s#%s' has the property type '%s' with a backing type of '%s' that differs from the metadata field type '%s'.", $class->name, $fieldName, $propertyType, $backingType, - $metadataFieldType + implode('|', $metadataFieldType) ); } @@ -429,7 +429,7 @@ function (array $fieldMapping) use ($class): ?string { ) { $backingType = (string) (new ReflectionEnum($fieldMapping['enumType']))->getBackingType(); - if ($metadataFieldType === $backingType) { + if (in_array($backingType, $metadataFieldType, true)) { return null; } @@ -439,7 +439,7 @@ function (array $fieldMapping) use ($class): ?string { $fieldName, $fieldMapping['enumType'], $backingType, - $metadataFieldType + implode('|', $metadataFieldType) ); } @@ -455,7 +455,7 @@ function (array $fieldMapping) use ($class): ?string { $class->name, $fieldName, $propertyType, - $metadataFieldType, + implode('|', $metadataFieldType), $fieldMapping['type'] ); }, @@ -468,8 +468,10 @@ function (array $fieldMapping) use ($class): ?string { /** * The exact DBAL type must be used (no subclasses), since consumers of doctrine/orm may have their own * customization around field types. + * + * @return list|null */ - private function findBuiltInType(Type $type): ?string + private function findBuiltInType(Type $type): ?array { $typeName = get_class($type); diff --git a/tests/Tests/Models/BigIntegers/BigIntegers.php b/tests/Tests/Models/BigIntegers/BigIntegers.php new file mode 100644 index 00000000000..16b6f58dd0e --- /dev/null +++ b/tests/Tests/Models/BigIntegers/BigIntegers.php @@ -0,0 +1,27 @@ +em->getClassMetadata(BigIntegers::class); + + self::assertSame( + ['The field \'Doctrine\Tests\Models\BigIntegers\BigIntegers#three\' has the property type \'float\' that differs from the metadata field type \'int|string\' returned by the \'bigint\' DBAL type.'], + $this->validator->validateClass($class) + ); + } } /** @MappedSuperclass */