Skip to content

Commit 34151e8

Browse files
authored
Merge pull request #1690 from dunglas/xml-allow-empty-op
Allow empty operations in XML configs
2 parents 2e4da92 + 288aaba commit 34151e8

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

src/Metadata/Extractor/XmlExtractor.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,25 @@ private function getOperations(\SimpleXMLElement $resource, string $operationTyp
7373
}
7474

7575
$operationsParent = $graphql ? 'graphql' : "{$operationType}s";
76-
7776
if (!isset($resource->$operationsParent)) {
7877
return null;
7978
}
8079

81-
return $this->getAttributes($resource->$operationsParent, $operationType);
80+
return $this->getAttributes($resource->$operationsParent, $operationType, true);
8281
}
8382

8483
/**
8584
* Recursively transforms an attribute structure into an associative array.
8685
*/
87-
private function getAttributes(\SimpleXMLElement $resource, string $elementName): array
86+
private function getAttributes(\SimpleXMLElement $resource, string $elementName, bool $topLevel = false): array
8887
{
8988
$attributes = [];
9089
foreach ($resource->$elementName as $attribute) {
9190
$value = isset($attribute->attribute[0]) ? $this->getAttributes($attribute, 'attribute') : XmlUtils::phpize($attribute);
91+
// allow empty operations definition, like <collectionOperation name="post" />
92+
if ($topLevel && '' === $value) {
93+
$value = [];
94+
}
9295
if (isset($attribute['name'])) {
9396
$attributes[(string) $attribute['name']] = $value;
9497
} else {

src/Metadata/schema/metadata.xsd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
</xsd:complexType>
4040

4141
<xsd:complexType name="itemOperation">
42-
<xsd:sequence maxOccurs="unbounded">
42+
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
4343
<xsd:element name="attribute" maxOccurs="unbounded" type="attribute"/>
4444
</xsd:sequence>
4545
<xsd:attribute type="xsd:string" name="name"/>
@@ -52,7 +52,7 @@
5252
</xsd:complexType>
5353

5454
<xsd:complexType name="collectionOperation">
55-
<xsd:sequence maxOccurs="unbounded">
55+
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
5656
<xsd:element name="attribute" maxOccurs="unbounded" type="attribute"/>
5757
</xsd:sequence>
5858
<xsd:attribute type="xsd:string" name="name"/>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<resources xmlns="https://api-platform.com/schema/metadata"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="https://api-platform.com/schema/metadata
5+
https://api-platform.com/schema/metadata/metadata-2.0.xsd">
6+
<resource class="App\Entity\Greeting">
7+
<collectionOperations>
8+
<collectionOperation name="get">
9+
<attribute name="filters">
10+
<attribute>greeting.search_filter</attribute>
11+
</attribute>
12+
</collectionOperation>
13+
<collectionOperation name="post" />
14+
</collectionOperations>
15+
<itemOperations>
16+
<itemOperation name="get" />
17+
<itemOperation name="put" />
18+
</itemOperations>
19+
</resource>
20+
</resources>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Metadata\Extractor;
15+
16+
use ApiPlatform\Core\Metadata\Extractor\XmlExtractor;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* @author Kévin Dunglas <[email protected]>
21+
*/
22+
class XmlExtractorTest extends TestCase
23+
{
24+
public function testEmptyOperation()
25+
{
26+
$resources = (new XmlExtractor([__DIR__.'/../../Fixtures/FileConfigurations/empty-operation.xml']))->getResources();
27+
28+
$this->assertSame(['filters' => ['greeting.search_filter']], $resources['App\Entity\Greeting']['collectionOperations']['get']);
29+
$this->assertSame([], $resources['App\Entity\Greeting']['collectionOperations']['post']);
30+
$this->assertSame(['get' => [], 'put' => []], $resources['App\Entity\Greeting']['itemOperations']);
31+
}
32+
}

0 commit comments

Comments
 (0)