Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add correct accept header to ajaxSubmit to prevent 406 error #6222

Merged
merged 2 commits into from
Jul 29, 2020
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
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",
jordisala1991 marked this conversation as resolved.
Show resolved Hide resolved
"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
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ This code manages the many-to-[one|many] association field popup
jQuery(form).ajaxSubmit({
url: url,
type: type,
headers: {
Accept: 'application/json'
},
data: data,
success: function(data) {
Admin.log('[{{ id }}|field_dialog_form_action] ajax success');
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