Skip to content

Commit

Permalink
Adjust FlattenException and HydraError normalizers to Symfony 5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubtobiasz committed Dec 15, 2022
1 parent 7448985 commit d9a7ecb
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 47 deletions.
29 changes: 29 additions & 0 deletions UPGRADE-API-1.12.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
# UPGRADE FROM `v1.12.1` TO `v1.12.2`

1. So far, on production environment when any non-validation related error occurred, the `FOS\RestBundle\Serializer\Normalizer\FlattenExceptionHandler` was used, even on API Platform endpoints.
Now, depending on the path of the request, the `FOS\RestBundle\Serializer\Normalizer\FlattenExceptionHandler` or `ApiPlatform\Hydra\Serializer\ErrorNormalizer` is used. If your code
rely on the previous behavior, you should add the following configuration to your `config/packages/_sylius.yaml` file:
```yaml
sylius_api:
legacy_error_handling: true
```
Example response before bugfix:
```json
{
"code": 500,
"message": "Internal Server Error"
}
```

Example response after bugfix:
```json
{
"@context": "/api/v2/contexts/Error",
"@type": "hydra:Error",
"hydra:title": "An error occurred",
"hydra:description": "Internal Server Error"
}
```
The status code is passed along the response as an HTTP status code, and the `message` value is returned in a `hydra:description` field.

# UPGRADE FROM `v1.11.X` TO `v1.12.0`

1. The `Sylius\Bundle\ApiBundle\DataProvider\CartShippingMethodsSubresourceDataProvider` has been removed and replaced by `Sylius\Bundle\ApiBundle\DataProvider\ShippingMethodsCollectionDataProvider`.
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<referencedClass name="Symfony\Component\Security\Core\Encoder\EncoderAwareInterface" /> <!-- deprecated in Symfony 5.3 -->
<referencedClass name="Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface" /> <!-- deprecated in Symfony 5.3 -->
<referencedClass name="Symfony\Component\Security\Core\Exception\UsernameNotFoundException" /> <!-- deprecated in Symfony 5.3 -->
<referencedClass name="Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface" /> <!-- deprecated in Symfony 6.1, probably a bug in psalm, as this is referenced as DeprecatedClass, not an interface -->
<referencedClass name="Symfony\Component\HttpFoundation\RequestMatcher" /> <!-- deprecated in Symfony 6.2 -->
</errorLevel>
</DeprecatedClass>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function iShouldNotBeAbleToChangeMyPasswordAgainWithTheSameToken(): void

Assert::same($lastResponse->getStatusCode(), Response::HTTP_INTERNAL_SERVER_ERROR);
$message = $this->responseChecker->getError($lastResponse);
Assert::startsWith($message, 'No user found with reset token: ');
Assert::startsWith($message, 'Internal Server Error');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Sylius/Behat/Context/Api/Shop/LoginContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public function iShouldNotBeAbleToChangeMyPasswordAgainWithTheSameToken(): void
// token is removed when used
Assert::same($this->client->getLastResponse()->getStatusCode(), 500);
$message = $this->responseChecker->getError($this->client->getLastResponse());
Assert::startsWith($message, 'No user found with reset token: ');
Assert::startsWith($message, 'Internal Server Error');
}

