Hi there,
I am having some issues when updating unique fields via the JSON API module and any help will be greatly appreciated. I hope I manage to explain it fine:
I have created some Constraints to make a field unique in a content type and in a couple of taxonomy terms.
When I create a new content or taxonomy via the JSON API Extra (I am using Postman to test) it works fine: I can try to create a new content that tries to set an existing value into the unique field and as expected it prevents me:
Here my field "talk_number" has the constraint to be unique (I have created an alias for it in JSON API Extra from "field_talk_number" to "talk_number")

POST api/node/talk:
(where talk_number is unique)

{
	"data":{
		"type": "talk",
		"attributes": {
			"title": "A NEW Talk created using JSON API Extras ",
			"talk_number": "1960001"
		}
	}
}

and the response:

  "errors": [
        {
            "title": "Unprocessable Entity",
            "status": "422",
            "detail": "field_talk_number: Talk number 1960001 is not unique",
            "source": {
                "pointer": "/data/attributes/field_talk_number"
            }
        }
    ]

However when I am trying to update this content/taxonomy and try to set an existing value to the unique field it fails: it updates disregarding the constraint. If I try the same operation from the drupal form it correctly prevents me from updating it.

PATCH api/node/talk/16f868dc-e7ef-4860-9c18-f1431f1567b7

{
	"data":{
		"type": "talk",
		"id": "16f868dc-e7ef-4860-9c18-f1431f1567b7",
		"attributes": {
			"title": "Updating Talk created using JSON API Extras ",
			"talk_number": "1960001"
		}
	}
}

I have attached a screenshot of the feedback I receive when trying to update from the form.

I have added the constraint to the talk number inside a hook_entity_bundle_field_info_alter() like so:

if (isset($fields['field_talk_number'])) {
      // Use the ID as defined in the annotation of the constraint definition
      $fields['field_talk_number']->addConstraint('UniqueTalkNumber');
    }
  }

Is there anything that I might be missing or might this be an issue with the JSON API modules?
thanks for your help!
Luis

Comments

lecabori created an issue. See original summary.

lecabori’s picture

Issue summary: View changes
lecabori’s picture

Issue summary: View changes
lecabori’s picture

looking further into this I noticed that the issue arises from the creating an aliases for my unique field (from "field_talk_number" to "talk_number")

lecabori’s picture

Issue summary: View changes
lecabori’s picture

Issue summary: View changes
e0ipso’s picture

Project: JSON:API Extras » JSON:API
Version: 8.x-3.7 » 8.x-2.x-dev

Thanks for the report @lecabori. I think this belongs to the JSON:API issue queue. That's the module doing the heavy lifting, including validations and processing field aliases.

lecabori’s picture

thanks @e0ipso , I will post it there then
oh, I just noticed you did, thanks
cheers

e0ipso’s picture

Don't worry about that @lecabori I have already moved the issue. :-)

lecabori’s picture

Status: Active » Needs review
StatusFileSize
new55.77 KB
new786 bytes

I have created a patch for what I think will fix this issue, and that is passing to the validate() function the internal field names for the fields received in the request

wim leers’s picture

Project: JSON:API » Drupal core
Version: 8.x-2.x-dev » 8.8.x-dev
Component: Code » jsonapi.module
Issue tags: +API-First Initiative, +Needs tests

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

bbrala’s picture

Status: Needs review » Postponed (maintainer needs more info)

I think this issue might be outdated, there have been quite some changes by now in how fields are handled, and aliasing is now in core. Can we get confirmation this issue still exists?

bbrala’s picture

Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)

I'm cleaning up the issue queue, since there is no more info supplied for such a long time i'm going to assume this issue fixed as mentioned in #13. If you run into this issue, please reopen this or submit a new issue.