Skip to content

Commit

Permalink
Decorate open API data to match the CQRS commands available fields in…
Browse files Browse the repository at this point in the history
…stead of the ApiResource ones
  • Loading branch information
jolelievre committed Jan 6, 2025
1 parent 10695c7 commit d8adede
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShopBundle\ApiPlatform\Metadata\Resource\Factory;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\OpenApi\Model\Operation as OpenapiOperation;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSCommand;
use PrestaShopBundle\ApiPlatform\OpenApi\RequestBodyFactory;

class OpenApiMetadataCollectionFactoryDecorator implements ResourceMetadataCollectionFactoryInterface
{
public function __construct(
private readonly ResourceMetadataCollectionFactoryInterface $innerFactory,
private readonly RequestBodyFactory $requestBodyFactory,
) {
}

public function create(string $resourceClass): ResourceMetadataCollection
{
// We call the original method since we only want to alter the result of this method.
$resourceMetadataCollection = $this->innerFactory->create($resourceClass);

/** @var ApiResource $resourceMetadata */
foreach ($resourceMetadataCollection as $resourceMetadata) {
$operations = $resourceMetadata->getOperations();
/** @var Operation $operation */
foreach ($operations as $key => $operation) {
if (!($operation instanceof CQRSCommand)) {
continue;
}

$openapiOperation = $operation->getOpenapi() ?: new OpenapiOperation();
$openapiOperation = $openapiOperation->withRequestBody($this->requestBodyFactory->build($operation));
$operations->add($key, $operation->withOpenapi($openapiOperation));
}
}

return $resourceMetadataCollection;
}
}
98 changes: 98 additions & 0 deletions src/PrestaShopBundle/ApiPlatform/OpenApi/RequestBodyFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShopBundle\ApiPlatform\OpenApi;

use ApiPlatform\OpenApi\Model\RequestBody;
use ArrayObject;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSCommand;
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyInitializableExtractorInterface;

class RequestBodyFactory
{
public function __construct(
protected readonly PropertyInfoExtractorInterface|PropertyInitializableExtractorInterface $propertyInfoExtractor
) {
}

public function build(CQRSCommand $operation): ?RequestBody
{
if (empty($operation->getCQRSCommand()) || !class_exists($operation->getCQRSCommand())) {
return null;
}

$requestMimeTypes = $this->flattenMimeTypes($operation->getInputFormats() ?: []);
$inputSchema = [];
foreach ($requestMimeTypes as $requestMimeType) {
$operationProperties = $this->getOperationProperties($operation);
$inputSchema[$requestMimeType] = [
'schema' => [
'type' => 'object',
'properties' => $operationProperties,
],
];
}

return new RequestBody(
description: sprintf('The %s %s resource', 'POST' === $operation->getMethod() ? 'new' : 'updated', $operation->getShortName()),
content: new ArrayObject($inputSchema),
);
}

private function getOperationProperties(CQRSCommand $operation): array
{
$operationProperties = [];
$classProperties = $this->propertyInfoExtractor->getProperties($operation->getCQRSCommand());
foreach ($classProperties as $property) {
if ($this->propertyInfoExtractor->isWritable($operation->getCQRSCommand(), $property)
|| $this->propertyInfoExtractor->isInitializable($operation->getCQRSCommand(), $property)) {
$propertyTypes = $this->propertyInfoExtractor->getTypes($operation->getCQRSCommand(), $property);
if (count($propertyTypes) === 1) {
$propertyType = $propertyTypes[0];
$type = $propertyType->getClassName() ?: $propertyType->getBuiltinType();
} else {
$type = 'mixed';
}
$operationProperties[$property] = ['type' => $type];
}
}

return $operationProperties;
}

private function flattenMimeTypes(array $responseFormats): array
{
$responseMimeTypes = [];
foreach ($responseFormats as $responseFormat => $mimeTypes) {
foreach ($mimeTypes as $mimeType) {
$responseMimeTypes[$mimeType] = $responseFormat;
}
}

return $responseMimeTypes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ services:
$innerFactory: '@.inner'
$isDebug: '%kernel.debug%'

PrestaShopBundle\ApiPlatform\Metadata\Resource\Factory\OpenApiMetadataCollectionFactoryDecorator:
decorates: api_platform.metadata.resource.metadata_collection_factory
autowire: true
arguments:
$innerFactory: '@.inner'

# This service depends on ResourceMetadataCollectionFactoryInterface (auto wired) and can extract the scopes defined on operations
PrestaShopBundle\ApiPlatform\Scopes\ApiResourceScopesExtractor:
autowire: true
Expand All @@ -59,3 +65,6 @@ services:
decorates: PrestaShopBundle\ApiPlatform\Scopes\ApiResourceScopesExtractor

PrestaShopBundle\ApiPlatform\Scopes\ApiResourceScopesExtractorInterface: '@PrestaShopBundle\ApiPlatform\Scopes\CachedApiResourceScopesExtractor'

PrestaShopBundle\ApiPlatform\OpenApi\RequestBodyFactory:
autowire: true

0 comments on commit d8adede

Please sign in to comment.