Skip to content

Commit

Permalink
xml/yaml support
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Mar 12, 2024
1 parent d5df12f commit 05fdf33
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 58 deletions.
30 changes: 28 additions & 2 deletions src/Metadata/Extractor/XmlResourceExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@

use ApiPlatform\Metadata\Exception\InvalidArgumentException;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\HeaderParameter;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\QueryParameter;
use ApiPlatform\Metadata\Tests\Fixtures\StateOptions;
use ApiPlatform\OpenApi\Model\ExternalDocumentation;
use ApiPlatform\OpenApi\Model\Operation as OpenApiOperation;
use ApiPlatform\OpenApi\Model\Parameter;
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
use ApiPlatform\OpenApi\Model\RequestBody;
use ApiPlatform\State\OptionsInterface;
use Symfony\Component\Config\Util\XmlUtils;
Expand Down Expand Up @@ -97,6 +99,7 @@ private function buildExtendedBase(\SimpleXMLElement $resource): array
'stateOptions' => $this->buildStateOptions($resource),
'links' => $this->buildLinks($resource),
'headers' => $this->buildHeaders($resource),
'parameters' => $this->buildParameters($resource),
]);
}

Expand Down Expand Up @@ -200,7 +203,7 @@ private function buildOpenapi(\SimpleXMLElement $resource): bool|OpenApiOperatio

if (isset($openapi->parameters->parameter)) {
foreach ($openapi->parameters->parameter as $parameter) {
$data['parameters'][(string) $parameter->attributes()->name] = new Parameter(
$data['parameters'][(string) $parameter->attributes()->name] = new OpenApiParameter(

Check warning on line 206 in src/Metadata/Extractor/XmlResourceExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Extractor/XmlResourceExtractor.php#L206

Added line #L206 was not covered by tests
name: $this->phpize($parameter, 'name', 'string'),
in: $this->phpize($parameter, 'in', 'string'),
description: $this->phpize($parameter, 'description', 'string'),
Expand Down Expand Up @@ -494,4 +497,27 @@ private function buildHeaders(\SimpleXMLElement $resource): ?array

return $headers;
}

/**
* @return array<string, \ApiPlatform\Metadata\Parameter>
*/
private function buildParameters(\SimpleXMLElement $resource): ?array
{
if (!$resource->parameters) {
return null;
}

$parameters = [];
foreach ($resource->parameters->parameter as $parameter) {
$key = (string) $parameter->attributes()->key;
$cl = ('header' === (string) $parameter->attributes()->in) ? HeaderParameter::class : QueryParameter::class;
$parameters[$key] = new $cl(
key: $key,
required: $this->phpize($parameter, 'required', 'bool'),
schema: isset($parameter->schema->values) ? $this->buildValues($parameter->schema->values) : null,
);

Check warning on line 518 in src/Metadata/Extractor/XmlResourceExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Extractor/XmlResourceExtractor.php#L510-L518

Added lines #L510 - L518 were not covered by tests
}

return $parameters;

Check warning on line 521 in src/Metadata/Extractor/XmlResourceExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Extractor/XmlResourceExtractor.php#L521

Added line #L521 was not covered by tests
}
}
25 changes: 25 additions & 0 deletions src/Metadata/Extractor/YamlResourceExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

use ApiPlatform\Metadata\Exception\InvalidArgumentException;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\HeaderParameter;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\QueryParameter;
use ApiPlatform\Metadata\Tests\Fixtures\StateOptions;
use ApiPlatform\OpenApi\Model\ExternalDocumentation;
use ApiPlatform\OpenApi\Model\Operation as OpenApiOperation;
Expand Down Expand Up @@ -124,6 +126,7 @@ private function buildExtendedBase(array $resource): array
'stateOptions' => $this->buildStateOptions($resource),
'links' => $this->buildLinks($resource),
'headers' => $this->buildHeaders($resource),
'parameters' => $this->buildParameters($resource),
]);
}

Expand Down Expand Up @@ -450,4 +453,26 @@ private function buildHeaders(array $resource): ?array

return $headers;
}

