|
15 | 15 |
|
16 | 16 | use ApiPlatform\Metadata\ApiProperty; |
17 | 17 | use ApiPlatform\Metadata\ApiResource; |
| 18 | +use ApiPlatform\Metadata\Exception\RuntimeException; |
18 | 19 | use ApiPlatform\Metadata\FilterInterface; |
19 | 20 | use ApiPlatform\Metadata\GetCollection; |
20 | 21 | use ApiPlatform\Metadata\Parameters; |
@@ -282,6 +283,32 @@ public function testParameterFactoryWithLimitedProperties(): void |
282 | 283 | $this->assertSame(['name'], $param->getProperties()); |
283 | 284 | } |
284 | 285 |
|
| 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 | + |
285 | 312 | public function testNestedPropertyWithNameConverter(): void |
286 | 313 | { |
287 | 314 | $nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class); |
@@ -537,3 +564,12 @@ class NestedTestVariation |
537 | 564 | public ?int $id = null; |
538 | 565 | public ?string $variantName = null; |
539 | 566 | } |
| 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