Skip to content

feat(json-schema): use TypeInfo's Type #6

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

Closed
Closed
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
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',
'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
{
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;
}

$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.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
{
// 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);

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

/**
* Gets the corresponding built-in PHP type.
*/
private function getPhpType(string $doctrineType): ?string
private function getPhpTypeLegacy(string $doctrineType): ?string
{
return match ($doctrineType) {
MongoDbType::INTEGER, MongoDbType::INT, MongoDbType::INTID, MongoDbType::KEY => LegacyType::BUILTIN_TYPE_INT,
Expand Down
Loading
Loading