diff -u b/core/modules/rest/src/ResourceValidationTrait.php b/core/modules/rest/src/ResourceValidationTrait.php --- b/core/modules/rest/src/ResourceValidationTrait.php +++ b/core/modules/rest/src/ResourceValidationTrait.php @@ -3,6 +3,7 @@ namespace Drupal\rest; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\FieldableEntityInterface; use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; trait ResourceValidationTrait { @@ -11,12 +12,16 @@ * Verifies that the whole entity does not violate any validation constraints. * * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity fields are attached to. + * The entity to validate. * * @throws \Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException * If validation errors are found. */ protected function validate(EntityInterface $entity) { + // @todo Remove when https://www.drupal.org/node/2164373 is committed. + if (!$entity instanceof FieldableEntityInterface) { + return; + } $violations = $entity->validate(); // Remove violations of inaccessible fields as they cannot stem from our @@ -28,10 +33,6 @@ foreach ($violations as $violation) { $message .= $violation->getPropertyPath() . ': ' . $violation->getMessage() . "\n"; } - // Instead of returning a generic 400 response we use the more specific - // 422 Unprocessable Entity code from RFC 4918. That way clients can - // distinguish between general syntax errors in bad serializations (code - // 400) and semantic errors in well-formed requests (code 422). throw new UnprocessableEntityHttpException($message); } }