Skip to content

Commit 4240130

Browse files
committed
fix(api): fix the search endpoint types
1 parent abb90cf commit 4240130

File tree

3 files changed

+108
-4
lines changed

3 files changed

+108
-4
lines changed

config/admin/services.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ services:
2828
PrestaShop\Module\APIResources\Validation\IframeValidationGroupsResolver:
2929
arguments:
3030
- '@prestashop.adapter.legacy.configuration'
31+
32+
# Custom denormalizer to handle type conversion for SearchProducts query
33+
PrestaShop\Module\APIResources\Normalizer\SearchProductsDenormalizer:
34+
autowire: true
35+
tags:
36+
- { name: 'serializer.normalizer', priority: 10 }

src/ApiPlatform/Resources/Product/FoundProduct.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
operations: [
3232
new CQRSGetCollection(
3333
uriTemplate: '/products/search/{phrase}/{resultsLimit}/{isoCode}',
34+
scopes: [
35+
'product_read',
36+
],
37+
CQRSQuery: SearchProducts::class,
3438
openapiContext: [
3539
'parameters' => [
3640
[
@@ -46,7 +50,7 @@
4650
'in' => 'path',
4751
'required' => true,
4852
'schema' => [
49-
'type' => 'int',
53+
'type' => 'integer',
5054
],
5155
],
5256
[
@@ -62,12 +66,11 @@
6266
'in' => 'query',
6367
'required' => false,
6468
'schema' => [
65-
'type' => 'int',
69+
'type' => 'integer',
6670
],
6771
],
6872
],
69-
],
70-
CQRSQuery: SearchProducts::class
73+
]
7174
),
7275
],
7376
)]
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Academic Free License version 3.0
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/AFL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to license@prestashop.com so we can send you a copy immediately.
15+
*
16+
* @author PrestaShop SA and Contributors <contact@prestashop.com>
17+
* @copyright Since 2007 PrestaShop SA and Contributors
18+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace PrestaShop\Module\APIResources\Normalizer;
24+
25+
use PrestaShop\PrestaShop\Core\Domain\Product\Query\SearchProducts;
26+
use PrestaShopBundle\ApiPlatform\DomainObjectDetector;
27+
use PrestaShopBundle\ApiPlatform\LocalizedValueUpdater;
28+
use PrestaShopBundle\ApiPlatform\Normalizer\CQRSApiNormalizer;
29+
use PrestaShopBundle\ApiPlatform\Validator\CQRSApiValidator;
30+
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
31+
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
32+
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface;
33+
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
34+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
35+
36+
class SearchProductsDenormalizer extends CQRSApiNormalizer
37+
{
38+
public function __construct(
39+
DomainObjectDetector $domainObjectDetector,
40+
LocalizedValueUpdater $localizedValueUpdater,
41+
CQRSApiValidator $CQRSApiValidator,
42+
?ClassMetadataFactoryInterface $classMetadataFactory = null,
43+
?NameConverterInterface $nameConverter = null,
44+
?PropertyAccessorInterface $propertyAccessor = null,
45+
?PropertyTypeExtractorInterface $propertyTypeExtractor = null,
46+
?ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null,
47+
?callable $objectClassResolver = null,
48+
array $defaultContext = []
49+
) {
50+
parent::__construct(
51+
$domainObjectDetector,
52+
$localizedValueUpdater,
53+
$CQRSApiValidator,
54+
$classMetadataFactory,
55+
$nameConverter,
56+
$propertyAccessor,
57+
$propertyTypeExtractor,
58+
$classDiscriminatorResolver,
59+
$objectClassResolver,
60+
$defaultContext
61+
);
62+
}
63+
64+
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
65+
{
66+
if ($type === SearchProducts::class && is_array($data)) {
67+
if (isset($data['resultsLimit']) && is_string($data['resultsLimit'])) {
68+
$data['resultsLimit'] = (int) $data['resultsLimit'];
69+
}
70+
71+
if (isset($data['orderId']) && is_string($data['orderId'])) {
72+
$data['orderId'] = (int) $data['orderId'];
73+
}
74+
}
75+
76+
return parent::denormalize($data, $type, $format, $context);
77+
}
78+
79+
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
80+
{
81+
if ($type === SearchProducts::class) {
82+
return parent::supportsDenormalization($data, $type, $format, $context);
83+
}
84+
85+
return false;
86+
}
87+
88+
public function getSupportedTypes(?string $format): array
89+
{
90+
return [
91+
SearchProducts::class => true,
92+
];
93+
}
94+
}
95+

0 commit comments

Comments
 (0)