Skip to content

feat(metadata/doctrine): Use Type of TypeInfo instead of PropertyInfo #6979

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

Merged
merged 4 commits into from
Apr 16, 2025
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
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@
"symfony/property-info": "^6.4 || ^7.1",
"symfony/serializer": "^6.4 || ^7.0",
"symfony/translation-contracts": "^3.3",
"symfony/web-link": "^6.4 || ^7.1",
"symfony/type-info": "^7.3-dev",
"symfony/web-link": "^6.4 || ^7.0",
"willdurand/negotiation": "^3.1"
},
"require-dev": {
Expand Down Expand Up @@ -164,7 +165,7 @@
"symfony/console": "^6.4 || ^7.0",
"symfony/css-selector": "^6.4 || ^7.0",
"symfony/dependency-injection": "^6.4 || ^7.0",
"symfony/doctrine-bridge": "^6.4.2 || ^7.0.2",
"symfony/doctrine-bridge": "^6.4.2 || ^7.1",
"symfony/dom-crawler": "^6.4 || ^7.0",
"symfony/error-handler": "^6.4 || ^7.0",
"symfony/event-dispatcher": "^6.4 || ^7.0",
Expand Down Expand Up @@ -208,5 +209,11 @@
"symfony/web-profiler-bundle": "To use the data collector.",
"webonyx/graphql-php": "To support GraphQL."
},
"type": "library"
"type": "library",
"repositories": [
{
"type": "vcs",
"url": "https://github.com/symfony/type-info"
}
]
}
9 changes: 8 additions & 1 deletion docs/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"symfony/property-info": "^7.0",
"symfony/runtime": "^7.0",
"symfony/security-bundle": "^7.0",
"symfony/type-info": "^7.3-dev",
"symfony/serializer": "^7.0",
"symfony/validator": "^7.0",
"symfony/yaml": "^7.0",
Expand All @@ -50,5 +51,11 @@
"name": "api-platform/api-platform",
"url": "https://github.com/api-platform/api-platform"
}
}
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/symfony/type-info"
}
]
}
3 changes: 1 addition & 2 deletions docs/guides/create-a-custom-doctrine-filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Operation;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\PropertyInfo\Type;

final class RegexpFilter extends AbstractFilter
{
Expand Down Expand Up @@ -67,7 +66,7 @@
foreach ($this->properties as $property => $strategy) {
$description["regexp_$property"] = [
'property' => $property,
'type' => Type::BUILTIN_TYPE_STRING,
'type' => 'string',

Check warning on line 69 in docs/guides/create-a-custom-doctrine-filter.php

View check run for this annotation

Codecov / codecov/patch

docs/guides/create-a-custom-doctrine-filter.php#L69

Added line #L69 was not covered by tests
'required' => false,
'description' => 'Filter using a regex. This will appear in the OpenAPI documentation!',
'openapi' => [
Expand Down
11 changes: 9 additions & 2 deletions src/Doctrine/Common/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"doctrine/mongodb-odm": "^2.10",
"doctrine/orm": "^2.17 || ^3.0",
"phpspec/prophecy-phpunit": "^2.2",
"phpunit/phpunit": "^11.2"
"phpunit/phpunit": "^11.2",
"symfony/type-info": "^7.3-dev"
},
"conflict": {
"doctrine/persistence": "<1.3"
Expand Down Expand Up @@ -73,5 +74,11 @@
},
"scripts": {
"test": "./vendor/bin/phpunit"
}
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/symfony/type-info"
}
]
}
84 changes: 75 additions & 9 deletions src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace ApiPlatform\Doctrine\Odm\PropertyInfo;

use ApiPlatform\Metadata\Util\PropertyInfoToTypeInfoHelper;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as MongoDbClassMetadata;
use Doctrine\ODM\MongoDB\Types\Type as MongoDbType;
Expand All @@ -25,6 +24,7 @@
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\PropertyInfo\Type as LegacyType;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\TypeIdentifier;

/**
* Extracts data using Doctrine MongoDB ODM metadata.
Expand Down Expand Up @@ -52,13 +52,71 @@
return $metadata->getFieldNames();
}

public function getType(string $class, string $property, array $context = []): ?Type
{
if (null === $metadata = $this->getMetadata($class)) {
return null;
}

if ($metadata->hasAssociation($property)) {
/** @var class-string|null */
$class = $metadata->getAssociationTargetClass($property);

if (null === $class) {
return null;
}

if ($metadata->isSingleValuedAssociation($property)) {
$nullable = $metadata instanceof MongoDbClassMetadata && $metadata->isNullable($property);

return $nullable ? Type::nullable(Type::object($class)) : Type::object($class);
}

