Problem/Motivation
Split from #2794431-121: [META] Formalize translations support. Postponed on #3199696: Add language support to ResourceObject.
Steps to reproduce
Proposed resolution
Remaining tasks
Write tests- Test suggestion: Test what happens when a header is sent with specific language to a canonical url which should return the default language.
- Address feedback #8 in https://www.drupal.org/project/drupal/issues/2794431#comment-13935684
User interface changes
None
API changes
Data model changes
None
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #36 | 3199697-nr-bot.txt | 10.69 KB | needs-review-queue-bot |
| #34 | 3199697-nr-bot.txt | 10.69 KB | needs-review-queue-bot |
| #23 | 3199697-nr-bot.txt | 7.49 KB | needs-review-queue-bot |
Issue fork drupal-3199697
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:
Comments
Comment #3
bbrala#3199696: Add language support to ResourceObject is in review which should unblock this.
Comment #5
bbrala#3199696: Add language support to ResourceObject has been committed. No longer postponed :)
Comment #6
wim leersOoohhh! 🤩
Comment #8
bbralaTodo from the parent issue
See comments in the parent issue: https://www.drupal.org/project/drupal/issues/2794431#comment-13935684
Probable issue in regards to using request attributes
Also the implementation at #3199696: Add language support to ResourceObject has the following comment by @larowlan (and @bradjones1 for that matter).
So i the end we removed that part of the patch. We have not discussed this further, so there might still be merit. But i feel this is probably a dead end.
Missing tests
Eventually we do needs tests also :)
Comment #9
bbrala#124.2: I'm removing the whole request attribute part of the change.
#124.4: I don't think this can happen since it checkes the languages supllied to the header language it seems? But this is a good candidate for a test. Added to tasks in IS.
Whats left by @gabesullice
I know that this attribute will not be set on collection requests, but can we add a guard like this so that we never accidentally apply this to collection responses?
(We should probably add
getCardinalitytoTopLevelDataInterfacewhile we're at it so we don't need a check against the concrete class, but that could be a follow up)Comment #10
bbralaMy editor is not being coorperative. Have not removed the resource attribute.
Comment #11
bbralaGoing through the code the following todo's are in the code:
Other todo's are dependend on other issues.
Comment #15
plachI'll try to push this forward a bit :)
Comment #16
bbrala<3
Comment #18
plachComment #19
plachThe commit above adds GET test coverage, the rest is still to do.
Comment #20
plachOk, this should be ready for reviews for reals now :)
Comment #22
wim leersTHANK YOU SO MUCH, @plach! 🙏🥳
Posted an initial very high-level review, looking at the broad strokes of this impressive (and big! 2000 LoC!) MR. 🤩
Completely reviewing this requires understanding lower level pieces of our JSON:API implementation, knowledge of which has evaporated my brain over the past half decade 😅😶🌫️
For now, I'm especially interested in understanding why this is not extending
ResourceTestBasebutJsonApiFunctionalTestBase. It might be fine/safe, but I'd love to A) understand it, B) have the rationale written down somewhere 🤓Comment #23
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #24
plach@Wim Leers
Thanks for the review and sorry for the belated reply!
(I'd need more birthdays to work on this regularly ;)
I think (it's been a while for me as well), I was just hoping that, as this is an experimental module, we could get away with only generic test coverage. Providing test coverage for every entity type seemed overkill at the time, as we did not discuss any entity-type specific logic. OTOH since the scaffolding is already there now, I assume it should not be a huge amount of work to try and leverage
ResourceTestBaseinstead. I'll give it a try, stay tuned :)Comment #25
plachI had a look and I'm not sure that providing a
ResourceTranslationTestBaseclass extendingResourceTestBaseis the way to go: we would need to duplicate the children classes (e.g.EntityTestTest), reimplementing the abstract methods twice with the same logic.I'd rather provide a test trait that could be used by both
JsonApiTranslationFunctionalTestand any resource-specific test class, e.g.EntityTestTranslationTest(extendingEntityTestTest). We could then extendTestCoverageTestto check for the translation test classes presence and ensure all resources also provide translation test coverage.Comment #26
bbralaOnly way i can see that happen is if we integrate these tests into resourcetestbase and adjust the jsonapi tests. But testing an experimental module in the main jsonapi module kinda feels weird. :x
Comment #27
bbralaSince i worked on this issue earlier, i'll keep it in the back of my mind to do a round of tweaks on.
Comment #28
bbralaRebased and refactored a lot, and did all the feedback.
Unfortunately i ended up in a testfailure in this test:
This should be the result of refactoring the checks on enities to a check on the resourcetype. Might be because it not uses the entity type instead of the entity itself in
checkEntityTranslatability(now renamed tocheckResourceTypeTranslatability).Time for today is up unfortunately.
Comment #29
bbralaJust a note. I did not handle the changes in the tests as discussed in #24
Comment #30
bbralaComment #31
bbralaOne of the mentioned issues is #2430335: Browser language detection is not cache aware. Did some work there to see if that can actually get fixed sometime soon.
Comment #32
bbralaAll green :)
Comment #33
smustgrave commentedI see that tests have been added so scratched that task out.
Would you all say the other tasks have been addressed?
Comment #34
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #35
bbralaRebased, added experimental flag, went through threads and resolved what was needed.
Comment #36
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #37
bbralaFixed phpstan issues and rebased (me like cache).
Comment #38
bbralaComment #41
tuwebo commentedAbout my comment in the MR:
I am using jsonapi_extras module, which defines the class
Drupal\jsonapi_extras\ResourceType\ConfigurableResourceTypeextendingDrupal\jsonapi\ResourceType\ResourceType.The module also provides a decorator repository
Drupal\jsonapi_extras\ResourceType\ConfigurableResourceTypeRepositorywhich overridescreateResourceType().The implementation calls the parent method and then instantiates a new ConfigurableResourceType based on the returned ResourceType.
My implementation currently looks like this (note the use of $resource_type->isTranslatable()):
While testing this, I occasionally encountered the following error:
This error seems to happen when all caches are cleared and I perform:
https://{{DOMAIN}}/jsonapi/node/mybundle/{{mybundle_UUID}}NOTE: No langCode neither Accept-Language have been added to the Request.
ResourceType objects are cached by ResourceTypeRepository, which means they are serialized and later unserialized from the cache backend.
If a typed property is introduced without a default value or the property is not accesible (private visibility), serializing/unserializing objects will not contain that property. When such an object is later unserialized and the property is accessed, PHP throws the “must not be accessed before initialization” error.
To prevent this, I've modified the property declaration in ResourceType to ensure it always has a default value (which may or may not have effect) but most important I've changed the visibility to be protected which seems to solve the issue at serialization time:
protected bool $isTranslatable = FALSE;This avoids accessing an uninitialized typed property.
However, initializing the property with a default value may also mask the underlying cache compatibility issue: older cached objects that do not contain the property will now behave as if $isTranslatable were FALSE, which may not reflect the actual runtime value. In practice this means the fatal error disappears, but stale cached objects may temporarily behave incorrectly until caches are rebuilt.
I am still investigating the exact conditions that trigger this behaviour and will report further findings once I have a clearer understanding of the root cause.
BTW I might be totally wrong since I've just jump in into this issue and I am getting lost with the code and "double layer" caching at this point.