Skip to content
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 4 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Feature: Viewing different price for different product variants
And the product "Wyborowa Vodka" has "Wyborowa Apple" variant priced at "$12.55"
And the "Wyborowa Lemon" product variant is disabled

@ui
@ui @api
Scenario: Viewing a detailed page with default variant's price
When I view product "Wyborowa Vodka"
Then the product price should be "$40.00"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ public function iRemoveItsNameFromTranslation(string $localeCode): void
*/
public function iShouldBeNotifiedThatItHasBeenSuccessfullyCreated(): void
{
$res = $this->client->getLastResponse();
Assert::true($this->responseChecker->isCreationSuccessful($this->client->getLastResponse()));
}

Expand Down
12 changes: 12 additions & 0 deletions src/Sylius/Behat/Context/Api/Shop/ProductContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,18 @@ public function theyShouldHaveOrderLikeAnd(string ...$productNames): void
}
}

/**
* @Then /^the product price should be ("[^"]+")$/
*/
public function theProductPriceShouldBe(int $price): void
{
$defaultVariantResponse = $this->client->showByIri(
$this->responseChecker->getValue($this->client->getLastResponse(), 'defaultVariant')
);

Assert::same($this->responseChecker->getValue($defaultVariantResponse, 'price'), $price);
}

private function hasProductWithPrice(array $products, int $price, ?string $productCode = null): bool
{
foreach ($products as $product) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ default:
- sylius.behat.context.transform.channel
- sylius.behat.context.transform.lexical
- sylius.behat.context.transform.product
- sylius.behat.context.transform.product_variant
- sylius.behat.context.transform.shared_storage
- sylius.behat.context.transform.taxon

Expand Down
58 changes: 0 additions & 58 deletions src/Sylius/Bundle/ApiBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,6 @@
<argument type="service" id="api_platform.iri_converter" />
</service>

<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_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>

<service id="api_platform.swagger.action.ui" class="Sylius\Bundle\ApiBundle\ApiPlatform\Bridge\Symfony\Bundle\Action\SwaggerUiAction" public="true">
<argument type="service" id="api_platform.metadata.resource.name_collection_factory" />
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
Expand Down Expand Up @@ -130,21 +95,6 @@
<tag name="sylius.api.command_data_transformer" />
</service>

<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\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>

<service id="sylius.api.factory.cart_shipping_method" class="Sylius\Bundle\ApiBundle\View\Factory\CartShippingMethodFactory" />

<service id="Sylius\Bundle\ApiBundle\Changer\PaymentMethodChanger">
Expand Down Expand Up @@ -173,14 +123,6 @@
</argument>
</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.api.section_resolver.admin_api_uri_based_section_resolver" class="Sylius\Bundle\ApiBundle\SectionResolver\AdminApiUriBasedSectionResolver">
<argument>%sylius.security.new_api_admin_route%</argument>
<tag name="sylius.uri_based_section_resolver" priority="30" />
Expand Down
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 src/Sylius/Bundle/ApiBundle/Resources/config/services/swagger.xml
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 src/Sylius/Bundle/ApiBundle/Serializer/ProductNormalizer.php
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;
Copy link
Member

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?

Copy link
Member Author

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


$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])) {
Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}
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;
}
}
Loading