private function addLocale(string $locale): void
Expand Down
5 changes: 3 additions & 2 deletions src/Sylius/Behat/Context/Api/Shop/ProductVariantContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,10 @@ public function variantAndVariantShouldNotBeDiscounted(ProductVariantInterface .
*/
public function iShouldNotSeeVariant(ProductVariantInterface $variant): void
{
$content = $this->responseChecker->getResponseContent($this->client->show(Resources::PRODUCT_VARIANTS, $variant->getCode()));
$response = $this->client->show(Resources::PRODUCT_VARIANTS, $variant->getCode());

Assert::same(
$content['code'],
$response->getStatusCode(),
404,
sprintf('%s variant should be disabled', $variant->getName()),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,9 @@ public function __construct (
) {
}

public function supportsNormalization($data, $format = null, array $context = [])
public function supportsNormalization($data, $format = null, array $context = []): bool
{
if (method_exists($this->requestStack, 'getMainRequest')) {
$path = $this->requestStack->getMainRequest()->getPathInfo();
} else {
/** @phpstan-ignore-next-line */
$path = $this->requestStack->getMasterRequest()->getPathInfo();
}
$path = $this->requestStack->getMainRequest()->getPathInfo();

if (str_starts_with($path, $this->newApiRoute)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,9 @@ public function normalize($object, $format = null, array $context = [])
return $this->decorated->normalize($object, $format, $context);
}

public function supportsNormalization($data, $format = null)
public function supportsNormalization($data, $format = null): bool
{
if (method_exists($this->requestStack, 'getMainRequest')) {
$path = $this->requestStack->getMainRequest()->getPathInfo();
} else {
/** @phpstan-ignore-next-line */
$path = $this->requestStack->getMasterRequest()->getPathInfo();
}
$path = $this->requestStack->getMainRequest()->getPathInfo();

if (!str_starts_with($path, $this->newApiRoute)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ function it_doesnt_support_normalization_when_path_starts_with_new_api_route(

$request->getPathInfo()->willReturn('/api/v2/products');

if (method_exists(RequestStack::class, 'getMainRequest')) {
$requestStack->getMainRequest()->willReturn($request);
} else {
/** @phpstan-ignore-next-line */
$requestStack->getMasterRequest()->willReturn($request);
}
$requestStack->getMainRequest()->willReturn($request);

$normalizer->supportsNormalization('data', 'format', ['context'])->shouldNotBeCalled();

Expand All @@ -61,15 +56,10 @@ function it_supports_normalization_when_path_doesnt_start_with_new_api_route(

$request->getPathInfo()->willReturn('/api/v1/products');

if (method_exists(RequestStack::class, 'getMainRequest')) {
$requestStack->getMainRequest()->willReturn($request);
} else {
/** @phpstan-ignore-next-line */
$requestStack->getMasterRequest()->willReturn($request);
}
$requestStack->getMainRequest()->willReturn($request);

$normalizer->supportsNormalization('data', 'format', ['context'])->shouldBeCalled();
$normalizer->supportsNormalization('data', 'format', ['context'])->shouldBeCalled()->willReturn(true);

$this->supportsNormalization('data', 'format', ['context']);
$this->supportsNormalization('data', 'format', ['context'])->shouldReturn(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,11 @@ function it_doesnt_support_normalization_when_path_doesnt_start_with_new_api_rou

$request->getPathInfo()->willReturn('/api/v1/resource');

if (method_exists(RequestStack::class, 'getMainRequest')) {
$requestStack->getMainRequest()->willReturn($request);
} else {
/** @phpstan-ignore-next-line */
$requestStack->getMasterRequest()->willReturn($request);
}
$requestStack->getMainRequest()->willReturn($request);

$normalizer->supportsNormalization('data', 'format')->shouldNotBeCalled();

$this->supportsNormalization('data', 'format');
$this->supportsNormalization('data', 'format')->shouldReturn(false);
}

function it_calls_decorated_support_normalize_method_when_path_starts_with_new_api_route(
Expand All @@ -60,16 +55,11 @@ function it_calls_decorated_support_normalize_method_when_path_starts_with_new_a

$request->getPathInfo()->willReturn('/api/v2/resource');

if (method_exists(RequestStack::class, 'getMainRequest')) {
$requestStack->getMainRequest()->willReturn($request);
} else {
/** @phpstan-ignore-next-line */
$requestStack->getMasterRequest()->willReturn($request);
}
$requestStack->getMainRequest()->willReturn($request);

$normalizer->supportsNormalization('data', 'format')->shouldBeCalled();
$normalizer->supportsNormalization('data', 'format')->shouldBeCalled()->willReturn(true);

$this->supportsNormalization('data', 'format');
$this->supportsNormalization('data', 'format')->shouldReturn(true);
}

function it_decorates_has_cacheable_supports_method(
Expand Down

0 comments on commit d9a7ecb

Please sign in to comment.