PATCH is not allowed if entity translation doesn't already exist.

Created on 12 January 2023, almost 2 years ago
Updated 23 May 2024, 7 months ago

Problem/Motivation

In my case translating user entity field throws an error if it is not already translated. And of course the fields are not translated yet, because only the default English content exists after creating a user.

https://api.website.com/fi/jsonapi/user/user/c59cedc5-895d-41e6-a88c-504c5edb7278||14703||Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: The requested translation of the resource object does not exist, instead modify one of the translations that do exist: en, de. in Drupal\jsonapi\ParamConverter\EntityUuidConverter->convert() (line 72 of .../core/modules/jsonapi/src/ParamConverter/EntityUuidConverter.php).

Steps to reproduce

1. Enable three languages English /en, Finnish /fi, German /de. Leave the site default as English. /admin/config/regional/language
2. Create a custom field to the User entity: "Bio". /admin/config/people/accounts/fields
3. Enable user entity translation. /admin/config/regional/content-language
4. Set "Bio" field translateble. /admin/config/regional/content-language
5. Enable Create translations, Edit translations, Translate user permissions (not sure if all needed).
6. In the Drupal UI as admin: Translate any users "Bio" field in /de and leave /fi not translated. /de/user/123/translations/add/en/de
7A Send PATCH /jsonapi/user/user/123 to change "Bio" field. => WORKS!
7B Send PATCH /de/jsonapi/user/user/123 to change "Bio" field. => WORKS!
7C Send PATCH /fi/jsonapi/user/user/123 to change "Bio" field. => FAILS with an error shown above.

Proposed resolution

Allow adding a translation if the entity translation is allowed for that entity field and the language is enabled on the site.

Release notes snippet

The responsible code in jsonapi/src/ParamConverter/EntityUuid/Converter.php

          if ($method === 'PATCH' && $entity->language()->getId() !== $current_content_language) {
            $available_translations = implode(', ', array_keys($entity->getTranslationLanguages())); 
            throw new MethodNotAllowedHttpException(['GET'], sprintf('The requested translation of the resource object does not exist, instead modify one of the translations that do exist: %s.', $available_translations));
          }
🐛 Bug report
Status

Active

Version

11.0 🔥

Component
JSON API 

Last updated 6 days ago

Created by

🇩🇪Germany TipiT Hamburg

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇳India rishabh666

    Hi @TipiT . I would like to look into the bug.

    Actually I am very new to Drupal, and that is why i am unable to reproduce the bug. I am stuck at setting the languages as i am unable to find the language option in the Regional settings, so am i supposed to download and install a module?

Production build 0.71.5 2024