Skip to content
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
16 changes: 15 additions & 1 deletion src/State/ApiResource/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
routeName: '_api_errors',
outputFormats: [
'json' => ['application/problem+json', 'application/json'],
'xml' => ['application/xml', 'text/xml'],
],
hideHydraOperation: true,
normalizationContext: [
Expand Down Expand Up @@ -75,6 +74,21 @@
'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
],
),
new Operation(
errors: [],
name: '_api_errors_xml',
routeName: '_api_errors',
outputFormats: [
'xml' => ['application/xml', 'text/xml'],
],
hideHydraOperation: true,
normalizationContext: [
SchemaFactory::OPENAPI_DEFINITION_NAME => '',
'groups' => ['jsonproblem'],
'skip_null_values' => true,
'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
],
),
new Operation(
name: '_api_errors',
hideHydraOperation: true,
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/EventListener/ErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ private function getFormatOperation(?string $format): string
'jsonproblem' => '_api_errors_problem',
'jsonld' => '_api_errors_hydra',
'jsonapi' => '_api_errors_jsonapi',
'xml' => '_api_errors_xml',
'html' => '_api_errors_problem', // This will be intercepted by the SwaggerUiProvider
default => '_api_errors_problem',
};
Expand Down
12 changes: 11 additions & 1 deletion src/Validator/Exception/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
name: '_api_validation_errors_problem',
outputFormats: [
'json' => ['application/problem+json'],
'xml' => ['application/xml', 'text/xml'],
],
normalizationContext: [
'groups' => ['json'],
Expand Down Expand Up @@ -79,6 +78,17 @@
'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
]
),
new ErrorOperation(
name: '_api_validation_errors_xml',
outputFormats: [
'xml' => ['application/xml', 'text/xml'],
],
normalizationContext: [
'groups' => ['json'],
'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
'skip_null_values' => true,
]
),
],
graphQlOperations: []
)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7287;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\NotExposed;

#[ApiResource(
outputFormats: ['jsonld'],
operations: [
new GetCollection(provider: [self::class, 'provide']),
new NotExposed(uriVariables: ['id']),
]
)]
class OperationWithDefaultFormat
{
public function __construct(
public string $id,
) {
}

public static function provide(): array
{
return [new self('1')];
}
}
1 change: 1 addition & 0 deletions tests/Fixtures/app/config/config_common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ api_platform:
jsonld: ['application/ld+json']
jsonapi: ['application/vnd.api+json']
html: ['text/html']
xml: ['application/xml', 'text/xml']
graphql:
enabled: true
nesting_separator: __
Expand Down
65 changes: 65 additions & 0 deletions tests/Functional/ErrorFormatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Functional;

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7287\OperationWithDefaultFormat;
use ApiPlatform\Tests\SetupClassResourcesTrait;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class ErrorFormatAppKernel extends \AppKernel
{
protected function build(ContainerBuilder $container): void
{
parent::build($container);
$container->addCompilerPass(new class implements CompilerPassInterface {
public function process(ContainerBuilder $container): void
{
$def = $container->getDefinition('api_platform.error_listener');
$def->setArgument(5, ['jsonld' => ['application/ld+json']]);
}
});
}
}

final class ErrorFormatTest extends ApiTestCase
{
use SetupClassResourcesTrait;

protected static function getKernelClass(): string
{
return ErrorFormatAppKernel::class;
}

protected static ?bool $alwaysBootKernel = true;

/**
* @return class-string[]
*/
public static function getResources(): array
{
return [OperationWithDefaultFormat::class];
}

public function testNotAcceptableXml(): void
{
self::createClient()->request('GET', '/operation_with_default_formats', [
'headers' => ['accept' => 'text/xml'],
]);

$this->assertJsonContains(['detail' => 'Requested format "text/xml" is not supported. Supported MIME types are "application/ld+json".']);
$this->assertResponseStatusCodeSame(406);
}
}
Loading