diff --git a/core/modules/rest/src/Plugin/rest/resource/EntityResourceValidationTrait.php b/core/modules/rest/src/Plugin/rest/resource/EntityResourceValidationTrait.php
index a2ff40a4c4b..09b4b64baec 100644
--- a/core/modules/rest/src/Plugin/rest/resource/EntityResourceValidationTrait.php
+++ b/core/modules/rest/src/Plugin/rest/resource/EntityResourceValidationTrait.php
@@ -2,6 +2,7 @@
namespace Drupal\rest\Plugin\rest\resource;
+use Drupal\Component\Render\PlainTextOutput;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
@@ -35,7 +36,9 @@ trait EntityResourceValidationTrait {
if ($violations->count() > 0) {
$message = "Unprocessable Entity: validation failed.\n";
foreach ($violations as $violation) {
- $message .= $violation->getPropertyPath() . ': ' . $violation->getMessage() . "\n";
+ // We strip every HTML from the error message to have a nicer to read
+ // message on REST responses.
+ $message .= $violation->getPropertyPath() . ': ' . PlainTextOutput::renderFromHtml($violation->getMessage()) . "\n";
}
throw new UnprocessableEntityHttpException($message);
}
diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php
index cb11e2e7b24..17b6cfdfa90 100644
--- a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php
+++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php
@@ -568,7 +568,7 @@ abstract class EntityResourceTestBase extends ResourceTestBase {
$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);
- $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\n$label_field: $label_field_capitalized: this field cannot hold more than 1 values.\n", $response);
+ $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\n$label_field: $label_field_capitalized: this field cannot hold more than 1 values.\n", $response);
$request_options[RequestOptions::BODY] = $parseable_invalid_request_body_2;
@@ -576,7 +576,7 @@ abstract class EntityResourceTestBase extends ResourceTestBase {
// DX: 422 when invalid entity: UUID field too long.
$response = $this->request('POST', $url, $request_options);
- $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nuuid.0.value: UUID: may not be longer than 128 characters.\n", $response);
+ $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nuuid.0.value: UUID: may not be longer than 128 characters.\n", $response);
$request_options[RequestOptions::BODY] = $parseable_invalid_request_body_3;
@@ -760,7 +760,7 @@ abstract class EntityResourceTestBase extends ResourceTestBase {
$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);
- $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\n$label_field: $label_field_capitalized: this field cannot hold more than 1 values.\n", $response);
+ $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\n$label_field: $label_field_capitalized: this field cannot hold more than 1 values.\n", $response);
$request_options[RequestOptions::BODY] = $parseable_invalid_request_body_2;
diff --git a/core/modules/rest/tests/src/Functional/EntityResource/User/UserResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/User/UserResourceTestBase.php
index 412a2a46ac5..38452c2a02e 100644
--- a/core/modules/rest/tests/src/Functional/EntityResource/User/UserResourceTestBase.php
+++ b/core/modules/rest/tests/src/Functional/EntityResource/User/UserResourceTestBase.php
@@ -163,7 +163,7 @@ abstract class UserResourceTestBase extends EntityResourceTestBase {
// DX: 422 when changing email without providing the password.
$response = $this->request('PATCH', $url, $request_options);
- $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nmail: Your current password is missing or incorrect; it's required to change the Email.\n", $response);
+ $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nmail: Your current password is missing or incorrect; it's required to change the Email.\n", $response);
$normalization['pass'] = [['existing' => 'wrong']];
@@ -171,7 +171,7 @@ abstract class UserResourceTestBase extends EntityResourceTestBase {
// DX: 422 when changing email while providing a wrong password.
$response = $this->request('PATCH', $url, $request_options);
- $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nmail: Your current password is missing or incorrect; it's required to change the Email.\n", $response);
+ $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nmail: Your current password is missing or incorrect; it's required to change the Email.\n", $response);
$normalization['pass'] = [['existing' => $this->account->passRaw]];
@@ -192,7 +192,7 @@ abstract class UserResourceTestBase extends EntityResourceTestBase {
// DX: 422 when changing password without providing the current password.
$response = $this->request('PATCH', $url, $request_options);
- $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\npass: Your current password is missing or incorrect; it's required to change the Password.\n", $response);
+ $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\npass: Your current password is missing or incorrect; it's required to change the Password.\n", $response);
$normalization['pass'][0]['existing'] = $this->account->pass_raw;