return Type::collection(Type::object(Collection::class), Type::object($class), Type::int());
}

if (!$metadata->hasField($property)) {
return null;
}

$typeOfField = $metadata->getTypeOfField($property);

if (!$typeIdentifier = $this->getTypeIdentifier($typeOfField)) {
return null;

Check warning on line 85 in src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php#L85

Added line #L85 was not covered by tests
}

$nullable = $metadata instanceof MongoDbClassMetadata && $metadata->isNullable($property);
$enumType = null;

if (null !== $enumClass = $metadata instanceof MongoDbClassMetadata ? $metadata->getFieldMapping($property)['enumType'] ?? null : null) {
$enumType = $nullable ? Type::nullable(Type::enum($enumClass)) : Type::enum($enumClass);
}

$builtinType = $nullable ? Type::nullable(Type::builtin($typeIdentifier)) : Type::builtin($typeIdentifier);

$type = match ($typeOfField) {
MongoDbType::DATE => Type::object(\DateTime::class),
MongoDbType::DATE_IMMUTABLE => Type::object(\DateTimeImmutable::class),
MongoDbType::HASH => Type::array(),
MongoDbType::COLLECTION => Type::list(),
MongoDbType::INT, MongoDbType::INTEGER, MongoDbType::STRING => $enumType ? $enumType : $builtinType,
default => $builtinType,
};

return $nullable ? Type::nullable($type) : $type;
}

/**
* {@inheritdoc}
*
* // deprecated since 4.2 use "getType" instead
*
* @return LegacyType[]|null
*/
public function getTypes(string $class, string $property, array $context = []): ?array
public function getTypes($class, $property, array $context = []): ?array

Check warning on line 116 in src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php#L116

Added line #L116 was not covered by tests
{
trigger_deprecation('api-platform/core', '4.2', 'The "%s()" method is deprecated, use "%s::getType()" instead.', __METHOD__, self::class);

Check warning on line 118 in src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php#L118

Added line #L118 was not covered by tests

if (null === $metadata = $this->getMetadata($class)) {
return null;
}
Expand Down Expand Up @@ -115,7 +173,7 @@
}
}

$builtinType = $this->getPhpType($typeOfField);
$builtinType = $this->getNativeTypeLegacy($typeOfField);

Check warning on line 176 in src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php#L176

Added line #L176 was not covered by tests

return $builtinType ? [new LegacyType($builtinType, $nullable)] : null;
}
Expand Down Expand Up @@ -156,15 +214,23 @@
}
}

public function getType(string $class, string $property, array $context = []): ?Type
/**
* Gets the corresponding built-in PHP type identifier.
*/
private function getTypeIdentifier(string $doctrineType): ?TypeIdentifier
{
return PropertyInfoToTypeInfoHelper::convertLegacyTypesToType($this->getTypes($class, $property, $context));
return match ($doctrineType) {

Check warning on line 222 in src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php#L222

Added line #L222 was not covered by tests
MongoDbType::INTEGER, MongoDbType::INT, MongoDbType::INTID, MongoDbType::KEY => TypeIdentifier::INT,
MongoDbType::FLOAT => TypeIdentifier::FLOAT,
MongoDbType::STRING, MongoDbType::ID, MongoDbType::OBJECTID, MongoDbType::TIMESTAMP, MongoDbType::BINDATA, MongoDbType::BINDATABYTEARRAY, MongoDbType::BINDATACUSTOM, MongoDbType::BINDATAFUNC, MongoDbType::BINDATAMD5, MongoDbType::BINDATAUUID, MongoDbType::BINDATAUUIDRFC4122 => TypeIdentifier::STRING,
MongoDbType::BOOLEAN, MongoDbType::BOOL => TypeIdentifier::BOOL,
MongoDbType::DATE, MongoDbType::DATE_IMMUTABLE => TypeIdentifier::OBJECT,
MongoDbType::HASH, MongoDbType::COLLECTION => TypeIdentifier::ARRAY,
default => null,
};

Check warning on line 230 in src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php#L230

Added line #L230 was not covered by tests
}

/**
* Gets the corresponding built-in PHP type.
*/
private function getPhpType(string $doctrineType): ?string
private function getNativeTypeLegacy(string $doctrineType): ?string

Check warning on line 233 in src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php#L233

Added line #L233 was not covered by tests
{
return match ($doctrineType) {
MongoDbType::INTEGER, MongoDbType::INT, MongoDbType::INTID, MongoDbType::KEY => LegacyType::BUILTIN_TYPE_INT,
Expand Down
Loading
Loading