Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
ResourceResponse should treat \stdClass as an array. Otherwise we receive an exception because we cannot find a normalizer.
I found this problem while working on #2403307: RPC endpoints for user authentication: log in, check login status, log out because I need to use a stdClass object with the current user and session data.
Patch attached.
Comment | File | Size | Author |
---|---|---|---|
#12 | resourcehandler_can_t-2427811-12.patch | 637 bytes | neclimdul |
#4 | treat_stdclass_as_an-2427811-4.patch | 1008 bytes | isholgueras |
rest-stdclass.patch | 670 bytes | marthinal | |
Comments
Comment #1
dawehnerIs this really a major issue? It sounds more like a normal task, for a small developer experience.
I guess in general we should have a test as well.
Comment #2
Crell CreditAttribution: Crell at Palantir.net commentedAgreed it's not major, but I'd like to see it. A quick test and we should be good to go.
Comment #3
isholgueras CreditAttribution: isholgueras commentedWorking on it.
Comment #4
isholgueras CreditAttribution: isholgueras commentedThe previous patch doesn't apply right. So I reroll and now apply right. This is the new patch with the same functionality.
Now I will write the tests.
Comment #5
marthinal CreditAttribution: marthinal commentedTo run the test you need to change the status to needs review.
Comment #6
isholgueras CreditAttribution: isholgueras commentedworking on tests
Comment #7
Wim LeersTests still needed.
Comment #8
neclimdulI think this might be the wrong approach and actually puts the solution before the problem.
It happens to work because currently ResourceHandler can't handle serializing "empty" objects and arrays because they evaluate to false which is a bug (#2661642: ResourceResponse can't serialize empty array). Once that is fixed, this fails for the case of an empty object because
json_encode((array) new \stdClass()) != json_encode(new \stdClass())
Ideally the serializer would know how to serialize a bare class the same way it knows how to serialize an array. I don't know the code well enough to know if that is feasible.
Comment #9
Wim LeersLet's postpone this on that issue then.
Comment #10
Wim LeersIt's not clear to me nor to damiankloip why exactly this is necessary. Could you provide a concrete use case/example in the IS?
Comment #11
Wim Leers#2661642: ResourceResponse can't serialize empty array landed, this is unblocked.
This still needs test coverage.
Comment #12
neclimdulTests, no implementation though. Should be easier to implement now that the previous patch fixed that ability to post empty data.
Comment #14
dawehnerThis seems really related to #2419825: Make serialization_class optional
Comment #15
neclimdulYeah sort of related. Its would probably be broken, though I'm not sure were, if it provided objects instead of an array containing scalars. Arrays and scalars work fine.
Comment #18
Wim LeersThere's no more need to use
\stdClass
. You should always use actual classes/interfaces. Otherwise… just usearray
. And as #15 says: arrays work fine.I don't see why we would want to support
\stdClass
.Comment #19
nikunjkotechaOne reason to support stdClass explained in this article
We are supporting this today using custom normaliser, it would be great to have support in CORE.