/**
* @return array<string, \ApiPlatform\Metadata\Parameter>
*/
private function buildParameters(array $resource): ?array
{
if (!isset($resource['parameters']) || !\is_array($resource['parameters'])) {
return null;
}

$parameters = [];
foreach ($resource['parameters'] as $key => $parameter) {
$cl = ($parameter['in'] ?? 'query') === 'header' ? HeaderParameter::class : QueryParameter::class;
$parameters[$key] = new $cl(
key: $key,
required: $this->phpize($parameter, 'required', 'bool'),
schema: $parameter['schema']
);

Check warning on line 473 in src/Metadata/Extractor/YamlResourceExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Extractor/YamlResourceExtractor.php#L466-L473

Added lines #L466 - L473 were not covered by tests
}

return $parameters;

Check warning on line 476 in src/Metadata/Extractor/YamlResourceExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Extractor/YamlResourceExtractor.php#L476

Added line #L476 was not covered by tests
}
}
3 changes: 2 additions & 1 deletion src/Metadata/Extractor/schema/resources.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@
<xsd:element name="schema" minOccurs="0" type="sequenceWithValues"/>
<xsd:element name="extraProperties" minOccurs="0" type="sequenceWithValues"/>
</xsd:sequence>
<xsd:attribute type="xsd:string" name="key"/>
<xsd:attribute type="xsd:string" name="key" use="required"/>
<xsd:attribute type="xsd:string" name="in"/>
<xsd:attribute type="xsd:string" name="provider"/>
<xsd:attribute type="xsd:string" name="filter"/>
<xsd:attribute type="xsd:string" name="property"/>
Expand Down
17 changes: 17 additions & 0 deletions src/Metadata/Tests/Extractor/Adapter/XmlResourceAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ final class XmlResourceAdapter implements ResourceAdapterInterface
'stateOptions',
'collectDenormalizationErrors',
'links',
'parameters',
];

