Commit badbcd33 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2813755 by Wim Leers, dawehner, tedbow, damiankloip: JSON responses...

Issue #2813755 by Wim Leers, dawehner, tedbow, damiankloip: JSON responses encoded inconsistently: make them all RFC4627-compliant
parent f53358d2
Loading
Loading
Loading
Loading
+11
Changes for core/lib/Drupal/Core/EventSubscriber/ExceptionJsonSubscriber.php: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -93,4 +93,15 @@ public function on415(GetResponseForExceptionEvent $event) {
    $event->setResponse($response);
  }

  /**
   * Handles a 422 error for JSON.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
   *   The event to process.
   */
  public function on422(GetResponseForExceptionEvent $event) {
    $response = new JsonResponse(['message' => $event->getException()->getMessage()], Response::HTTP_UNPROCESSABLE_ENTITY);
    $event->setResponse($response);
  }

}
+3 −17
Changes for core/modules/hal/src/Encoder/JsonEncoder.php: 3 added lines, 17 removed lines.
Original line number Diff line number Diff line
@@ -2,34 +2,20 @@

namespace Drupal\hal\Encoder;

use Symfony\Component\Serializer\Encoder\JsonEncoder as SymfonyJsonEncoder;
use Drupal\serialization\Encoder\JsonEncoder as SerializationJsonEncoder;

/**
 * Encodes HAL data in JSON.
 *
 * Simply respond to hal_json format requests using the JSON encoder.
 */
class JsonEncoder extends SymfonyJsonEncoder {
class JsonEncoder extends SerializationJsonEncoder {

  /**
   * The formats that this Encoder supports.
   *
   * @var string
   */
  protected $format = 'hal_json';

  /**
   * {@inheritdoc}
   */
  public function supportsEncoding($format) {
    return $format == $this->format;
  }

  /**
   * {@inheritdoc}
   */
  public function supportsDecoding($format) {
    return $format == $this->format;
  }
  protected static $format = ['hal_json'];

}
+3 −15
Changes for core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php: 3 added lines, 15 removed lines.
Original line number Diff line number Diff line
@@ -565,11 +565,7 @@ public function testPost() {
    $response = $this->request('POST', $url, $request_options);
    $label_field = $this->entity->getEntityType()->hasKey('label') ? $this->entity->getEntityType()->getKey('label') : static::$labelFieldName;
    $label_field_capitalized = ucfirst($label_field);
    // @todo Uncomment, remove next 3 in https://www.drupal.org/node/2813755.
    // $this->assertErrorResponse(422, "Unprocessable Entity: validation failed.\ntitle: <em class=\"placeholder\">Title</em>: this field cannot hold more than 1 values.\n", $response);
    $this->assertSame(422, $response->getStatusCode());
    $this->assertSame([static::$mimeType], $response->getHeader('Content-Type'));
    $this->assertSame($this->serializer->encode(['message' => "Unprocessable Entity: validation failed.\n$label_field: <em class=\"placeholder\">$label_field_capitalized</em>: this field cannot hold more than 1 values.\n"], static::$format), (string) $response->getBody());
    $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\n$label_field: <em class=\"placeholder\">$label_field_capitalized</em>: this field cannot hold more than 1 values.\n", $response);


    $request_options[RequestOptions::BODY] = $parseable_invalid_request_body_2;
@@ -577,11 +573,7 @@ public function testPost() {

    // DX: 422 when invalid entity: UUID field too long.
    $response = $this->request('POST', $url, $request_options);
    // @todo Uncomment, remove next 3 in https://www.drupal.org/node/2813755.
    // $this->assertErrorResponse(422, "Unprocessable Entity: validation failed.\nuuid.0.value: <em class=\"placeholder\">UUID</em>: may not be longer than 128 characters.\n", $response);
    $this->assertSame(422, $response->getStatusCode());
    $this->assertSame([static::$mimeType], $response->getHeader('Content-Type'));
    $this->assertSame($this->serializer->encode(['message' => "Unprocessable Entity: validation failed.\nuuid.0.value: <em class=\"placeholder\">UUID</em>: may not be longer than 128 characters.\n"], static::$format), (string) $response->getBody());
    $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nuuid.0.value: <em class=\"placeholder\">UUID</em>: may not be longer than 128 characters.\n", $response);


