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
4 changes: 0 additions & 4 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ parameters:
- "#Call to function method_exists\\(\\) with Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\ClassMetadata\\|Doctrine\\\\ORM\\\\Mapping\\\\ClassMetadata and 'isChangeTrackingDef…' will always evaluate to true\\.#"
- "#Call to function method_exists\\(\\) with Symfony\\\\Component\\\\Serializer\\\\Exception\\\\PartialDenormalizationException and 'getNotNormalizableV…' will always evaluate to true\\.#"

# See https://github.com/phpstan/phpstan-symfony/issues/27
-
message: '#^Service "[^"]+" is private.$#'
path: src


# Allow extra assertions in tests: https://github.com/phpstan/phpstan-strict-rules/issues/130
Expand Down
4 changes: 2 additions & 2 deletions src/Hal/Tests/Serializer/CollectionNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ private function normalizePaginator(bool $partial = false): array
$paginator->method('current')->willReturn('foo'); // @phpstan-ignore-line

if (!$partial) {
$paginator->method('getLastPage')->willReturn(7.); // @phpstan-ignore-line
$paginator->method('getTotalItems')->willReturn(1312.); // @phpstan-ignore-line
$paginator->method('getLastPage')->willReturn(7.);
$paginator->method('getTotalItems')->willReturn(1312.);
} else {
$paginator->method('count')->willReturn(12);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function setUp(): void
);
}

$definitionNameFactory = new DefinitionNameFactory(null);
$definitionNameFactory = new DefinitionNameFactory();

$baseSchemaFactory = new BaseSchemaFactory(
resourceMetadataFactory: $resourceMetadataFactory->reveal(),
Expand Down
6 changes: 3 additions & 3 deletions src/JsonApi/Tests/JsonSchema/SchemaFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function setUp(): void
$propertyNameCollectionFactory->create(Dummy::class, ['enable_getter_setter_extraction' => true, 'schema_type' => Schema::TYPE_INPUT])->willReturn(new PropertyNameCollection());
$propertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);

$definitionNameFactory = new DefinitionNameFactory(null);
$definitionNameFactory = new DefinitionNameFactory();

