Skip to content

Commit

Permalink
product slug api documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
paullla committed Nov 9, 2021
1 parent 7196f03 commit 1f178eb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@
<attribute name="read">false</attribute>
<attribute name="openapi_context">
<attribute name="summary">Use slug to retrieve a product resource.</attribute>
<attribute name="parameters">
<attribute>
<attribute name="name">slug</attribute>
<attribute name="in">path</attribute>
<attribute name="required">true</attribute>
<attribute name="schema">
<attribute name="type">string</attribute>
</attribute>
</attribute>
</attribute>
</attribute>
<attribute name="normalization_context">
<attribute name="groups">shop:product:read</attribute>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@
<argument type="service" id="Sylius\Bundle\ApiBundle\Provider\ProductImageFilterProviderInterface" />
</service>

<service
id="Sylius\Bundle\ApiBundle\Swagger\ProductSlugDocumentationNormalizer"
decorates="api_platform.swagger.normalizer.documentation"
autoconfigure="false"
decoration-priority="20"
>
<argument type="service" id="Sylius\Bundle\ApiBundle\Swagger\ProductSlugDocumentationNormalizer.inner" />
</service>

<service
id="Sylius\Bundle\ApiBundle\Swagger\ProductVariantDocumentationNormalizer"
decorates="api_platform.swagger.normalizer.documentation"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\Swagger;

use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/** @experimental */
final class ProductSlugDocumentationNormalizer implements NormalizerInterface
{
private const PRODUCT_SLUG_PATH = '/api/v2/shop/products/slug/{slug}';

private NormalizerInterface $decoratedNormalizer;

public function __construct(NormalizerInterface $decoratedNormalizer)
{
$this->decoratedNormalizer = $decoratedNormalizer;
}

public function supportsNormalization($data, $format = null)
{
return $this->decoratedNormalizer->supportsNormalization($data, $format);
}

public function normalize($object, $format = null, array $context = [])
{
$docs = $this->decoratedNormalizer->normalize($object, $format, $context);

$params = $docs['paths'][self::PRODUCT_SLUG_PATH]['get']['parameters'];

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

return $docs;
}
}

0 comments on commit 1f178eb

Please sign in to comment.