-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API][Product] Expose default variant on product show and index #12649
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a0d7e4f
[API][Product] Expose default variant on product show and index
GSadee 1639cb0
[API][Product] Add defaultVariant field to the Swagger schema
GSadee 5e62ecd
[API][Product] Use proper normalizer in custom product normalizer ser…
GSadee 0fb6a90
[API][Product] Add case when there is no variant to product normalizer
GSadee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
48 changes: 48 additions & 0 deletions
48
src/Sylius/Bundle/ApiBundle/Resources/config/services/serializers.xml
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,48 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!-- | ||
|
||
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. | ||
|
||
--> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" | ||
> | ||
<services> | ||
<service id="Sylius\Bundle\ApiBundle\Serializer\AddressDenormalizer"> | ||
<argument type="service" id="serializer.normalizer.object" /> | ||
<argument type="string">%sylius.model.address.class%</argument> | ||
<argument type="string">%sylius.model.address.interface%</argument> | ||
<tag name="serializer.normalizer" priority="64" /> | ||
</service> | ||
|
||
<service id="Sylius\Bundle\ApiBundle\Serializer\CommandFieldItemIriToIdentifierDenormalizer"> | ||
<argument type="service" id="serializer.normalizer.object" /> | ||
<argument type="service" id="Sylius\Bundle\ApiBundle\Converter\ItemIriToIdentifierConverter" /> | ||
<argument type="service" id="sylius.api.data_transformer.command_aware_input_data_transformer" /> | ||
<argument type="service" id="Sylius\Bundle\ApiBundle\Map\CommandItemIriArgumentToIdentifierMap" /> | ||
<tag name="serializer.normalizer" /> | ||
</service> | ||
|
||
<service id="Sylius\Bundle\ApiBundle\Serializer\ProductNormalizer"> | ||
<argument type="service" id="sylius.product_variant_resolver.default" /> | ||
<argument type="service" id="api_platform.iri_converter" /> | ||
<tag name="serializer.normalizer" priority="64" /> | ||
</service> | ||
|
||
<service id="Sylius\Bundle\ApiBundle\Serializer\ProductVariantSerializer"> | ||
<argument type="service" id="api_platform.serializer.normalizer.item" /> | ||
<argument type="service" id="sylius.calculator.product_variant_price" /> | ||
<argument type="service" id="sylius.context.channel" /> | ||
<argument type="service" id="sylius.availability_checker" /> | ||
<tag name="serializer.normalizer" priority="64" /> | ||
</service> | ||
</services> | ||
</container> |
65 changes: 65 additions & 0 deletions
65
src/Sylius/Bundle/ApiBundle/Resources/config/services/swagger.xml
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,65 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!-- | ||
|
||
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. | ||
|
||
--> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" | ||
> | ||
<services> | ||
<service | ||
id="sylius.api.swagger_admin_authentication_documentation_normalizer" | ||
class="Sylius\Bundle\ApiBundle\Swagger\AdminAuthenticationTokenDocumentationNormalizer" | ||
decorates="api_platform.swagger.normalizer.documentation" | ||
public="true" | ||
autoconfigure="false" | ||
decoration-priority="20" | ||
> | ||
<argument type="service" id="sylius.api.swagger_admin_authentication_documentation_normalizer.inner" /> | ||
<argument>%sylius.security.new_api_route%</argument> | ||
</service> | ||
|
||
<service | ||
id="sylius.api.swagger_shop_authentication_documentation_normalizer" | ||
class="Sylius\Bundle\ApiBundle\Swagger\ShopAuthenticationTokenDocumentationNormalizer" | ||
decorates="api_platform.swagger.normalizer.documentation" | ||
public="true" | ||
autoconfigure="false" | ||
decoration-priority="10" | ||
> | ||
<argument type="service" id="sylius.api.swagger_shop_authentication_documentation_normalizer.inner" /> | ||
<argument>%sylius.security.new_api_route%</argument> | ||
</service> | ||
|
||
<service | ||
id="sylius.api.swagger_product_documentation_normalizer" | ||
class="Sylius\Bundle\ApiBundle\Swagger\ProductDocumentationNormalizer" | ||
decorates="api_platform.swagger.normalizer.documentation" | ||
public="true" | ||
autoconfigure="false" | ||
decoration-priority="20" | ||
> | ||
<argument type="service" id="sylius.api.swagger_product_documentation_normalizer.inner" /> | ||
</service> | ||
|
||
<service | ||
id="sylius.api.swagger_product_variant_documentation_normalizer" | ||
class="Sylius\Bundle\ApiBundle\Swagger\ProductVariantDocumentationNormalizer" | ||
decorates="api_platform.swagger.normalizer.documentation" | ||
public="true" | ||
autoconfigure="false" | ||
decoration-priority="20" | ||
> | ||
<argument type="service" id="sylius.api.swagger_product_variant_documentation_normalizer.inner" /> | ||
</service> | ||
</services> | ||
</container> |
57 changes: 57 additions & 0 deletions
57
src/Sylius/Bundle/ApiBundle/Serializer/ProductNormalizer.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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\Serializer; | ||
|
||
use ApiPlatform\Core\Api\IriConverterInterface; | ||
use Sylius\Component\Core\Model\ProductInterface; | ||
use Sylius\Component\Product\Resolver\ProductVariantResolverInterface; | ||
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** @experimental */ | ||
final class ProductNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface | ||
{ | ||
use NormalizerAwareTrait; | ||
|
||
private const ALREADY_CALLED = 'product_normalizer_already_called'; | ||
|
||
/** @var ProductVariantResolverInterface */ | ||
private $defaultProductVariantResolver; | ||
|
||
/** @var IriConverterInterface */ | ||
private $iriConverter; | ||
|
||
public function __construct( | ||
ProductVariantResolverInterface $defaultProductVariantResolver, | ||
IriConverterInterface $iriConverter | ||
) { | ||
$this->defaultProductVariantResolver = $defaultProductVariantResolver; | ||
$this->iriConverter = $iriConverter; | ||
} | ||
|
||
public function normalize($object, $format = null, array $context = []) | ||
{ | ||
Assert::isInstanceOf($object, ProductInterface::class); | ||
|
||
$context[self::ALREADY_CALLED] = true; | ||
|
||
$data = $this->normalizer->normalize($object, $format, $context); | ||
$variant = $this->defaultProductVariantResolver->getVariant($object); | ||
$data['defaultVariant'] = $variant === null ? null : $this->iriConverter->getIriFromItem($variant); | ||
|
||
return $data; | ||
} | ||
|
||
public function supportsNormalization($data, $format = null, $context = []): bool | ||
{ | ||
if (isset($context[self::ALREADY_CALLED])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add one more spec for testing of this logic |
||
return false; | ||
} | ||
|
||
return $data instanceof ProductInterface; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/Sylius/Bundle/ApiBundle/Swagger/ProductDocumentationNormalizer.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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ApiBundle\Swagger; | ||
|
||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
/** @experimental */ | ||
final class ProductDocumentationNormalizer implements NormalizerInterface | ||
{ | ||
/** @var NormalizerInterface */ | ||
private $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); | ||
|
||
$defaultVariantSchema = [ | ||
'type' => 'string', | ||
'format' => 'iri-reference', | ||
'nullable' => true, | ||
'readOnly' => true, | ||
]; | ||
|
||
$docs['components']['schemas']['Product.jsonld-admin.product.read']['properties']['defaultVariant'] = $defaultVariantSchema; | ||
$docs['components']['schemas']['Product.jsonld-shop.product.read']['properties']['defaultVariant'] = $defaultVariantSchema; | ||
|
||
return $docs; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we should add assert of early return in case this class is already called indeed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO there is no need, because this check is in
supportsNormalization
method, but I can add it to be in 100% secured