Skip to content

Commit 4319fa1

Browse files
committed
api: extract examples for nested write payload ourselves
With - api-platform/core#6960 generating the nested write payload objects via the schema completely broke. The examples in the OpenApi Webui are still broken.
1 parent b3a20ab commit 4319fa1

File tree

7 files changed

+63
-6
lines changed

7 files changed

+63
-6
lines changed

api/tests/Api/Activities/CreateActivityTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,12 @@ public function getExampleReadPayload($attributes = [], $except = []) {
693693
Activity::class,
694694
Get::class,
695695
$attributes,
696-
['category', 'progressLabel'],
696+
[
697+
'category',
698+
'progressLabel',
699+
'camp',
700+
'rootContentNode',
701+
],
697702
$except
698703
);
699704
}

api/tests/Api/Categories/CreateCategoryTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,11 @@ public function getExampleReadPayload($attributes = [], $except = []) {
648648
Category::class,
649649
Get::class,
650650
$attributes,
651-
['camp', 'preferredContentTypes'],
651+
[
652+
'camp',
653+
'preferredContentTypes',
654+
'rootContentNode',
655+
],
652656
$except
653657
);
654658
}

api/tests/Api/Comments/CreateCommentTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public function getExampleWritePayload($attributes = [], $except = []) {
173173
'camp' => $this->getIriFor('camp1'),
174174
'activity' => $this->getIriFor('activity1'),
175175
], $attributes),
176-
[],
176+
['author'],
177177
$except
178178
);
179179
}

api/tests/Api/Comments/ListCommentsTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public function testListCommentsSortyByCreateTime() {
5252
'camp' => $this->getIriFor('camp1'),
5353
'activity' => $this->getIriFor('activity1'),
5454
],
55-
[],
55+
[
56+
'author',
57+
],
5658
[]
5759
)])->toArray();
5860

api/tests/Api/ECampApiTestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ protected function getExamplePayload(string $resourceClass, string $operationCla
184184
$schemaName = $matches[1];
185185
$properties = $schema->getDefinitions()[$schemaName]['properties'] ?? [];
186186
$writableProperties = array_filter($properties, fn ($property) => !($property['readOnly'] ?? false));
187+
$exampleExtractor = new ExampleExtractor($resourceClass);
188+
array_walk($writableProperties, fn (&$property, $key) => $property['example'] = $exampleExtractor->getExampleFor($key, $property));
187189
$writablePropertiesWithExample = array_filter($writableProperties, fn ($property) => ($property['example'] ?? false));
188190
$examples = array_map(fn ($property) => $property['example'] ?? $property['default'] ?? null, $writablePropertiesWithExample);
189191
$examples = array_map(function ($example) {

api/tests/Api/ExampleExtractor.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace App\Tests\Api;
4+
5+
use ApiPlatform\Metadata\ApiProperty;
6+
7+
readonly class ExampleExtractor {
8+
private \ReflectionClass $reflectionClass;
9+
10+
/**
11+
* @throws \ReflectionException
12+
*/
13+
public function __construct(
14+
string $resourceClass
15+
) {
16+
$this->reflectionClass = new \ReflectionClass($resourceClass);
17+
}
18+
19+
/**
20+
* @throws \ReflectionException
21+
*/
22+
public function getExampleFor($propertyName, $schemaProperty) {
23+
if (isset($schemaProperty['example'])) {
24+
return $schemaProperty['example'];
25+
}
26+
$reflectionProperty = $this->reflectionClass->getProperty($propertyName);
27+
$reflectionAttributes = $reflectionProperty->getAttributes(ApiProperty::class);
28+
if (empty($reflectionAttributes)) {
29+
return null;
30+
}
31+
if (count($reflectionAttributes) > 1) {
32+
$apiPropertyClass = ApiProperty::class;
33+
34+
throw new \RuntimeException("More than one {$apiPropertyClass} annotation found for property {$this->reflectionClass->name}->{$propertyName}");
35+
}
36+
37+
return $reflectionAttributes[0]->newInstance()->getExample();
38+
}
39+
}

api/tests/Api/MaterialItems/CreateMaterialItemTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ public function testCreateMaterialItemValidatesInvalidQuantity() {
386386

387387
$this->assertResponseStatusCodeSame(400);
388388
$this->assertJsonContains([
389-
'detail' => 'The type of the "quantity" attribute must be "float", "string" given.',
389+
'detail' => 'The type of the "quantity" attribute must be "float|null", "string" given.',
390390
]);
391391
}
392392

@@ -521,7 +521,12 @@ public function getExampleReadPayload($attributes = [], $except = []) {
521521
MaterialItem::class,
522522
Get::class,
523523
$attributes,
524-
['materialList', 'period', 'materialNode'],
524+
[
525+
'materialList',
526+
'period',
527+
'materialNode',
528+
'camp',
529+
],
525530
$except
526531
);
527532
}

0 commit comments

Comments
 (0)