Skip to content

Commit cf8c7a0

Browse files
committed
fix(symfony): unload deprecated json problem services
1 parent 4d9d17a commit cf8c7a0

File tree

9 files changed

+145
-25
lines changed

9 files changed

+145
-25
lines changed

.github/workflows/ci.yml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,85 @@ jobs:
11531153
php-coveralls --coverage_clover=build/logs/phpunit/clover.xml
11541154
continue-on-error: true
11551155

1156+
phpunit_legacy_errors:
1157+
name: PHPUnit Legacy errors (PHP ${{ matrix.php }})
1158+
env:
1159+
RFC_7807_COMPLIANT_ERRORS: 0
1160+
runs-on: ubuntu-latest
1161+
timeout-minutes: 20
1162+
strategy:
1163+
matrix:
1164+
php:
1165+
- '8.3'
1166+
include:
1167+
- php: '8.3'
1168+
coverage: true
1169+
fail-fast: false
1170+
steps:
1171+
- name: Checkout
1172+
uses: actions/checkout@v4
1173+
- name: Setup PHP
1174+
uses: shivammathur/setup-php@v2
1175+
with:
1176+
php-version: ${{ matrix.php }}
1177+
tools: pecl, composer
1178+
extensions: intl, bcmath, curl, openssl, mbstring, pdo_sqlite
1179+
coverage: pcov
1180+
ini-values: memory_limit=-1
1181+
- name: Get composer cache directory
1182+
id: composercache
1183+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
1184+
- name: Cache dependencies
1185+
uses: actions/cache@v4
1186+
with:
1187+
path: ${{ steps.composercache.outputs.dir }}
1188+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
1189+
restore-keys: ${{ runner.os }}-composer-
1190+
- name: Enable code coverage
1191+
if: matrix.coverage
1192+
run: echo "COVERAGE=1" >> $GITHUB_ENV
1193+
- name: Update project dependencies
1194+
run: composer update --no-interaction --no-progress --ansi
1195+
- name: Install PHPUnit
1196+
run: vendor/bin/simple-phpunit --version
1197+
- name: Clear test app cache
1198+
run: tests/Fixtures/app/console cache:clear --ansi
1199+
- name: Use legacy ignored deprecations
1200+
run: cp tests/.ignored-deprecations-legacy-events tests/.ignored-deprecations
1201+
- name: Run PHPUnit tests
1202+
run: |
1203+
mkdir -p build/logs/phpunit
1204+
if [ "$COVERAGE" = '1' ]; then
1205+
vendor/bin/simple-phpunit --log-junit build/logs/phpunit/junit.xml --coverage-clover build/logs/phpunit/clover.xml
1206+
else
1207+
vendor/bin/simple-phpunit --log-junit build/logs/phpunit/junit.xml
1208+
fi
1209+
- name: Upload test artifacts
1210+
if: always()
1211+
uses: actions/upload-artifact@v4
1212+
with:
1213+
name: phpunit-logs-php${{ matrix.php }}
1214+
path: build/logs/phpunit
1215+
continue-on-error: true
1216+
- name: Upload coverage results to Codecov
1217+
if: matrix.coverage
1218+
uses: codecov/codecov-action@v3
1219+
with:
1220+
directory: build/logs/phpunit
1221+
name: phpunit-php${{ matrix.php }}
1222+
flags: phpunit
1223+
fail_ci_if_error: true
1224+
continue-on-error: true
1225+
- name: Upload coverage results to Coveralls
1226+
if: matrix.coverage
1227+
env:
1228+
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1229+
run: |
1230+
composer global require --prefer-dist --no-interaction --no-progress --ansi php-coveralls/php-coveralls
1231+
export PATH="$PATH:$HOME/.composer/vendor/bin"
1232+
php-coveralls --coverage_clover=build/logs/phpunit/clover.xml
1233+
continue-on-error: true
1234+
11561235
behat_legacy:
11571236
name: Behat Legacy event listeners (PHP ${{ matrix.php }})
11581237
env:

