Skip to content

Commit f19aba0

Browse files
Decrease ApiGatewayNormalizer priority
1 parent 5c8820d commit f19aba0

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

src/Bridge/Symfony/Bundle/Resources/config/swagger.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
<service id="api_platform.swagger.normalizer.api_gateway" class="ApiPlatform\Core\Swagger\Serializer\ApiGatewayNormalizer" public="false" decorates="api_platform.swagger.normalizer.documentation">
3838
<argument type="service" id="api_platform.swagger.normalizer.api_gateway.inner" />
39-
<tag name="serializer.normalizer" priority="17" />
39+
<tag name="serializer.normalizer" priority="-1" />
4040
</service>
4141

4242
<service id="api_platform.swagger.command.swagger_command" class="ApiPlatform\Core\Bridge\Symfony\Bundle\Command\SwaggerCommand">

src/Swagger/Serializer/ApiGatewayNormalizer.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\Core\Swagger\Serializer;
1515

1616
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
17+
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
1718
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1819

1920
/**
@@ -25,7 +26,7 @@
2526
*
2627
* @author Vincent Chalamon <[email protected]>
2728
*/
28-
final class ApiGatewayNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
29+
final class ApiGatewayNormalizer implements ContextAwareNormalizerInterface, CacheableSupportsMethodInterface
2930
{
3031
const API_GATEWAY = 'api_gateway';
3132

@@ -43,10 +44,8 @@ public function __construct(NormalizerInterface $documentationNormalizer, $defau
4344
*/
4445
public function normalize($object, $format = null, array $context = [])
4546
{
47+
unset($context[self::API_GATEWAY]);
4648
$data = $this->documentationNormalizer->normalize($object, $format, $context);
47-
if (!($context[self::API_GATEWAY] ?? $this->defaultContext[self::API_GATEWAY])) {
48-
return $data;
49-
}
5049

5150
if (empty($data['basePath'])) {
5251
$data['basePath'] = '/';
@@ -109,9 +108,9 @@ public function normalize($object, $format = null, array $context = [])
109108
/**
110109
* {@inheritdoc}
111110
*/
112-
public function supportsNormalization($data, $format = null)
111+
public function supportsNormalization($data, $format = null, array $context = [])
113112
{
114-
return $this->documentationNormalizer->supportsNormalization($data, $format);
113+
return $this->documentationNormalizer->supportsNormalization($data, $format) && ($context[self::API_GATEWAY] ?? ($this->defaultContext[self::API_GATEWAY] ?? false));
115114
}
116115

117116
/**

tests/Swagger/Serializer/ApiGatewayNormalizerTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,20 @@ protected function setUp()
5151
public function testSupportsNormalization()
5252
{
5353
$this->documentationNormalizerMock->supportsNormalization('foo', 'bar')->willReturn(true)->shouldBeCalledTimes(1);
54-
$this->assertTrue($this->normalizer->supportsNormalization('foo', 'bar'));
54+
$this->assertTrue($this->normalizer->supportsNormalization('foo', 'bar', ['api_gateway' => true]));
5555
$this->assertTrue($this->normalizer->hasCacheableSupportsMethod());
5656
}
5757

58-
public function testNormalizeWithoutApiGateway()
58+
public function testItDoesNotSupportNormalizationWithoutApiGatewayContext()
5959
{
60-
$this->documentationNormalizerMock->normalize($this->objectMock, 'jsonld', [])
61-
->willReturn(['basePath' => '/api'])
62-
->shouldBeCalledTimes(1);
63-
$this->assertEquals(['basePath' => '/api'], $this->normalizer->normalize($this->objectMock->reveal(), 'jsonld'));
60+
$this->documentationNormalizerMock->supportsNormalization('foo', 'bar')->willReturn(true)->shouldBeCalledTimes(2);
61+
$this->assertFalse($this->normalizer->supportsNormalization('foo', 'bar', ['api_gateway' => false]));
62+
$this->assertFalse($this->normalizer->supportsNormalization('foo', 'bar'));
6463
}
6564

6665
public function testNormalizeWithApiGateway()
6766
{
68-
$this->documentationNormalizerMock->normalize($this->objectMock, 'jsonld', ['api_gateway' => true])
67+
$this->documentationNormalizerMock->normalize($this->objectMock, 'jsonld', [])
6968
->willReturn([
7069
'basePath' => '',
7170
'paths' => [

0 commit comments

Comments
 (0)