Problem/Motivation
A resource object's self link is supposed to be a URL that returns that resource. On a fallback read, this module publishes one that returns 400.
EntityResource::buildWrappedResponse() rewrites the self link of every individual read so it can carry the hreflang advertisement, and while doing so it strips the langCode query parameter. That part is deliberate: the self link is meant to be the language-neutral canonical URL, with the available translations advertised on it. What it does not strip is includeFallback.
includeFallback is only valid alongside langCode. EntityResource::isFallbackRequested() rejects it otherwise, by design, so that cache variants stay canonical:
The "includeFallback" query parameter requires the "langCode" query parameter.
So the link the server publishes carries exactly the one parameter that makes the request invalid once its companion has been removed. Any client that follows self links, which is the ordinary JSON:API idiom and what generic client libraries do unprompted, gets a 400 on every fallback read.
Steps to reproduce
- A translatable entity, and any configured language.
GET /jsonapi/node/article/{uuid}?langCode=fr&includeFallback=1→200, as expected.- Read
data.links.self.hreffrom the response body. - Observed:
https://example.com/jsonapi/node/article/{uuid}?includeFallback=1 GETthat URL →400 Bad Request. Expected: the same resource.
Captured verbatim from the regression test written before the fix:
Failed asserting that
'https://example.com/jsonapi/node/article/{uuid}?includeFallback=1'
does not contain "includeFallback".
Strict reads (?langCode=fr alone) are unaffected: the only language parameter present is already stripped, so the published link is the clean canonical URL.
Proposed resolution
Strip both language selectors when building the self link, not just one.
buildWrappedResponse()unsetsPARAM_INCLUDE_FALLBACKalongside the existingPARAM_LANGCODE. One line, using the class constants as the surrounding code does.- The rule this encodes, worth stating for whoever adds the next language parameter: any parameter that is only valid alongside
langCodemust not survive into theselflink, because the link is published withoutlangCode. - Every other query parameter is still preserved.
?include=and?fields[]=belong in theselflink and continue to appear there, matching core JSON:API.
Explicitly not changed: the stripping of langCode itself. Whether the self link should instead retain langCode and point at the specific translation is a contract question for #3199697, not a bug. This change only makes the existing design self-consistent.
Issue fork jsonapi_multilingual-3614408
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 #4
lauriii