Skip to content
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
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,17 @@ jobs:
tools: pecl, composer
extensions: intl, bcmath, curl, openssl, mbstring, pdo_sqlite, mongodb
ini-values: memory_limit=-1
- name: Run ${{ matrix.component }} tests
- name: Run ${{ matrix.component }} install
working-directory: src/${{ matrix.component }}
run: |
composer update
- name: PHP version tweaks
if: matrix.component == 'api-platform/metadata' && matrix.php != '8.1'
run: composer require symfony/type-info
working-directory: 'src/Metadata'
- name: Run ${{ matrix.component }} tests
working-directory: src/${{ matrix.component }}
run: |
mkdir -p build/logs/phpunit
vendor/bin/phpunit --log-junit "build/logs/phpunit/junit.xml" ${{ matrix.coverage && '--coverage-clover build/logs/phpunit/clover.xml' || '' }}
- name: Upload test artifacts
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@
"allow-plugins": {
"composer/package-versions-deprecated": true,
"phpstan/extension-installer": true,
"php-http/discovery": true
"php-http/discovery": true,
"soyuka/pmu": true
}
},
"extra": {
Expand Down
45 changes: 26 additions & 19 deletions src/Doctrine/Odm/PropertyInfo/DoctrineExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

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 @@ -22,7 +23,8 @@
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\PropertyInfo\Type as LegacyType;
use Symfony\Component\TypeInfo\Type;

/**
* Extracts data using Doctrine MongoDB ODM metadata.
Expand Down Expand Up @@ -53,9 +55,9 @@ public function getProperties($class, array $context = []): ?array
/**
* {@inheritdoc}
*
* @return Type[]|null
* @return LegacyType[]|null
*/
public function getTypes($class, $property, array $context = []): ?array
public function getTypes(string $class, string $property, array $context = []): ?array
{
if (null === $metadata = $this->getMetadata($class)) {
return null;
Expand All @@ -72,19 +74,19 @@ public function getTypes($class, $property, array $context = []): ?array
if ($metadata->isSingleValuedAssociation($property)) {
$nullable = $metadata instanceof MongoDbClassMetadata && $metadata->isNullable($property);

return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $class)];
return [new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, $nullable, $class)];
}

$collectionKeyType = Type::BUILTIN_TYPE_INT;
$collectionKeyType = LegacyType::BUILTIN_TYPE_INT;

return [
new Type(
Type::BUILTIN_TYPE_OBJECT,
new LegacyType(
LegacyType::BUILTIN_TYPE_OBJECT,
false,
Collection::class,
true,
new Type($collectionKeyType),
new Type(Type::BUILTIN_TYPE_OBJECT, false, $class)
new LegacyType($collectionKeyType),
new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, $class)
),
];
}
Expand All @@ -94,18 +96,18 @@ public function getTypes($class, $property, array $context = []): ?array
$nullable = $metadata instanceof MongoDbClassMetadata && $metadata->isNullable($property);
$enumType = null;
if (null !== $enumClass = $metadata instanceof MongoDbClassMetadata ? $metadata->getFieldMapping($property)['enumType'] ?? null : null) {
$enumType = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $enumClass);
$enumType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, $nullable, $enumClass);
}

switch ($typeOfField) {
case MongoDbType::DATE:
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, \DateTime::class)];
return [new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, $nullable, \DateTime::class)];
case MongoDbType::DATE_IMMUTABLE:
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, \DateTimeImmutable::class)];
return [new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, $nullable, \DateTimeImmutable::class)];
case MongoDbType::HASH:
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
return [new LegacyType(LegacyType::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
case MongoDbType::COLLECTION:
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT))];
return [new LegacyType(LegacyType::BUILTIN_TYPE_ARRAY, $nullable, null, true, new LegacyType(LegacyType::BUILTIN_TYPE_INT))];
case MongoDbType::INT:
case MongoDbType::STRING:
if ($enumType) {
Expand All @@ -115,7 +117,7 @@ public function getTypes($class, $property, array $context = []): ?array

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

return $builtinType ? [new Type($builtinType, $nullable)] : null;
return $builtinType ? [new LegacyType($builtinType, $nullable)] : null;
}

return null;
Expand Down Expand Up @@ -154,16 +156,21 @@ private function getMetadata(string $class): ?ClassMetadata
}
}

public function getType(string $class, string $property, array $context = []): ?Type
{
return PropertyInfoToTypeInfoHelper::convertLegacyTypesToType($this->getTypes($class, $property, $context));
}