/**
Expand Down Expand Up @@ -521,6 +522,22 @@ private function buildHeaders(\SimpleXMLElement $resource, ?array $values = null
}
}

private function buildParameters(\SimpleXMLElement $resource, ?array $values = null): void
{
if (!$values) {
return;
}

$node = $resource->addChild('parameters');
foreach ($values as $key => $value) {
$childNode = $node->addChild('parameter');
$childNode->addAttribute('in', 'query');
$childNode->addAttribute('key', $key);
$childNode->addAttribute('required', $this->parse($value['required']));
$this->buildValues($childNode->addChild('schema'), $value['schema']);
}
}

private function parse($value): ?string
{
if (null === $value) {
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Tests/Extractor/Adapter/resources.xml

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/Metadata/Tests/Extractor/Adapter/resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ resources:
-
rel: 'http://www.w3.org/ns/json-ld#error'
href: 'http://www.w3.org/ns/hydra/error'
parameters:
author:
key: author
required: true
schema: { type: string }
-
uriTemplate: '/users/{userId}/comments/{commentId}{._format}'
class: ApiPlatform\Metadata\Get
Expand Down
55 changes: 18 additions & 37 deletions src/Metadata/Tests/Extractor/ResourceMetadataCompatibilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\QueryParameter;
use ApiPlatform\Metadata\Resource\Factory\ExtractorResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\Resource\Factory\OperationDefaultsTrait;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
Expand Down Expand Up @@ -421,6 +422,9 @@ final class ResourceMetadataCompatibilityTest extends TestCase
'links' => [
['rel' => 'http://www.w3.org/ns/json-ld#error', 'href' => 'http://www.w3.org/ns/hydra/error'],
],
'parameters' => [
'author' => ['key' => 'author', 'required' => true, 'schema' => ['type' => 'string']],
],
],
[
'uriTemplate' => '/users/{userId}/comments/{commentId}{._format}',
Expand Down Expand Up @@ -508,6 +512,7 @@ final class ResourceMetadataCompatibilityTest extends TestCase
'stateOptions',
'links',
'headers',
'parameters',
];

/**
Expand All @@ -528,12 +533,8 @@ public function testValidMetadata(string $extractorClass, ResourceAdapterInterfa
throw new AssertionFailedError('Failed asserting that the schema is valid according to '.ApiResource::class, 0, $exception);
}

$a = new ResourceMetadataCollection(self::RESOURCE_CLASS, $this->buildApiResources());
$b = $collection;

$this->assertEquals($a[0], $b[0]);

$this->assertEquals(new ResourceMetadataCollection(self::RESOURCE_CLASS, $this->buildApiResources()), $collection);
$resources = $this->buildApiResources();
$this->assertEquals(new ResourceMetadataCollection(self::RESOURCE_CLASS, $resources), $collection);
}

public static function getExtractors(): array
Expand Down Expand Up @@ -750,37 +751,17 @@ private function withLinks(array $values): ?array
return [new Link($values[0]['rel'] ?? null, $values[0]['href'] ?? null)];
}

private function withParameters(array $values): array
private function withParameters(array $values): ?array
{
// $uriVariables = [];
// foreach ($values as $parameterName => $value) {
// if (\is_string($value)) {
// $uriVariables[$value] = $value;
// continue;
// }
//
// if (isset($value['fromClass']) || isset($value[0])) {
// $uriVariables[$parameterName]['from_class'] = $value['fromClass'] ?? $value[0];
// }
// if (isset($value['fromProperty']) || isset($value[1])) {
// $uriVariables[$parameterName]['from_property'] = $value['fromProperty'] ?? $value[1];
// }
// if (isset($value['toClass'])) {
// $uriVariables[$parameterName]['to_class'] = $value['toClass'];
// }
// if (isset($value['toProperty'])) {
// $uriVariables[$parameterName]['to_property'] = $value['toProperty'];
// }
// if (isset($value['identifiers'])) {
// $uriVariables[$parameterName]['identifiers'] = $value['identifiers'];
// }
// if (isset($value['compositeIdentifier'])) {
// $uriVariables[$parameterName]['composite_identifier'] = $value['compositeIdentifier'];
// }
// }

dd($values);

return $values;
if (!$values) {
return null;
}

$parameters = [];
foreach ($values as $k => $value) {
$parameters[$k] = new QueryParameter(key: $value['key'], required: $value['required'], schema: $value['schema']);
}

return $parameters;
}
}
14 changes: 14 additions & 0 deletions src/Metadata/Tests/Extractor/XmlExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ApiPlatform\Metadata\Extractor\XmlResourceExtractor;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\QueryParameter;
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\Comment;
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\User;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -102,6 +103,7 @@ public function testValidXML(): void
'stateOptions' => null,
'links' => null,
'headers' => null,
'parameters' => null,
],
[
'uriTemplate' => '/users/{author}/comments{._format}',
Expand Down Expand Up @@ -275,6 +277,7 @@ public function testValidXML(): void
'stateOptions' => null,
'links' => null,
'headers' => ['hello' => 'world'],
'parameters' => null,
],
[
'name' => null,
Expand Down Expand Up @@ -376,6 +379,16 @@ public function testValidXML(): void
'stateOptions' => null,
'links' => null,
'headers' => ['hello' => 'world'],
'parameters' => [
'author' => new QueryParameter(
key: 'author',
required: true,
schema: [
'type' => 'string',
],
extraProperties: []
),
],
],
],
'graphQlOperations' => null,
Expand All @@ -387,6 +400,7 @@ public function testValidXML(): void
'stateOptions' => null,
'links' => null,
'headers' => ['hello' => 'world'],
'parameters' => null,
],
],
], $extractor->getResources());
Expand Down
7 changes: 7 additions & 0 deletions src/Metadata/Tests/Extractor/YamlExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ApiPlatform\Metadata\Extractor\YamlResourceExtractor;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\QueryParameter;
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\FlexConfig;
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\Program;
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\SingleFileConfigDummy;
Expand Down Expand Up @@ -102,6 +103,7 @@ public function testValidYaml(): void
'stateOptions' => null,
'links' => null,
'headers' => null,
'parameters' => null,
],
],
Program::class => [
Expand Down Expand Up @@ -174,6 +176,7 @@ public function testValidYaml(): void
'stateOptions' => null,
'links' => null,
'headers' => null,
'parameters' => null,
],
[
'uriTemplate' => '/users/{author}/programs{._format}',
Expand Down Expand Up @@ -317,6 +320,7 @@ public function testValidYaml(): void
'stateOptions' => null,
'links' => null,
'headers' => ['hello' => 'world'],
'parameters' => null,
],
[
'name' => null,
Expand Down Expand Up @@ -401,6 +405,7 @@ public function testValidYaml(): void
'stateOptions' => null,
'links' => null,
'headers' => ['hello' => 'world'],
'parameters' => ['author' => new QueryParameter(schema: ['type' => 'string'], required: true, key: 'author')],
],
],
'graphQlOperations' => null,
Expand All @@ -411,6 +416,7 @@ public function testValidYaml(): void
'stateOptions' => null,
'links' => null,
'headers' => ['hello' => 'world'],
'parameters' => null,
],
],
SingleFileConfigDummy::class => [
Expand Down Expand Up @@ -483,6 +489,7 @@ public function testValidYaml(): void
'stateOptions' => null,
'links' => null,
'headers' => null,
'parameters' => null,
],
],
], $extractor->getResources());
Expand Down
32 changes: 16 additions & 16 deletions src/Metadata/Tests/Extractor/xml/valid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,24 @@
<value name="boolean">true</value>
</values>
</extraProperties>

<parameters>
<parameter key="author" required="true" in="query">
<schema>
<values>
<value name="type">string</value>
</values>
</schema>
<extraProperties>
<values>
<value name="foo">bar</value>
</values>
</extraProperties>
</parameter>
</parameters>

</operation>
</operations>

<parameters>
<parameter key="author" required="true">
<schema>
<values>
<value name="type">string</value>
</values>
</schema>
<extraProperties>
<values>
<value name="foo">bar</value>
<value name="boolean">true</value>
</values>
</extraProperties>
</parameter>
</parameters>

</resource>
</resources>
Loading

0 comments on commit 05fdf33

Please sign in to comment.