Problem/Motivation

Discovered at #2930028-14: Comprehensive JSON API integration test coverage phase 1: for every entity type, individual resources only:

1) Drupal\Tests\jsonapi\Functional\NodeTest::testPostIndividual
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-{"errors":[{"title":"Unprocessable Entity","status":422,"detail":"Unprocessable Entity: validation failed.\ntitle: Title: this field cannot hold more than 1 values.\n","links":{"info":"http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html"},"code":0}]}
+{"errors":[{"title":"Unprocessable Entity","status":422,"detail":"title: \u003Cem class=\u0022placeholder\u0022\u003ETitle\u003C\/em\u003E: this field cannot hold more than 1 values.","code":0,"source":{"pointer":"\/data\/attributes\/title"}}]}

Hurray, that is the error we need! It just is formatted in a different way. That should be easy enough to fix. Continuing tomorrow :)

The root cause? \Drupal\jsonapi\Normalizer\UnprocessableHttpEntityExceptionNormalizer::buildErrorObjects() simply passes on the return value from \Symfony\Component\Validator\ConstraintViolation::getMessage(), which is for Drupal entity/field validation returning HTML. Hence the JSON-encoded <em class="placeholder">Title[…] message. Which is obviously less than ideal.

Proposed resolution

Almost exactly a year ago, this was fixed in core REST, in #2835683: Remove HTML from EntityResource validation 422 exception message. Apply the same fix: convert to plain text.

CommentFileSizeAuthor
#2 2934370-2.patch2.29 KBwim leers

Comments

Wim Leers created an issue. See original summary.

wim leers’s picture

Status: Active » Needs review
StatusFileSize
new2.29 KB
wim leers’s picture

Status: Needs review » Reviewed & tested by the community

Green!

  • Wim Leers committed b9efc0b on 8.x-1.x
    Issue #2934370 by Wim Leers: Entity validation constraint violation...
wim leers’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.