Skip to content

Allow empty operations in XML configs #1690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Metadata/Extractor/XmlExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,25 @@ private function getOperations(\SimpleXMLElement $resource, string $operationTyp
}

$operationsParent = $graphql ? 'graphql' : "{$operationType}s";

if (!isset($resource->$operationsParent)) {
return null;
}

return $this->getAttributes($resource->$operationsParent, $operationType);
return $this->getAttributes($resource->$operationsParent, $operationType, true);
}

/**
* Recursively transforms an attribute structure into an associative array.
*/
private function getAttributes(\SimpleXMLElement $resource, string $elementName): array
private function getAttributes(\SimpleXMLElement $resource, string $elementName, bool $topLevel = false): array
{
$attributes = [];
foreach ($resource->$elementName as $attribute) {
$value = isset($attribute->attribute[0]) ? $this->getAttributes($attribute, 'attribute') : XmlUtils::phpize($attribute);
// allow empty operations definition, like <collectionOperation name="post" />
if ($topLevel && '' === $value) {
$value = [];
}
if (isset($attribute['name'])) {
$attributes[(string) $attribute['name']] = $value;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/Metadata/schema/metadata.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</xsd:complexType>

<xsd:complexType name="itemOperation">
<xsd:sequence maxOccurs="unbounded">
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
<xsd:element name="attribute" maxOccurs="unbounded" type="attribute"/>
</xsd:sequence>
<xsd:attribute type="xsd:string" name="name"/>
Expand All @@ -52,7 +52,7 @@
</xsd:complexType>

<xsd:complexType name="collectionOperation">
<xsd:sequence maxOccurs="unbounded">
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
<xsd:element name="attribute" maxOccurs="unbounded" type="attribute"/>
</xsd:sequence>
<xsd:attribute type="xsd:string" name="name"/>
Expand Down
20 changes: 20 additions & 0 deletions tests/Fixtures/FileConfigurations/empty-operation.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8" ?>
<resources xmlns="https://api-platform.com/schema/metadata"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://api-platform.com/schema/metadata
https://api-platform.com/schema/metadata/metadata-2.0.xsd">
<resource class="App\Entity\Greeting">
<collectionOperations>
<collectionOperation name="get">
<attribute name="filters">
<attribute>greeting.search_filter</attribute>
</attribute>
</collectionOperation>
<collectionOperation name="post" />
</collectionOperations>
<itemOperations>
<itemOperation name="get" />
<itemOperation name="put" />
</itemOperations>
</resource>
</resources>
32 changes: 32 additions & 0 deletions tests/Metadata/Extractor/XmlExtractorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Metadata\Extractor;

use ApiPlatform\Core\Metadata\Extractor\XmlExtractor;
use PHPUnit\Framework\TestCase;

/**
* @author Kévin Dunglas <[email protected]>
*/
class XmlExtractorTest extends TestCase
{
public function testEmptyOperation()
{
$resources = (new XmlExtractor([__DIR__.'/../../Fixtures/FileConfigurations/empty-operation.xml']))->getResources();

$this->assertSame(['filters' => ['greeting.search_filter']], $resources['App\Entity\Greeting']['collectionOperations']['get']);
$this->assertSame([], $resources['App\Entity\Greeting']['collectionOperations']['post']);
$this->assertSame(['get' => [], 'put' => []], $resources['App\Entity\Greeting']['itemOperations']);
}
}