Skip to content

Commit

Permalink
Allow wildcard Accept header for CRUDController AJAX requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen van den Nieuwenhuisen committed Jul 29, 2020
1 parent ca3d5c3 commit 8f7c365
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"symfony/css-selector": "^4.4",
"symfony/filesystem": "^4.4",
"symfony/maker-bundle": "^1.17",
"symfony/phpunit-bridge": "^5.1",
"symfony/phpunit-bridge": "^5.1.1",
"symfony/yaml": "^4.4"
},
"suggest": {
Expand Down
8 changes: 4 additions & 4 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1563,8 +1563,8 @@ private function setFormTheme(FormView $formView, ?array $theme = null): void

private function handleXmlHttpRequestErrorResponse(Request $request, FormInterface $form): ?JsonResponse
{
if (!\in_array('application/json', $request->getAcceptableContentTypes(), true)) {
@trigger_error('In next major version response will return 406 NOT ACCEPTABLE without `Accept: application/json`', E_USER_DEPRECATED);
if (empty(array_intersect(['application/json', '*/*'], $request->getAcceptableContentTypes()))) {
@trigger_error('In next major version response will return 406 NOT ACCEPTABLE without `Accept: application/json` or `Accept: */*`', E_USER_DEPRECATED);

return null;
}
Expand All @@ -1585,8 +1585,8 @@ private function handleXmlHttpRequestErrorResponse(Request $request, FormInterfa
*/
private function handleXmlHttpRequestSuccessResponse(Request $request, $object): JsonResponse
{
if (!\in_array('application/json', $request->getAcceptableContentTypes(), true)) {
@trigger_error('In next major version response will return 406 NOT ACCEPTABLE without `Accept: application/json`', E_USER_DEPRECATED);
if (empty(array_intersect(['application/json', '*/*'], $request->getAcceptableContentTypes()))) {
@trigger_error('In next major version response will return 406 NOT ACCEPTABLE without `Accept: application/json` or `Accept: */*`', E_USER_DEPRECATED);
}

return $this->renderJson([
Expand Down
7 changes: 5 additions & 2 deletions tests/Controller/CRUDControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use Sonata\Exporter\Exporter;
use Sonata\Exporter\Source\SourceIteratorInterface;
use Sonata\Exporter\Writer\JsonWriter;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine;
use Symfony\Component\DependencyInjection\Container;
Expand Down Expand Up @@ -74,6 +75,8 @@
*/
class CRUDControllerTest extends TestCase
{
use ExpectDeprecationTrait;

/**
* @var CRUDController
*/
Expand Down Expand Up @@ -1665,7 +1668,6 @@ public function testEditActionAjaxError(): void

/**
* @legacy
* @expectedDeprecation In next major version response will return 406 NOT ACCEPTABLE without `Accept: application/json`
*/
public function testEditActionAjaxErrorWithoutAcceptApplicationJson(): void
{
Expand Down Expand Up @@ -1706,6 +1708,7 @@ public function testEditActionAjaxErrorWithoutAcceptApplicationJson(): void
->method('trans')
->willReturn('flash message');

$this->expectDeprecation('In next major version response will return 406 NOT ACCEPTABLE without `Accept: application/json` or `Accept: */*`');
$this->assertInstanceOf(Response::class, $response = $this->controller->editAction(null));
$this->assertSame($this->admin, $this->parameters['admin']);
$this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']);
Expand Down Expand Up @@ -2363,7 +2366,6 @@ public function testCreateActionAjaxError(): void

/**
* @legacy
* @expectedDeprecation In next major version response will return 406 NOT ACCEPTABLE without `Accept: application/json`
*/
public function testCreateActionAjaxErrorWithoutAcceptApplicationJson(): void
{
Expand Down Expand Up @@ -2408,6 +2410,7 @@ public function testCreateActionAjaxErrorWithoutAcceptApplicationJson(): void
->method('trans')
->willReturn('flash message');

$this->expectDeprecation('In next major version response will return 406 NOT ACCEPTABLE without `Accept: application/json` or `Accept: */*`');
$this->assertInstanceOf(Response::class, $response = $this->controller->createAction());
$this->assertSame($this->admin, $this->parameters['admin']);
$this->assertSame('@SonataAdmin/ajax_layout.html.twig', $this->parameters['base_template']);
Expand Down

0 comments on commit 8f7c365

Please sign in to comment.