Skip to content

Commit afc6c92

Browse files
2.0-upgrade add open api docs
1 parent 2b31d79 commit afc6c92

File tree

3 files changed

+113
-61
lines changed

3 files changed

+113
-61
lines changed

config/api_platform/resources/Product.xml

-61
Original file line numberDiff line numberDiff line change
@@ -22,67 +22,6 @@
2222
</value>
2323
</values>
2424
</normalizationContext>
25-
<openapi>
26-
<parameters>
27-
<parameter name="query" in="query" description="Search query string" required="false" example="shoes" deprecated="false" allowEmptyValue="false" explode="false" style="form" allowReserved="false">
28-
<schema>
29-
<values>
30-
<value name="type">string</value>
31-
</values>
32-
</schema>
33-
</parameter>
34-
<parameter name="limit" in="query" description="Number of items to return per page" required="false" example="10" deprecated="false" allowEmptyValue="false" explode="false" style="form" allowReserved="false">
35-
<schema>
36-
<values>
37-
<value name="type">integer</value>
38-
<value name="default">10</value>
39-
</values>
40-
</schema>
41-
</parameter>
42-
<parameter name="page" in="query" description="Page number" required="false" example="1" deprecated="false" allowEmptyValue="false" explode="false" style="form" allowReserved="false">
43-
<schema>
44-
<values>
45-
<value name="type">integer</value>
46-
<value name="default">1</value>
47-
</values>
48-
</schema>
49-
</parameter>
50-
<parameter name="order_by" in="query" description="Field to order by (sold_units, product_created_at, price)" required="false" example="product_created_at" deprecated="false" allowEmptyValue="false" explode="false" style="form" allowReserved="false">
51-
<schema>
52-
<values>
53-
<value name="type">string</value>
54-
<value name="enum">
55-
<values>
56-
<value>sold_units</value>
57-
<value>product_created_at</value>
58-
<value>price</value>
59-
</values>
60-
</value>
61-
</values>
62-
</schema>
63-
</parameter>
64-
<parameter name="sort" in="query" description="Order direction (asc, desc)" required="false" example="desc" deprecated="false" allowEmptyValue="false" explode="false" style="form" allowReserved="false">
65-
<schema>
66-
<values>
67-
<value name="type">string</value>
68-
<value name="enum">
69-
<values>
70-
<value>asc</value>
71-
<value>desc</value>
72-
</values>
73-
</value>
74-
</values>
75-
</schema>
76-
</parameter>
77-
<parameter name="facets" in="query" description="Filter facets with dynamic keys" required="false" example='{"t_shirt_material": ["100% cotton"], "t_shirt_brand": ["modern_wear"]}' deprecated="false" allowEmptyValue="false" explode="true" style="deepObject" allowReserved="false">
78-
<schema>
79-
<values>
80-
<value name="type">object</value>
81-
</values>
82-
</schema>
83-
</parameter>
84-
</parameters>
85-
</openapi>
8625
</operation>
8726
</operations>
8827
</resource>

config/services.xml

+5
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,10 @@
3939
<service id="bitbag.sylius_elasticsearch_plugin.repository.product_option_repository" class="BitBag\SyliusElasticsearchPlugin\Repository\ProductOptionRepository">
4040
<argument type="service" id="sylius.repository.product_option" />
4141
</service>
42+
43+
<service id="BitBag\SyliusElasticsearchPlugin\Api\OpenApi\Documentation\ProductSearchDocumentationModifier"
44+
decorates="api_platform.openapi.factory" public="true">
45+
<argument type="service" id="BitBag\SyliusElasticsearchPlugin\Api\OpenApi\Documentation\ProductSearchDocumentationModifier.inner" />
46+
</service>
4247
</services>
4348
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BitBag\SyliusElasticsearchPlugin\Api\OpenApi\Documentation;
6+
7+
use ApiPlatform\OpenApi\Model\Parameter;
8+
use ApiPlatform\OpenApi\OpenApi;
9+
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
10+
11+
final class ProductSearchDocumentationModifier implements OpenApiFactoryInterface
12+
{
13+
private OpenApiFactoryInterface $decorated;
14+
15+
public function __construct(OpenApiFactoryInterface $decorated)
16+
{
17+
$this->decorated = $decorated;
18+
}
19+
20+
public function __invoke(array $context = []): OpenApi
21+
{
22+
$openApi = $this->decorated->__invoke($context);
23+
$paths = $openApi->getPaths();
24+
$pathItem = $paths->getPath('/api/v2/shop/products/search');
25+
26+
if (!$pathItem) {
27+
return $openApi;
28+
}
29+
30+
$operation = $pathItem->getGet();
31+
if (!$operation) {
32+
return $openApi;
33+
}
34+
35+
$parameters = [
36+
new Parameter(
37+
'query',
38+
'query',
39+
'Search query string',
40+
false,
41+
false,
42+
false,
43+
['type' => 'string'],
44+
example: 'shoes'
45+
),
46+
47+
new Parameter(
48+
'limit',
49+
'query',
50+
'Number of items per page',
51+
false,
52+
false,
53+
false,
54+
['type' => 'integer', 'default' => 10],
55+
example: 20
56+
),
57+
58+
new Parameter(
59+
'page',
60+
'query',
61+
'Page number',
62+
false,
63+
false,
64+
false,
65+
['type' => 'integer', 'default' => 1],
66+
example: 2
67+
),
68+
69+
new Parameter(
70+
'order_by',
71+
'query',
72+
'Sort field',
73+
false,
74+
false,
75+
false,
76+
['type' => 'string', 'enum' => ['sold_units', 'product_created_at', 'price']],
77+
example: 'price'
78+
),
79+
80+
new Parameter(
81+
'sort',
82+
'query',
83+
'Sort direction',
84+
false,
85+
false,
86+
false,
87+
['type' => 'string', 'enum' => ['asc', 'desc']],
88+
example: 'asc'
89+
),
90+
91+
new Parameter(
92+
'facets',
93+
'query',
94+
'Filter facets with dynamic keys',
95+
false,
96+
false,
97+
false,
98+
['type' => 'object'],
99+
example: '{"t_shirt_material": ["100% cotton"], "t_shirt_brand": ["modern_wear"]}'
100+
),
101+
];
102+
103+
$newOperation = $operation->withParameters($parameters);
104+
$paths->addPath('/api/v2/shop/products/search', $pathItem->withGet($newOperation));
105+
106+
return $openApi->withPaths($paths);
107+
}
108+
}

0 commit comments

Comments
 (0)