Skip to content

Commit

Permalink
refactor Sylius#13881 Use api route prefix parameter instead of `/api…
Browse files Browse the repository at this point in the history
…/v2` (Prometee)

This PR was merged into the 1.12-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | master
| Bug fix?        | yes
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | fixes Sylius#13854
| License         | MIT

Small PR to be able to use another api route prefix while generating the swagger page.


Commits
-------

a5aa5f7 Use api route prefix parameter instead of fixed /api/v2
  • Loading branch information
lchrusciel authored Apr 25, 2022
2 parents 5cf9044 + a5aa5f7 commit 6cbcaa8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
>
<argument type="service" id="Sylius\Bundle\ApiBundle\Swagger\ProductImageDocumentationNormalizer.inner" />
<argument type="service" id="Sylius\Bundle\ApiBundle\Provider\ProductImageFilterProviderInterface" />
<argument>%sylius.security.new_api_route%</argument>
</service>

<service
Expand All @@ -91,6 +92,7 @@
decoration-priority="20"
>
<argument type="service" id="Sylius\Bundle\ApiBundle\Swagger\ProductSlugDocumentationNormalizer.inner" />
<argument>%sylius.security.new_api_route%</argument>
</service>

<service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,13 @@
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/** @experimental */
class ProductImageDocumentationNormalizer implements NormalizerInterface
final class ProductImageDocumentationNormalizer implements NormalizerInterface
{
private const SHOP_ITEM_PATH = '/api/v2/shop/product-images/{id}';

/** @var NormalizerInterface */
private $decoratedNormalizer;

/** @var ProductImageFilterProviderInterface */
private $filterProvider;

public function __construct(NormalizerInterface $decoratedNormalizer, ProductImageFilterProviderInterface $filterProvider)
{
$this->decoratedNormalizer = $decoratedNormalizer;
$this->filterProvider = $filterProvider;
public function __construct(
private NormalizerInterface $decoratedNormalizer,
private ProductImageFilterProviderInterface $filterProvider,
private string $apiRoute
) {
}

public function supportsNormalization($data, $format = null)
Expand All @@ -45,17 +38,18 @@ public function normalize($object, $format = null, array $context = [])
$enums = $this->filterProvider->provideShopFilters();
$enums = array_keys($enums);

$params = $docs['paths'][self::SHOP_ITEM_PATH]['get']['parameters'];
$shopProductImagePath = $this->apiRoute . '/shop/product-images/{id}';
$params = $docs['paths'][$shopProductImagePath]['get']['parameters'];

foreach ($params as $index => $param) {
foreach ($params as &$param) {
if ($param['in'] === 'query') {
if (is_string($param['schema']['enum']) || $param['schema']['enum'] === null) {
$docs['paths'][self::SHOP_ITEM_PATH]['get']['parameters'][$index]['schema']['enum'] = $enums;
$param['schema']['enum'] = $enums;

break;
}

$docs['paths'][self::SHOP_ITEM_PATH]['get']['parameters'][$index]['schema']['enum'] = array_merge($param['schema']['enum'], $enums);
$param['schema']['enum'] = array_merge($param['schema']['enum'], $enums);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
/** @experimental */
final class ProductSlugDocumentationNormalizer implements NormalizerInterface
{
private const PRODUCT_SLUG_PATH = '/api/v2/shop/products-by-slug/{slug}';

public function __construct(private NormalizerInterface $decoratedNormalizer)
{
public function __construct(
private NormalizerInterface $decoratedNormalizer,
private string $apiRoute
) {
}

public function supportsNormalization($data, $format = null): bool
Expand All @@ -33,11 +33,12 @@ public function normalize($object, $format = null, array $context = [])
{
$docs = $this->decoratedNormalizer->normalize($object, $format, $context);

$params = $docs['paths'][self::PRODUCT_SLUG_PATH]['get']['parameters'];
$shopProductBySlugPath = $this->apiRoute . '/shop/products-by-slug/{slug}';
$params = $docs['paths'][$shopProductBySlugPath]['get']['parameters'];

foreach ($params as $index => $param) {
if ($param['name'] === 'code') {
unset($docs['paths'][self::PRODUCT_SLUG_PATH]['get']['parameters'][$index]);
unset($docs['paths'][$shopProductBySlugPath]['get']['parameters'][$index]);
}
}

Expand Down

0 comments on commit 6cbcaa8

Please sign in to comment.