/**
* Gets the corresponding built-in PHP type.
*/
private function getPhpType(string $doctrineType): ?string
{
return match ($doctrineType) {
MongoDbType::INTEGER, MongoDbType::INT, MongoDbType::INTID, MongoDbType::KEY => Type::BUILTIN_TYPE_INT,
MongoDbType::FLOAT => Type::BUILTIN_TYPE_FLOAT,
MongoDbType::STRING, MongoDbType::ID, MongoDbType::OBJECTID, MongoDbType::TIMESTAMP, MongoDbType::BINDATA, MongoDbType::BINDATABYTEARRAY, MongoDbType::BINDATACUSTOM, MongoDbType::BINDATAFUNC, MongoDbType::BINDATAMD5, MongoDbType::BINDATAUUID, MongoDbType::BINDATAUUIDRFC4122 => Type::BUILTIN_TYPE_STRING,
MongoDbType::BOOLEAN, MongoDbType::BOOL => Type::BUILTIN_TYPE_BOOL,
MongoDbType::INTEGER, MongoDbType::INT, MongoDbType::INTID, MongoDbType::KEY => LegacyType::BUILTIN_TYPE_INT,
MongoDbType::FLOAT => LegacyType::BUILTIN_TYPE_FLOAT,
MongoDbType::STRING, MongoDbType::ID, MongoDbType::OBJECTID, MongoDbType::TIMESTAMP, MongoDbType::BINDATA, MongoDbType::BINDATABYTEARRAY, MongoDbType::BINDATACUSTOM, MongoDbType::BINDATAFUNC, MongoDbType::BINDATAMD5, MongoDbType::BINDATAUUID, MongoDbType::BINDATAUUIDRFC4122 => LegacyType::BUILTIN_TYPE_STRING,
MongoDbType::BOOLEAN, MongoDbType::BOOL => LegacyType::BUILTIN_TYPE_BOOL,
default => null,
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/GraphQl/Serializer/SerializerContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApiPlatform\Metadata\GraphQl\Subscription;
use GraphQL\Type\Definition\ResolveInfo;
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;

/**
Expand Down Expand Up @@ -101,7 +102,7 @@ private function denormalizePropertyName(string $property, ?string $resourceClas
if (null === $this->nameConverter) {
return $property;
}
if ($this->nameConverter instanceof AdvancedNameConverterInterface) {
if ($this->nameConverter instanceof AdvancedNameConverterInterface || $this->nameConverter instanceof MetadataAwareNameConverter) {
return $this->nameConverter->denormalize($property, $resourceClass, null, $context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@

namespace ApiPlatform\GraphQl\Tests\Fixtures\Serializer\NameConverter;

use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;

/**
* Custom converter that will only convert a property named "nameConverted"
* with the same logic as Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter.
*/
class CustomConverter implements NameConverterInterface
class CustomConverter implements AdvancedNameConverterInterface
{
private NameConverterInterface $nameConverter;

Expand All @@ -29,12 +30,12 @@ public function __construct()
$this->nameConverter = new CamelCaseToSnakeCaseNameConverter();
}

public function normalize(string $propertyName): string
public function normalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
{
return 'nameConverted' === $propertyName ? $this->nameConverter->normalize($propertyName) : $propertyName;
}

public function denormalize(string $propertyName): string
public function denormalize(string $propertyName, ?string $class = null, ?string $format = null, array $context = []): string
{
return 'name_converted' === $propertyName ? $this->nameConverter->denormalize($propertyName) : $propertyName;
}
Expand Down
3 changes: 2 additions & 1 deletion src/GraphQl/Type/FieldsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;

/**
Expand Down Expand Up @@ -572,7 +573,7 @@ private function normalizePropertyName(string $property, string $resourceClass):
if (null === $this->nameConverter) {
return $property;
}
if ($this->nameConverter instanceof AdvancedNameConverterInterface) {
if ($this->nameConverter instanceof AdvancedNameConverterInterface || $this->nameConverter instanceof MetadataAwareNameConverter) {
return $this->nameConverter->normalize($property, $resourceClass);
}

Expand Down
78 changes: 78 additions & 0 deletions src/Metadata/Tests/Util/PropertyInfoToTypeInfoHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ApiPlatform\Metadata\Tests\Util;

use ApiPlatform\Metadata\Util\PropertyInfoToTypeInfoHelper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Type as LegacyType;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\TypeIdentifier;

/**
* @group legacy
*/
class PropertyInfoToTypeInfoHelperTest extends TestCase
{
/**
* @dataProvider convertLegacyTypesToTypeDataProvider
*
* @param list<LegacyType>|null $legacyTypes
*/
public function testConvertLegacyTypesToType(?Type $type, ?array $legacyTypes): void
{
if (!class_exists(Type::class)) {
$this->markTestSkipped('symfony/type-info requires PHP > 8.2');
}

$this->assertEquals($type, PropertyInfoToTypeInfoHelper::convertLegacyTypesToType($legacyTypes));
}

/**
* @return iterable<array{0: ?Type, 1: list<LegacyType>|null}>
*/
public function convertLegacyTypesToTypeDataProvider(): iterable
{
if (!class_exists(Type::class)) {
return;
}

yield [null, null];
yield [Type::null(), [new LegacyType('null')]];
// yield [Type::void(), [new LegacyType('void')]];
yield [Type::int(), [new LegacyType('int')]];
yield [Type::object(\stdClass::class), [new LegacyType('object', false, \stdClass::class)]];
yield [
Type::generic(Type::object('Foo'), Type::string(), Type::int()), // @phpstan-ignore-line
[new LegacyType('object', false, 'Foo', false, [new LegacyType('string')], new LegacyType('int'))],
];
yield [Type::nullable(Type::int()), [new LegacyType('int', true)]]; // @phpstan-ignore-line
yield [Type::union(Type::int(), Type::string()), [new LegacyType('int'), new LegacyType('string')]];
yield [
Type::union(Type::int(), Type::string(), Type::null()),
[new LegacyType('int', true), new LegacyType('string', true)],
];

$type = Type::collection(Type::builtin(TypeIdentifier::ARRAY), Type::int(), Type::string()); // @phpstan-ignore-line
yield [$type, [new LegacyType('array', false, null, true, [new LegacyType('string')], new LegacyType('int'))]];
}
}
Loading