src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function load(array $configs, ContainerBuilder $container): void
157157
$this->registerJsonApiConfiguration($formats, $loader, $config);
158158
$this->registerJsonLdHydraConfiguration($container, $formats, $loader, $config);
159159
$this->registerJsonHalConfiguration($formats, $loader);
160-
$this->registerJsonProblemConfiguration($errorFormats, $loader);
160+
$this->registerJsonProblemConfiguration($errorFormats, $loader, $config);
161161
$this->registerGraphQlConfiguration($container, $config, $loader);
162162
$this->registerCacheConfiguration($container);
163163
$this->registerDoctrineOrmConfiguration($container, $config, $loader);
@@ -585,12 +585,16 @@ private function registerJsonHalConfiguration(array $formats, XmlFileLoader $loa
585585
$loader->load('hal.xml');
586586
}
587587

588-
private function registerJsonProblemConfiguration(array $errorFormats, XmlFileLoader $loader): void
588+
private function registerJsonProblemConfiguration(array $errorFormats, XmlFileLoader $loader, array $config): void
589589
{
590590
if (!isset($errorFormats['jsonproblem'])) {
591591
return;
592592
}
593593

594+
if (false === ($config['defaults']['extra_properties']['rfc_7807_compliant_errors'] ?? true)) {
595+
$loader->load('legacy/problem.xml');
596+
}
597+
594598
$loader->load('problem.xml');
595599
}
596600

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<services>
8+
<service id="api_platform.problem.normalizer.constraint_violation_list" class="ApiPlatform\Problem\Serializer\ConstraintViolationListNormalizer" public="false">
9+
<argument>%api_platform.validator.serialize_payload_fields%</argument>
10+
<argument type="service" id="api_platform.name_converter" on-invalid="ignore" />
11+
12+
<tag name="serializer.normalizer" priority="-780" />
13+
</service>
14+
15+
<!-- deprecated -->
16+
<service id="api_platform.problem.normalizer.error" class="ApiPlatform\Problem\Serializer\ErrorNormalizer" public="false">
17+
<argument>%kernel.debug%</argument>
18+
<argument type="collection"></argument>
19+
20+
<tag name="serializer.normalizer" priority="-810" />
21+
</service>
22+
</services>
23+
24+
</container>

src/Symfony/Bundle/Resources/config/problem.xml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,12 @@
1212
<tag name="serializer.encoder" />
1313
</service>
1414

15-
<service id="api_platform.problem.normalizer.constraint_violation_list" class="ApiPlatform\Problem\Serializer\ConstraintViolationListNormalizer" public="false">
16-
<argument>%api_platform.validator.serialize_payload_fields%</argument>
17-
<argument type="service" id="api_platform.name_converter" on-invalid="ignore" />
18-
19-
<tag name="serializer.normalizer" priority="-780" />
20-
</service>
21-
2215
<service id="api_platform.problem.normalizer.validation_exception" class="ApiPlatform\Symfony\Validator\Serializer\ValidationExceptionNormalizer" public="false">
2316
<argument type="service" id="api_platform.serializer.normalizer.item" />
2417
<argument type="service" id="api_platform.name_converter" on-invalid="ignore" />
2518

2619
<tag name="serializer.normalizer" priority="-800" />
2720
</service>
28-
29-
<!-- deprecated -->
30-
<service id="api_platform.problem.normalizer.error" class="ApiPlatform\Problem\Serializer\ErrorNormalizer" public="false">
31-
<argument>%kernel.debug%</argument>
32-
<argument type="collection"></argument>
33-
34-
<tag name="serializer.normalizer" priority="-810" />
35-
</service>
3621
</services>
3722

3823
</container>

tests/Fixtures/app/AppKernel.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ class_exists(NativePasswordHasher::class) ? 'password_hashers' : 'encoders' => [
245245
$legacyConfig = ['event_listeners_backward_compatibility_layer' => $metadataBackwardCompatibilityLayer];
246246
}
247247

