Skip to content
Snippets Groups Projects
Commit e0a8d337 authored by catch's avatar catch
Browse files

Issue #3052954 by Wim Leers, gabesullice, garphy: Incorrect use of...

Issue #3052954 by Wim Leers, gabesullice, garphy: Incorrect use of UnprocessableHttpEntityException in EntityResource::deserialize()
parent 50a6a9ef
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -30,7 +30,6 @@ ...@@ -30,7 +30,6 @@
use Drupal\jsonapi\Entity\EntityValidationTrait; use Drupal\jsonapi\Entity\EntityValidationTrait;
use Drupal\jsonapi\Access\TemporaryQueryGuard; use Drupal\jsonapi\Access\TemporaryQueryGuard;
use Drupal\jsonapi\Exception\EntityAccessDeniedHttpException; use Drupal\jsonapi\Exception\EntityAccessDeniedHttpException;
use Drupal\jsonapi\Exception\UnprocessableHttpEntityException;
use Drupal\jsonapi\IncludeResolver; use Drupal\jsonapi\IncludeResolver;
use Drupal\jsonapi\JsonApiResource\IncludedData; use Drupal\jsonapi\JsonApiResource\IncludedData;
use Drupal\jsonapi\JsonApiResource\LinkCollection; use Drupal\jsonapi\JsonApiResource\LinkCollection;
...@@ -53,6 +52,7 @@ ...@@ -53,6 +52,7 @@
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Drupal\Core\Http\Exception\CacheableBadRequestHttpException; use Drupal\Core\Http\Exception\CacheableBadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\ConflictHttpException; use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Symfony\Component\Serializer\Exception\InvalidArgumentException; use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException; use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Serializer\SerializerInterface;
...@@ -822,10 +822,10 @@ protected function deserialize(ResourceType $resource_type, Request $request, $c ...@@ -822,10 +822,10 @@ protected function deserialize(ResourceType $resource_type, Request $request, $c
// These two serialization exception types mean there was a problem with // These two serialization exception types mean there was a problem with
// the structure of the decoded data and it's not valid. // the structure of the decoded data and it's not valid.
catch (UnexpectedValueException $e) { catch (UnexpectedValueException $e) {
throw new UnprocessableHttpEntityException($e->getMessage()); throw new UnprocessableEntityHttpException($e->getMessage());
} }
catch (InvalidArgumentException $e) { catch (InvalidArgumentException $e) {
throw new UnprocessableHttpEntityException($e->getMessage()); throw new UnprocessableEntityHttpException($e->getMessage());
} }
} }
......
...@@ -1053,4 +1053,36 @@ public function testNonTranslatableEntityUpdatesFromIssue3043168() { ...@@ -1053,4 +1053,36 @@ public function testNonTranslatableEntityUpdatesFromIssue3043168() {
$this->assertSame('Constantine', $response_document['data']['attributes']['name']); $this->assertSame('Constantine', $response_document['data']['attributes']['name']);
} }
/**
* Ensure POSTing invalid data results in a 422 response, not a PHP error.
*
* @see https://www.drupal.org/project/drupal/issues/3052954
*/
public function testInvalidDataTriggersUnprocessableEntityErrorFromIssue3052954() {
$this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE);
// Set up data model.
$user = $this->drupalCreateUser(['bypass node access']);
// Test.
$request_options = [
RequestOptions::HEADERS => [
'Content-Type' => 'application/vnd.api+json',
'Accept' => 'application/vnd.api+json',
],
RequestOptions::JSON => [
'data' => [
'type' => 'article',
'attributes' => [
'title' => 'foobar',
'created' => 'not_a_date',
],
],
],
RequestOptions::AUTH => [$user->getAccountName(), $user->pass_raw],
];
$response = $this->request('POST', Url::fromUri('internal:/jsonapi/node/article'), $request_options);
$this->assertSame(422, $response->getStatusCode());
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment