Skip to content

Commit

Permalink
feat(metadata/doctrine): use TypeInfo's Type
Browse files Browse the repository at this point in the history
  • Loading branch information
mtarld committed Feb 22, 2025
1 parent ad54075 commit d8618a7
Show file tree
Hide file tree
Showing 46 changed files with 726 additions and 221 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@
"symfony/http-foundation": "^6.4 || ^7.0",
"symfony/http-kernel": "^6.4 || ^7.0",
"symfony/property-access": "^6.4 || ^7.0",
"symfony/property-info": "^6.4 || ^7.0",
"symfony/property-info": "^7.1",
"symfony/serializer": "^6.4 || ^7.0",
"symfony/translation-contracts": "^3.3",
"symfony/type-info": "^7.2",
"symfony/web-link": "^6.4 || ^7.0",
"willdurand/negotiation": "^3.1"
},
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": "^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
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 @@ public function getDescription(string $resourceClass): array
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
2 changes: 1 addition & 1 deletion src/Doctrine/Common/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
],
"require": {
"php": ">=8.2",
"api-platform/metadata": "^3.4 || ^4.0",
"api-platform/metadata": "^4.1",
"api-platform/state": "^3.4 || ^4.0",
"doctrine/collections": "^2.1",
"doctrine/common": "^3.2.2",
Expand Down
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 @@ public function getProperties($class, array $context = []): ?array
return $metadata->getFieldNames();
}

public function getType($class, $property, array $context = []): ?Type

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L55 was not covered by tests
{
if (null === $metadata = $this->getMetadata($class)) {
return null;

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

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php#L57-L58

Added lines #L57 - L58 were not covered by tests
}

if ($metadata->hasAssociation($property)) {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L61 was not covered by tests
/** @var class-string|null */
$class = $metadata->getAssociationTargetClass($property);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L63 was not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php#L65-L66

Added lines #L65 - L66 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php#L69-L70

Added lines #L69 - L70 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L72 was not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L75 was not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php#L78-L79

Added lines #L78 - L79 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L82 was not covered by tests

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#L84-L85

Added lines #L84 - L85 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php#L88-L89

Added lines #L88 - L89 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php#L91-L92

Added lines #L91 - L92 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L95 was not covered by tests

$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,
};

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

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php#L97-L104

Added lines #L97 - L104 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L106 was not covered by tests
}

/**
* {@inheritdoc}
*
* // deprecated since 4.1, 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.1', 'The "%s()" method is deprecated, use "%s::getType()" instead.', __METHOD__, self::class);

if (null === $metadata = $this->getMetadata($class)) {
return null;
}
Expand Down Expand Up @@ -115,7 +173,7 @@ public function getTypes(string $class, string $property, array $context = []):
}
}

$builtinType = $this->getPhpType($typeOfField);
$builtinType = $this->getPhpTypeLegacy($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 @@ private function getMetadata(string $class): ?ClassMetadata
}
}

public function getType(string $class, string $property, array $context = []): ?Type
/**
* Gets the corresponding built-in PHP type identifier.
*/
private function getTypeIdentifier(string $doctrineType): ?TypeIdentifier

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L220 was not covered by tests
{
return PropertyInfoToTypeInfoHelper::convertLegacyTypesToType($this->getTypes($class, $property, $context));
return match ($doctrineType) {
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#L222-L230

Added lines #L222 - L230 were not covered by tests
}

/**
* Gets the corresponding built-in PHP type.
*/
private function getPhpType(string $doctrineType): ?string
private function getPhpTypeLegacy(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

0 comments on commit d8618a7

Please sign in to comment.