248+
if (!$rfc7807CompliantErrors) {
249+
$loader->load(__DIR__.'/config/config_legacy_error.yml');
250+
}
251+
248252
$c->prependExtensionConfig('api_platform', $legacyConfig + [
249253
'mapping' => [
250254
'paths' => ['%kernel.project_dir%/../TestBundle/Resources/config/api_resources'],

tests/Fixtures/app/config/config_common.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,6 @@ services:
433433
arguments:
434434
$decorated: '@.inner'
435435

436-
ApiPlatform\Tests\Fixtures\TestBundle\Serializer\ErrorNormalizer:
437-
decorates: 'api_platform.problem.normalizer.error'
438-
arguments: [ '@.inner' ]
439-
440436
api_platform.http_cache.tag_collector:
441437
class: ApiPlatform\Tests\Fixtures\TestBundle\HttpCache\TagCollectorDefault
442438
public: true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
ApiPlatform\Tests\Fixtures\TestBundle\Serializer\ErrorNormalizer:
3+
decorates: 'api_platform.problem.normalizer.error'
4+
arguments: [ '@.inner' ]
5+

tests/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -622,16 +622,12 @@ public function testJsonProblemConfiguration(): void
622622
$services = [
623623
// problem.xml
624624
'api_platform.problem.encoder',
625-
'api_platform.problem.normalizer.constraint_violation_list',
626-
'api_platform.problem.normalizer.error',
627625
];
628626

629627
$this->assertContainerHas($services, []);
630628

631629
// problem.xml
632630
$this->assertServiceHasTags('api_platform.problem.encoder', ['serializer.encoder']);
633-
$this->assertServiceHasTags('api_platform.problem.normalizer.constraint_violation_list', ['serializer.normalizer']);
634-
$this->assertServiceHasTags('api_platform.problem.normalizer.error', ['serializer.normalizer']);
635631
}
636632

637633
public function testGraphQlConfiguration(): void
@@ -1292,4 +1288,26 @@ public function testGraphQlLegacyConfigurationInDebugMode(): void
12921288
(new ApiPlatformExtension())->load($config, $this->container);
12931289
$this->assertTrue($this->container->hasDefinition('api_platform.graphql.resolver.factory.item'));
12941290
}
1291+
1292+
/**
1293+
* @group legacy
1294+
*/
1295+
public function testLegacyJsonProblemConfiguration(): void
1296+
{
1297+
$config = self::DEFAULT_CONFIG;
1298+
$config['api_platform']['defaults']['extra_properties'] = ['rfc_7807_compliant_errors' => false];
1299+
(new ApiPlatformExtension())->load($config, $this->container);
1300+
1301+
$services = [
1302+
// problem.xml
1303+
'api_platform.problem.normalizer.constraint_violation_list',
1304+
'api_platform.problem.normalizer.error',
1305+
];
1306+
1307+
$this->assertContainerHas($services, []);
1308+
1309+
// problem.xml
1310+
$this->assertServiceHasTags('api_platform.problem.normalizer.constraint_violation_list', ['serializer.normalizer']);
1311+
$this->assertServiceHasTags('api_platform.problem.normalizer.error', ['serializer.normalizer']);
1312+
}
12951313
}

tests/Symfony/Bundle/Test/ApiTestCaseTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ private function recreateSchema(array $options = []): void
345345
*/
346346
public function testExceptionNormalizer(): void
347347
{
348+
$container = static::getContainer();
349+
if (true === static::$kernel->getContainer()->getParameter('api_platform.rfc_7807_compliant_errors')) {
350+
$this->markTestSkipped();
351+
}
352+
348353
$response = self::createClient()->request('GET', '/issue5921', [
349354
'headers' => [
350355
'accept' => 'application/json',

0 commit comments

Comments
 (0)