Skip to content

Commit

Permalink
Add forward and backward compatibility to ProblemExceptionNormalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
niels-nijens committed Oct 27, 2023
1 parent f4cfa95 commit 0072174
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
31 changes: 31 additions & 0 deletions src/ExceptionHandling/Normalizer/NormalizerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/*
* This file is part of the OpenapiBundle package.
*
* (c) Niels Nijens <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nijens\OpenapiBundle\ExceptionHandling\Normalizer;

use Nijens\OpenapiBundle\NijensOpenapiBundle;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface as BaseNormalizerInterface;

/*
* Ensures compatibility with both Symfony versions 5.4 and 7.0.
*/
if (NijensOpenapiBundle::getSymfonyVersion() < 60100) {
interface NormalizerInterface extends ContextAwareNormalizerInterface
{
}
} else {
interface NormalizerInterface extends BaseNormalizerInterface
{
}
}
13 changes: 11 additions & 2 deletions src/ExceptionHandling/Normalizer/ProblemExceptionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Throwable;
Expand All @@ -27,7 +26,7 @@
*
* @author Niels Nijens <[email protected]>
*/
final class ProblemExceptionNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
final class ProblemExceptionNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;

Expand All @@ -43,6 +42,9 @@ public function __construct(bool $debug = false)
$this->debug = $debug;
}

/**
* @return array
*/
public function normalize($object, $format = null, array $context = [])
{
if ($object instanceof ProblemExceptionInterface === false) {
Expand Down Expand Up @@ -71,6 +73,13 @@ public function supportsNormalization($data, $format = null, array $context = []
return $data instanceof ProblemExceptionInterface;
}

public function getSupportedTypes(?string $format): array
{
return [
ProblemExceptionInterface::class => false,
];
}

private function removeDetailsToPreventInformationDisclosure(ProblemExceptionInterface $object, array &$data): void
{
if ($this->debug) {
Expand Down

0 comments on commit 0072174

Please sign in to comment.