forked from PrestaShop/PrestaShop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decorate open API data to match the CQRS commands available fields in…
…stead of the ApiResource ones
- Loading branch information
1 parent
10695c7
commit d8adede
Showing
3 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
...undle/ApiPlatform/Metadata/Resource/Factory/OpenApiMetadataCollectionFactoryDecorator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
98
src/PrestaShopBundle/ApiPlatform/OpenApi/RequestBodyFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters