Skip to content

Commit d8e6eb8

Browse files
committed
Throw exception if parameter property does not match class property name
1 parent f210abc commit d8e6eb8

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,8 @@ private function createParametersFromAttributes(Operation $operation): Parameter
487487

488488
if (null === $parameter->getProperty()) {
489489
$parameter = $parameter->withProperty($reflectionProperty->getName());
490+
} elseif ($parameter->getProperty() !== $reflectionProperty->getName()) {
491+
throw new RuntimeException(\sprintf('Parameter attribute on property "%s" must target itself or have no explicit property. Got "property: \'%s\'" instead.', $reflectionProperty->getName(), $parameter->getProperty()));
490492
}
491493

492494
$parameters->add($key, $parameter);

src/Metadata/Tests/Resource/Factory/ParameterResourceMetadataCollectionFactoryTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use ApiPlatform\Metadata\ApiProperty;
1717
use ApiPlatform\Metadata\ApiResource;
18+
use ApiPlatform\Metadata\Exception\RuntimeException;
1819
use ApiPlatform\Metadata\FilterInterface;
1920
use ApiPlatform\Metadata\GetCollection;
2021
use ApiPlatform\Metadata\Parameters;
@@ -282,6 +283,32 @@ public function testParameterFactoryWithLimitedProperties(): void
282283
$this->assertSame(['name'], $param->getProperties());
283284
}
284285

286+
public function testQueryParameterFromPropertyAttributeThrowsExceptionWhenPropertyMismatch(): void
287+
{
288+
$this->expectException(RuntimeException::class);
289+
$this->expectExceptionMessage('Parameter attribute on property "name" must target itself or have no explicit property. Got "property: \'description\'" instead.');
290+
291+
$nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class);
292+
$nameCollection->method('create')->willReturn(new PropertyNameCollection(['id', 'name', 'description']));
293+
294+
$propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class);
295+
$propertyMetadata->method('create')->willReturn(
296+
new ApiProperty(readable: true),
297+
);
298+
299+
$filterLocator = $this->createStub(ContainerInterface::class);
300+
$filterLocator->method('has')->willReturn(false);
301+
302+
$parameterFactory = new ParameterResourceMetadataCollectionFactory(
303+
$nameCollection,
304+
$propertyMetadata,
305+
new AttributesResourceMetadataCollectionFactory(),
306+
$filterLocator
307+
);
308+
309+
$parameterFactory->create(ParameterOnPropertiesMismatchException::class);
310+
}
311+
285312
public function testNestedPropertyWithNameConverter(): void
286313
{
287314
$nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class);
@@ -537,3 +564,12 @@ class NestedTestVariation
537564
public ?int $id = null;
538565
public ?string $variantName = null;
539566
}
567+
568+
#[ApiResource]
569+
class ParameterOnPropertiesMismatchException
570+
{
571+
#[QueryParameter(key: 'search', property: 'description')]
572+
public string $name = '';
573+
574+
public string $description = '';
575+
}

0 commit comments

Comments
 (0)