Problem/Motivation
Nothing is throwing \Drupal\Core\TypedData\Exception\ReadOnlyException, even though it was added for a reason in #1696640: Implement API to unify entity properties and fields. It was present in the very first patch there: #1696640-3: Implement API to unify entity properties and fields. It's just that nothing has used it so far!
Drupal 8 core has several computed property typed data classes:
\Drupal\text\TextProcessed\Drupal\datetime\DateTimeComputed
All of them have a setValue() method that actually sets a value, instead of throwing \Drupal\Core\TypedData\Exception\ReadOnlyException. Because the docs of \Drupal\Core\TypedData\TypedDataInterface::setValue() say this:
* @throws \Drupal\Core\TypedData\Exception\ReadOnlyException
* If the data is read-only.
Which of course is pretty bad.
It also means that when you do a PATCH or POST request via the core REST module (or the contrib JSON API or GraphQL modules), you don't get an error message, but you should!
Proposed resolution
- Fix
::setValue()method of:\Drupal\text\TextProcessed\Drupal\datetime\DateTimeComputed
- Fix
\Drupal\serialization\Normalizer\FieldItemNormalizer::denormalize()to only set non-computed properties' values
Remaining tasks
None.
User interface changes
None.
API changes
None.
Data model changes
None.
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | 2921890-6.patch | 3.18 KB | dagmar |
| #2 | 2921890-2.patch | 3.53 KB | wim leers |
Comments
Comment #2
wim leersComment #3
wim leersComment #4
wim leersThis will need explicit REST test coverage.
Comment #6
dagmarRe-rolled. #2 didn't apply.
Comment #8
amateescu commentedQuoting the issue title:
Why is that? Computed does not mean read only..
Comment #9
yched commented"Computed does not mean read only"
Indeed. I think I remember struggling a bit with @fago and @Berdir so that the 'target_id' (stored) and 'entity' (computed) properties of an entity_ref field can be set both ways :
IIRC that was related to the "autocreate taxo tags" feature, and that was kind of a pain to make it work :-)
Comment #10
amateescu commentedThe parent issue arrived at the same conclusion in the meantime :)
Comment #11
wim leersSee #2972988-5: Error when saving a denormalized entity with text fields with "processed" property — this really is a problem. But I should've made this issue not about computed properties, but read-only computed properties.
Anyway, I hope to now see you all over at #2972988: Error when saving a denormalized entity with text fields with "processed" property!