Problem/Motivation
The typehint for the $deserialization_target_class parameter of \Drupal\jsonapi\ResourceType\ResourceType::__construct, and the associated property (ResourceType::$deserializationTargetClass) and accessor (ResourceType::getDeserializationTargetClass) is string, but there are several tests passing NULL instead.
Steps to reproduce
$ ./vendor/bin/phpstan analyse -c core/phpstan.neon.dist --level=5 --error-format=raw | grep deserialization_target_class | sed "s|$(pwd)/||g"
10896/10896 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
core/modules/jsonapi/tests/src/Kernel/Controller/EntityResourceTest.php:241:Parameter #3 $deserialization_target_class of class Drupal\jsonapi\ResourceType\ResourceType constructor expects string, null given.
core/modules/jsonapi/tests/src/Kernel/Query/FilterTest.php:337:Parameter #3 $deserialization_target_class of class Drupal\jsonapi\ResourceType\ResourceType constructor expects string, null given.
core/modules/jsonapi/tests/src/Kernel/Query/FilterTest.php:406:Parameter #3 $deserialization_target_class of class Drupal\jsonapi\ResourceType\ResourceType constructor expects string, null given.
core/modules/jsonapi/tests/src/Unit/EventSubscriber/ResourceResponseValidatorTest.php:84:Parameter #3 $deserialization_target_class of class Drupal\jsonapi\ResourceType\ResourceType constructor expects string, null given.
core/modules/jsonapi/tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php:50:Parameter #3 $deserialization_target_class of class Drupal\jsonapi\ResourceType\ResourceType constructor expects string, null given.
core/modules/jsonapi/tests/src/Unit/Normalizer/ResourceIdentifierNormalizerTest.php:56:Parameter #3 $deserialization_target_class of class Drupal\jsonapi\ResourceType\ResourceType constructor expects string, null given.
core/modules/jsonapi/tests/src/Unit/Normalizer/ResourceIdentifierNormalizerTest.php:61:Parameter #3 $deserialization_target_class of class Drupal\jsonapi\ResourceType\ResourceType constructor expects string, null given.
Proposed resolution
- Change typehints to
class-string - Pass
Node::classwhere appropriate, and\stdClass::classeverywhere else
Remaining tasks
FWIW, this property and accessor don't appear to be used in core, but it is in contrib. Is this something core should deprecate?
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Issue fork drupal-3557053
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3557053-deserialization-target
changes, plain diff MR !13744
Comments
Comment #3
bbralaDid you mean to "needs review" this?
Comment #4
longwaveCore does use this from ResourceTypeRepository:
and then it's read and used in JsonApiDocumentTopLevelNormalizer:
Agree that it shouldn't be nullable, although does it make sense for null to fall back to
\stdClass?Comment #5
mstrelan commentedYes, I was waiting for a green pipeline and then forgot to come back to it.
Thanks for pointing that out, I was relying on usages detected by phpstorm but should have grepped for it. You first example is just setting the value, but my point was that I didn't think it was ever accessed, which was wrong. It is also accessed in
ResourceObjectNormalizer::serializeField, but with a todo comment to remove it.I think it's probably good to force an explicit decision to be made.
I think we could probably also update the typehint in
\Drupal\jsonapi\Serializer\Serializer::selfSupportsDenormalizationto expectclass-stringin this issue as well.Comment #6
longwaveLet's deprecate passing null then we can add a typehint to the argument in D12. I don't see this being widely used so no point leaving it until D13 if we can help it.
Comment #7
mstrelan commentedDeprecated passing null, added a CR.
Comment #8
bbralaThis is looking good. Weird commit with the phpcs change (and undo), but you probably had your reasons.
Feedback was adressed and good to clean up ^^
Comment #10
catchUpdated the deprecated version to 11.3.0 on commit, this isn't real API surface so seems fine to remove in 12.0.0
Committed/pushed to 11.x, thanks!