    $request_options[RequestOptions::BODY] = $parseable_invalid_request_body_3;
@@ -765,11 +757,7 @@ public function testPatch() {
    $response = $this->request('PATCH', $url, $request_options);
    $label_field = $this->entity->getEntityType()->hasKey('label') ? $this->entity->getEntityType()->getKey('label') : static::$labelFieldName;
    $label_field_capitalized = ucfirst($label_field);
    // @todo Uncomment, remove next 3 in https://www.drupal.org/node/2813755.
    // $this->assertErrorResponse(422, "Unprocessable Entity: validation failed.\ntitle: <em class=\"placeholder\">Title</em>: this field cannot hold more than 1 values.\n", $response);
    // $this->assertSame(422, $response->getStatusCode());
    // $this->assertSame([static::$mimeType], $response->getHeader('Content-Type'));
    $this->assertSame($this->serializer->encode(['message' => "Unprocessable Entity: validation failed.\n$label_field: <em class=\"placeholder\">$label_field_capitalized</em>: this field cannot hold more than 1 values.\n"], static::$format), (string) $response->getBody());
    $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\n$label_field: <em class=\"placeholder\">$label_field_capitalized</em>: this field cannot hold more than 1 values.\n", $response);


    $request_options[RequestOptions::BODY] = $parseable_invalid_request_body_2;
+3 −15
Changes for core/modules/rest/tests/src/Functional/EntityResource/User/UserResourceTestBase.php: 3 added lines, 15 removed lines.
Original line number Diff line number Diff line
@@ -163,11 +163,7 @@ public function testPatchDxForSecuritySensitiveBaseFields() {

    // DX: 422 when changing email without providing the password.
    $response = $this->request('PATCH', $url, $request_options);
    // @todo use this commented line instead of the 3 lines thereafter once https://www.drupal.org/node/2813755 lands.
    // $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nmail: Your current password is missing or incorrect; it's required to change the <em class=\"placeholder\">Email</em>.\n", $response);
    $this->assertSame(422, $response->getStatusCode());
    $this->assertSame([static::$mimeType], $response->getHeader('Content-Type'));
    $this->assertSame($this->serializer->encode(['message' => "Unprocessable Entity: validation failed.\nmail: Your current password is missing or incorrect; it's required to change the <em class=\"placeholder\">Email</em>.\n"], static::$format), (string) $response->getBody());
    $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nmail: Your current password is missing or incorrect; it's required to change the <em class=\"placeholder\">Email</em>.\n", $response);


    $normalization['pass'] = [['existing' => 'wrong']];
@@ -175,11 +171,7 @@ public function testPatchDxForSecuritySensitiveBaseFields() {

    // DX: 422 when changing email while providing a wrong password.
    $response = $this->request('PATCH', $url, $request_options);
    // @todo use this commented line instead of the 3 lines thereafter once https://www.drupal.org/node/2813755 lands.
    // $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nmail: Your current password is missing or incorrect; it's required to change the <em class=\"placeholder\">Email</em>.\n", $response);
    $this->assertSame(422, $response->getStatusCode());
    $this->assertSame([static::$mimeType], $response->getHeader('Content-Type'));
    $this->assertSame($this->serializer->encode(['message' => "Unprocessable Entity: validation failed.\nmail: Your current password is missing or incorrect; it's required to change the <em class=\"placeholder\">Email</em>.\n"], static::$format), (string) $response->getBody());
    $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nmail: Your current password is missing or incorrect; it's required to change the <em class=\"placeholder\">Email</em>.\n", $response);


    $normalization['pass'] = [['existing' => $this->account->passRaw]];
@@ -200,11 +192,7 @@ public function testPatchDxForSecuritySensitiveBaseFields() {

    // DX: 422 when changing password without providing the current password.
    $response = $this->request('PATCH', $url, $request_options);
    // @todo use this commented line instead of the 3 lines thereafter once https://www.drupal.org/node/2813755 lands.
    // $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\npass: Your current password is missing or incorrect; it's required to change the <em class=\"placeholder\">Password</em>.\n", $response);
    $this->assertSame(422, $response->getStatusCode());
    $this->assertSame([static::$mimeType], $response->getHeader('Content-Type'));
    $this->assertSame($this->serializer->encode(['message' => "Unprocessable Entity: validation failed.\npass: Your current password is missing or incorrect; it's required to change the <em class=\"placeholder\">Password</em>.\n"], static::$format), (string) $response->getBody());
    $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\npass: Your current password is missing or incorrect; it's required to change the <em class=\"placeholder\">Password</em>.\n", $response);


    $normalization['pass'][0]['existing'] = $this->account->pass_raw;
+1 −3
Changes for core/modules/rest/tests/src/Functional/ResourceTestBase.php: 1 added line, 3 removed lines.
Original line number Diff line number Diff line
@@ -340,9 +340,7 @@ protected function assertResourceResponse($expected_status_code, $expected_body,
   *   The error response to assert.
   */
  protected function assertResourceErrorResponse($expected_status_code, $expected_message, ResponseInterface $response) {
    // @todo Fix this in https://www.drupal.org/node/2813755.
    $encode_options = ['json_encode_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT];
    $expected_body = $this->serializer->encode(['message' => $expected_message], static::$format, $encode_options);
    $expected_body = $this->serializer->encode(['message' => $expected_message], static::$format);
    $this->assertResourceResponse($expected_status_code, $expected_body, $response);
  }

Loading