$baseSchemaFactory = new BaseSchemaFactory(
resourceMetadataFactory: $resourceMetadataFactory->reveal(),
Expand Down Expand Up @@ -316,7 +316,7 @@ private function buildSchemaFactoryWithPolymorphicRelation(): SchemaFactory
$resourceClassResolver->isResourceClass(RelatedDummy::class)->willReturn(true);
$resourceClassResolver->isResourceClass(OtherRelatedDummy::class)->willReturn(true);

$definitionNameFactory = new DefinitionNameFactory(null);
$definitionNameFactory = new DefinitionNameFactory();

$baseSchemaFactory = new BaseSchemaFactory(
resourceMetadataFactory: $resourceMetadataFactory->reveal(),
Expand Down Expand Up @@ -377,7 +377,7 @@ private function buildSchemaFactoryWithRelation(): SchemaFactory
$resourceClassResolver->isResourceClass(Dummy::class)->willReturn(true);
$resourceClassResolver->isResourceClass(RelatedDummy::class)->willReturn(true);

$definitionNameFactory = new DefinitionNameFactory(null);
$definitionNameFactory = new DefinitionNameFactory();

$baseSchemaFactory = new BaseSchemaFactory(
resourceMetadataFactory: $resourceMetadataFactory->reveal(),
Expand Down
12 changes: 7 additions & 5 deletions src/JsonLd/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,15 @@ private function resolveType(string $resourceClass, bool $isResourceClass, array

$types = $operation instanceof HttpOperation ? $operation->getTypes() : null;
if (null === $types) {
if (isset($context['item_uri_template'])) {
// The members carry the item resource's @type to match their @id, which dereferences
// to the item_uri_template operation rather than to the collection's own resource.
$typeClass = $isResourceClass ? $resourceClass : ($operation->getClass() ?? $resourceClass);
if (isset($context['item_uri_template']) || $operation->getClass() === $typeClass) {
// The operation serves the class being normalized: use its shortName so @type matches the
// (possibly deduplicated) @context. For item_uri_template, $resourceClass is the collection
// resource, so the operation remains authoritative for the item type.
$types = [$operation->getShortName()];
} else {
// Use resource-level shortName to avoid operation-specific overrides
$typeClass = $isResourceClass ? $resourceClass : ($operation->getClass() ?? $resourceClass);
// Embedded/related resource: the operation belongs to another class, so fall back to its
// resource-level shortName instead of an operation-specific override.
try {
$types = [$this->resourceMetadataCollectionFactory->create($typeClass)[0]->getShortName()];
} catch (\Exception) {
Expand Down
12 changes: 1 addition & 11 deletions src/JsonSchema/DefinitionNameFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ final class DefinitionNameFactory implements DefinitionNameFactoryInterface

private array $prefixCache = [];

public function __construct(private ?array $distinctFormats = null)
{
if ($distinctFormats) {
trigger_deprecation('api-platform/json-schema', '4.2', 'The distinctFormats argument is deprecated and will be removed in 5.0.');
}
}

public function create(string $className, string $format = 'json', ?string $inputOrOutputClass = null, ?Operation $operation = null, array $serializerContext = []): string
{
if ($operation) {
Expand All @@ -50,10 +43,7 @@ public function create(string $className, string $format = 'json', ?string $inpu
$prefix .= self::GLUE.$this->createPrefixFromClass($inputOrOutputClass);
}

// TODO: remove in 5.0
$v = $this->distinctFormats ? ($this->distinctFormats[$format] ?? false) : true;

if (!\in_array($format, ['json', 'merge-patch+json'], true) && $v) {
if (!\in_array($format, ['json', 'merge-patch+json'], true)) {
// JSON is the default, and so isn't included in the definition name
// JSON merge patch is postfixed at the end
$prefix .= self::GLUE.$format;
Expand Down
4 changes: 2 additions & 2 deletions src/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareI
// Edge case where the related resource is not readable (for example: NotExposed) but we have groups to read the whole related object
public const OPENAPI_DEFINITION_NAME = 'openapi_definition_name';

public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly ?NameConverterInterface $nameConverter = null, ?ResourceClassResolverInterface $resourceClassResolver = null, ?array $distinctFormats = null, private ?DefinitionNameFactoryInterface $definitionNameFactory = null)
public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly ?NameConverterInterface $nameConverter = null, ?ResourceClassResolverInterface $resourceClassResolver = null, private ?DefinitionNameFactoryInterface $definitionNameFactory = null)
{
if (!$definitionNameFactory) {
$this->definitionNameFactory = new DefinitionNameFactory($distinctFormats);
$this->definitionNameFactory = new DefinitionNameFactory();
}

$this->resourceMetadataFactory = $resourceMetadataFactory;
Expand Down
4 changes: 0 additions & 4 deletions src/Laravel/ApiPlatformProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -935,16 +935,12 @@ public function register(): void
});

$this->app->singleton(SchemaFactory::class, static function (Application $app) {
/** @var ConfigRepository */
$config = $app['config'];

return new SchemaFactory(
$app->make(ResourceMetadataCollectionFactoryInterface::class),
$app->make(PropertyNameCollectionFactoryInterface::class),
$app->make(PropertyMetadataFactoryInterface::class),
$app->make(NameConverterInterface::class),
$app->make(ResourceClassResolverInterface::class),
$config->get('api-platform.formats'),
$app->make(DefinitionNameFactoryInterface::class),
);
});
Expand Down
11 changes: 11 additions & 0 deletions src/Laravel/Controller/ApiPlatformController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\State\ProviderInterface;
use ApiPlatform\State\SerializerContextBuilderInterface;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Event;
Expand Down Expand Up @@ -77,6 +78,16 @@ public function __invoke(Request $request): Response
$operation = $operation->withDeserialize(\in_array($operation->getMethod(), ['POST', 'PUT', 'PATCH'], true));
}

$denormalizationContext = $operation->getDenormalizationContext() ?? [];
if ($operation->canDeserialize() && !isset($denormalizationContext[SerializerContextBuilderInterface::ASSIGN_OBJECT_TO_POPULATE])) {
$method = $operation->getMethod();
$assignObjectToPopulate = 'POST' === $method
|| 'PATCH' === $method
|| ('PUT' === $method && !($operation->getExtraProperties()['standard_put'] ?? true));

$operation = $operation->withDenormalizationContext($denormalizationContext + [SerializerContextBuilderInterface::ASSIGN_OBJECT_TO_POPULATE => $assignObjectToPopulate]);
}

$body = $this->provider->provide($operation, $uriVariables, $context);

// The provider can change the Operation, extract it again from the Request attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ private function hasSameOperation(ApiResource $resource, string $operationClass,
*/
private function deduplicateShortNames(array $resources): array
{
$enabled = $this->defaults['extra_properties']['deduplicate_resource_short_names'] ?? false;
$shortNameCounts = [];

foreach ($resources as $index => $resource) {
Expand All @@ -249,14 +248,6 @@ private function deduplicateShortNames(array $resources): array
continue;
}

if (!$enabled) {
if (1 === $shortNameCounts[$shortName]) {
trigger_deprecation('api-platform/core', '4.2', 'Having multiple "#[ApiResource]" attributes with the same "shortName" "%s" on class "%s" is deprecated and will result in automatic short name deduplication in API Platform 5.x. Set "defaults.extra_properties.deduplicate_resource_short_names" to "true" in the API Platform configuration to enable it now.', $shortName, $resource->getClass());
}
++$shortNameCounts[$shortName];
continue;
}

$newShortName = $shortName.(++$shortNameCounts[$shortName]);
$resource = $resource->withShortName($newShortName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ class: AttributeResource::class,
graphQlOperations: $this->getDefaultGraphqlOperations('AttributeResource', AttributeResource::class, AttributeResourceProvider::class)
),
new ApiResource(
shortName: 'AttributeResource',
shortName: 'AttributeResource2',
class: AttributeResource::class,
uriTemplate: '/dummy/{dummyId}/attribute_resources/{identifier}{._format}',
operations: [
'_api_/dummy/{dummyId}/attribute_resources/{identifier}{._format}_get' => new Get(
class: AttributeResource::class,
uriTemplate: '/dummy/{dummyId}/attribute_resources/{identifier}{._format}',
shortName: 'AttributeResource',
shortName: 'AttributeResource2',
inputFormats: ['json' => ['application/merge-patch+json']],
priority: 4,
status: 301,
Expand All @@ -116,7 +116,7 @@ class: AttributeResource::class,
'_api_/dummy/{dummyId}/attribute_resources/{identifier}{._format}_patch' => new Patch(
class: AttributeResource::class,
uriTemplate: '/dummy/{dummyId}/attribute_resources/{identifier}{._format}',
shortName: 'AttributeResource',
shortName: 'AttributeResource2',
inputFormats: ['json' => ['application/merge-patch+json']],
priority: 5,
status: 301,
Expand Down Expand Up @@ -272,11 +272,9 @@ public function testNameDeclarationShouldNotBeRemoved(): void
$this->assertTrue($operations->has('password_reset'));
}

public function testDeduplicateShortNamesWhenEnabled(): void
public function testDeduplicateShortNames(): void
{
$factory = new AttributesResourceMetadataCollectionFactory(defaults: [
'extra_properties' => ['deduplicate_resource_short_names' => true],
], graphQlEnabled: true);
$factory = new AttributesResourceMetadataCollectionFactory(graphQlEnabled: true);

$collection = $factory->create(AttributeResource::class);

Expand All @@ -292,20 +290,6 @@ public function testDeduplicateShortNamesWhenEnabled(): void
}
}

/** @group legacy */
public function testDeduplicateShortNamesTriggersDeprecationWhenDisabled(): void
{
$factory = new AttributesResourceMetadataCollectionFactory(graphQlEnabled: true);

$this->expectUserDeprecationMessage('Since api-platform/core 4.2: Having multiple "#[ApiResource]" attributes with the same "shortName" "AttributeResource" on class "ApiPlatform\Metadata\Tests\Fixtures\ApiResource\AttributeResource" is deprecated and will result in automatic short name deduplication in API Platform 5.x. Set "defaults.extra_properties.deduplicate_resource_short_names" to "true" in the API Platform configuration to enable it now.');

$collection = $factory->create(AttributeResource::class);

// Without the flag, shortNames are NOT deduplicated
$this->assertSame('AttributeResource', $collection[0]->getShortName());
$this->assertSame('AttributeResource', $collection[1]->getShortName());
}

public function testWithParameters(): void
{
$attributeResourceMetadataCollectionFactory = new AttributesResourceMetadataCollectionFactory();
Expand Down
6 changes: 3 additions & 3 deletions src/OpenApi/Tests/Factory/OpenApiFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ public function testInvoke(): void

$propertyMetadataFactory = $propertyMetadataFactoryProphecy->reveal();

$definitionNameFactory = new DefinitionNameFactory([]);
$definitionNameFactory = new DefinitionNameFactory();

$schemaFactory = new SchemaFactory(
resourceMetadataFactory: $resourceCollectionMetadataFactory,
Expand Down Expand Up @@ -1397,7 +1397,7 @@ public function testGetExtensionPropertiesWithFalseValue(): void
$resourceCollectionMetadataFactory = $this->createMock(ResourceMetadataCollectionFactoryInterface::class);
$propertyNameCollectionFactory = $this->createMock(PropertyNameCollectionFactoryInterface::class);
$propertyMetadataFactory = $this->createMock(PropertyMetadataFactoryInterface::class);
$definitionNameFactory = new DefinitionNameFactory([]);
$definitionNameFactory = new DefinitionNameFactory();

$resourceCollectionMetadata = new ResourceMetadataCollection(Dummy::class, [(new ApiResource(operations: [
(new Get())->withOpenapi(true)->withShortName('Dummy')->withName('api_dummies_get_collection')->withRouteName('api_dummies_get_collection'),
Expand Down Expand Up @@ -1447,7 +1447,7 @@ public function testMetadataParameterInOpenApiOperationParametersThrows(): void
$resourceCollectionMetadataFactory = $this->createMock(ResourceMetadataCollectionFactoryInterface::class);
$propertyNameCollectionFactory = $this->createMock(PropertyNameCollectionFactoryInterface::class);
$propertyMetadataFactory = $this->createMock(PropertyMetadataFactoryInterface::class);
$definitionNameFactory = new DefinitionNameFactory([]);
$definitionNameFactory = new DefinitionNameFactory();

$resourceCollectionMetadata = new ResourceMetadataCollection(Dummy::class, [(new ApiResource(operations: [
(new GetCollection())
Expand Down
2 changes: 1 addition & 1 deletion src/OpenApi/Tests/Serializer/OpenApiNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function testNormalize(): void
$propertyNameCollectionFactory = $propertyNameCollectionFactoryProphecy->reveal();
$propertyMetadataFactory = $propertyMetadataFactoryProphecy->reveal();

$definitionNameFactory = new DefinitionNameFactory(null);
$definitionNameFactory = new DefinitionNameFactory();

$schemaFactory = new SchemaFactory(
resourceMetadataFactory: $resourceMetadataFactory,
Expand Down
87 changes: 0 additions & 87 deletions src/State/Processor/ObjectMapperProcessor.php

This file was deleted.

Loading
Loading