Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
use ApiPlatform\Metadata\Util\TypeHelper;
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
use ApiPlatform\Serializer\Filter\FilterInterface as SerializerFilterInterface;
use ApiPlatform\State\Parameter\ValueCaster;
Expand Down Expand Up @@ -356,7 +357,7 @@ private function getDefaultParameters(Operation $operation, string $resourceClas
}
}

if (null === $parameter->getCastFn() && ($nativeType = $parameter->getNativeType()) && ValueCaster::resolveEnumType($nativeType)) {
if (null === $parameter->getCastFn() && ($nativeType = $parameter->getNativeType()) && TypeHelper::resolveEnumType($nativeType)) {
$parameter = $parameter->withCastFn([ValueCaster::class, 'toBackedEnum']);
if (null === $parameter->getCastToNativeType()) {
$parameter = $parameter->withCastToNativeType(true);
Expand Down Expand Up @@ -406,7 +407,7 @@ private function addFilterMetadata(Parameter $parameter): Parameter

private function addNativeTypeSchema(Parameter $parameter): Parameter
{
if (!($nativeType = $parameter->getNativeType()) || !($enumType = ValueCaster::resolveEnumType($nativeType))) {
if (!($nativeType = $parameter->getNativeType()) || !($enumType = TypeHelper::resolveEnumType($nativeType))) {
return $parameter;
}

Expand Down
28 changes: 28 additions & 0 deletions src/Metadata/Util/TypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\Type\CollectionType;
use Symfony\Component\TypeInfo\Type\EnumType;
use Symfony\Component\TypeInfo\Type\NullableType;
use Symfony\Component\TypeInfo\Type\ObjectType;
use Symfony\Component\TypeInfo\Type\UnionType;

/**
* @internal
Expand Down Expand Up @@ -52,4 +55,29 @@ public static function getClassName(Type $type): ?string

return null;
}

public static function resolveEnumType(Type $type): ?EnumType
{
if ($type instanceof EnumType) {
return $type;
}

if ($type instanceof CollectionType) {
return self::resolveEnumType($type->getCollectionValueType());
}

if ($type instanceof NullableType) {
return self::resolveEnumType($type->getWrappedType());
}

if ($type instanceof UnionType) {
foreach ($type->getTypes() as $t) {
if ($enum = self::resolveEnumType($t)) {
return $enum;
}
}
}

return null;
}
}
33 changes: 2 additions & 31 deletions src/State/Parameter/ValueCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@
namespace ApiPlatform\State\Parameter;

use ApiPlatform\Metadata\Parameter;
use Symfony\Component\TypeInfo\Type;
use ApiPlatform\Metadata\Util\TypeHelper;
use Symfony\Component\TypeInfo\Type\BackedEnumType;
use Symfony\Component\TypeInfo\Type\CollectionType;
use Symfony\Component\TypeInfo\Type\EnumType;
use Symfony\Component\TypeInfo\Type\NullableType;
use Symfony\Component\TypeInfo\Type\UnionType;
use Symfony\Component\TypeInfo\TypeIdentifier;

/**
Expand Down Expand Up @@ -84,7 +80,7 @@ public static function toDateTime(mixed $v): mixed

public static function toBackedEnum(mixed $v, ?Parameter $parameter = null): mixed
{
$type = null !== $parameter?->getNativeType() ? self::resolveEnumType($parameter->getNativeType()) : null;
$type = null !== $parameter?->getNativeType() ? TypeHelper::resolveEnumType($parameter->getNativeType()) : null;
if (!$type instanceof BackedEnumType || (!\is_string($v) && !\is_int($v))) {
return $v;
}
Expand All @@ -101,29 +97,4 @@ public static function toBackedEnum(mixed $v, ?Parameter $parameter = null): mix

return $class::tryFrom($value) ?? $v;
}

public static function resolveEnumType(Type $type): ?EnumType
{
if ($type instanceof EnumType) {
return $type;
}

if ($type instanceof CollectionType) {
return self::resolveEnumType($type->getCollectionValueType());
}

if ($type instanceof NullableType) {
return self::resolveEnumType($type->getWrappedType());
}

if ($type instanceof UnionType) {
foreach ($type->getTypes() as $t) {
if ($enum = self::resolveEnumType($t)) {
return $enum;
}
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use ApiPlatform\Metadata\Util\TypeHelper;
use ApiPlatform\State\Parameter\ValueCaster;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\TypeInfo\Type\BackedEnumType;

Expand Down Expand Up @@ -231,7 +230,7 @@ private function resolveEnumClass(string $resourceClass, string $property): ?str
return null;
}

$enumType = null !== $type ? ValueCaster::resolveEnumType($type) : null;
$enumType = null !== $type ? TypeHelper::resolveEnumType($type) : null;

return $enumType instanceof BackedEnumType ? $enumType->getClassName() : null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Validator/Util/ParameterValidationConstraints.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
namespace ApiPlatform\Validator\Util;

use ApiPlatform\Metadata\Parameter;
use ApiPlatform\Metadata\Util\TypeHelper;
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
use ApiPlatform\State\Parameter\ValueCaster;
use Symfony\Component\TypeInfo\Type\BackedEnumType;
use Symfony\Component\TypeInfo\Type\CollectionType;
use Symfony\Component\TypeInfo\Type\UnionType;
Expand Down Expand Up @@ -101,7 +101,7 @@ public static function getParameterValidationConstraints(Parameter $parameter, ?
}

$nativeType = $parameter->getNativeType();
$enumType = $nativeType && $parameter->getCastToNativeType() ? ValueCaster::resolveEnumType($nativeType) : null;
$enumType = $nativeType && $parameter->getCastToNativeType() ? TypeHelper::resolveEnumType($nativeType) : null;
$castsToEnum = $enumType instanceof BackedEnumType;

if ($castsToEnum) {
Expand Down
Loading