Skip to content

Commit 77cc08e

Browse files
committed
properties exception
1 parent e929b2d commit 77cc08e

2 files changed

Lines changed: 52 additions & 8 deletions

File tree

src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,19 @@ private function createParametersFromAttributes(Operation $operation): Parameter
483483
foreach ($reflectionProperty->getAttributes(Parameter::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
484484
$parameter = $attribute->newInstance();
485485

486-
$key = $parameter->getKey() ?? $reflectionProperty->getName();
487-
488-
if (null === $parameter->getProperty()) {
489-
$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()));
486+
$propertyName = $reflectionProperty->getName();
487+
$key = $parameter->getKey() ?? $propertyName;
488+
489+
if (null === $parameterPropertyName = $parameter->getProperty()) {
490+
$parameter = $parameter->withProperty($propertyName);
491+
} elseif ($parameterPropertyName !== $propertyName) {
492+
throw new RuntimeException(\sprintf('Parameter attribute on property "%s" must target itself or have no explicit property. Got "property: \'%s\'" instead.', $propertyName, $parameterPropertyName));
493+
}
494+
495+
if (null === $parameterProperties = $parameter->getProperties()) {
496+
$parameter = $parameter->withProperties([$propertyName]);
497+
} elseif (!\in_array($propertyName, $parameterProperties, true)) {
498+
throw new RuntimeException(\sprintf('Parameter attribute on property "%s" must target itself or have no explicit properties. Got "properties: [%s]" instead.', $propertyName, implode(', ', $parameterProperties)));
492499
}
493500

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

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,33 @@ public function testQueryParameterFromPropertyAttributeThrowsExceptionWhenProper
347347
$filterLocator
348348
);
349349

350-
$parameterFactory->create(ParameterOnPropertiesMismatchException::class);
350+
$parameterFactory->create(ParameterOnPropertiesMismatchPropertyException::class);
351+
}
352+
353+
public function testQueryParameterFromPropertyAttributeThrowsExceptionWhenPropertiesMismatch(): void
354+
{
355+
$this->expectException(RuntimeException::class);
356+
$this->expectExceptionMessage('Parameter attribute on property "name" must target itself or have no explicit properties. Got "properties: [description]" instead.');
357+
358+
$nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class);
359+
$nameCollection->method('create')->willReturn(new PropertyNameCollection(['id', 'name', 'description']));
360+
361+
$propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class);
362+
$propertyMetadata->method('create')->willReturn(
363+
new ApiProperty(readable: true),
364+
);
365+
366+
$filterLocator = $this->createStub(ContainerInterface::class);
367+
$filterLocator->method('has')->willReturn(false);
368+
369+
$parameterFactory = new ParameterResourceMetadataCollectionFactory(
370+
$nameCollection,
371+
$propertyMetadata,
372+
new AttributesResourceMetadataCollectionFactory(),
373+
$filterLocator
374+
);
375+
376+
$parameterFactory->create(ParameterOnPropertiesMismatchPropertiesException::class);
351377
}
352378

353379
public function testNestedPropertyWithNameConverter(): void
@@ -617,10 +643,21 @@ class ParameterOnProperties
617643
}
618644

619645
#[ApiResource]
620-
class ParameterOnPropertiesMismatchException
646+
class ParameterOnPropertiesMismatchPropertyException
621647
{
622648
#[QueryParameter(key: 'search', property: 'description')]
623649
public string $name = '';
624650

625651
public string $description = '';
626652
}
653+
654+
#[ApiResource]
655+
class ParameterOnPropertiesMismatchPropertiesException
656+
{
657+
#[QueryParameter(key: 'search', properties: ['description'])]
658+
public string $name = '';
659+
660+
public string $description = '';
661+
}
662+
663+

0 commit comments

Comments
 (0)