Add support to apply automatic name pattern when using POST via JSON API

Created on 29 August 2023, about 1 year ago
Updated 31 August 2023, about 1 year ago

I work with the JSONAPI and I noticed that I got an error message:

{
    "jsonapi": {
        "version": "1.0",
        "meta": {}
    },
    "errors": [
        {
            "title": "Unprocessable Entity",
            "status": "422",
            "detail": "name: This value should not be null.",
            "source": {
                "file": "/foo......./modules/jsonapi/src/Entity/EntityValidationTrait.php",
                "line": 59,
                "pointer": "/data/attributes/name"
            },
            "meta": {

which was a suprise to me because I have setup the auto-pattern for the name field.

Instead of manually entering a name on each Storage entity within a form, you can define a name pattern here for auto-generating a value for it. This pattern will be applied everytime a Storage entity is being saved. Tokens are allowed, e.g. [storage:string-representation]. Leave empty to not use a name pattern for entities of this Storage type. If a name pattern is being used, you may optionally hide the name field in the Manage form display settings.

I just noticed, that the help description is talks about a form, as an example how useful it might be to use a pattern.

How to reproduce

- Create a storage type bernd
- create a plain text field with name 'label'
- setup the autofill pattern for the storage engine name as [storage:field_label:value]
- hide the name field as suggested
- create a storage entity bernd via Drupal UI and notice, that just filling the Label will successfully create the storage entity.
- notice, if module devel is installed, that the name field is filled with the same name as the field_label
- fire up Postman or your favorite REST client.
- use the correct JSONAPI call to create a storage entity of typ 'bernd'.

POST body

{ 
    "data": {
        "type": "storage--bernd",
        "attributes": {
            "field_label": "Karlzz"
        }
    }
}

You'll get the error message shown above, that the "name: This value should not be null." cannot be null. Which - in my case - is unexpected, because I have setup an auto-pattern to fill it.

Feature request
Status

Active

Version

1.3

Component

Code

Created by

🇩🇪Germany ro-no-lo

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

Comments & Activities

  • Issue created by @ro-no-lo
  • Status changed to Postponed: needs info about 1 year ago
  • Status changed to Active about 1 year ago
  • 🇩🇪Germany mxh Offenburg

    The problem here is that you're POSTing data via JSON API, not via a form. The name pattern is currently only automatically applied when submitting via entity forms (or programmatically). When POSTing data via JSON API, the entity that is supposed to be saved will be validated before, and since you're not posting a name property, the validation will fail.

    So the missing part here is that the automatic name pattern is not being applied when trying to POST data via JSON API. I don't know whether that's possible. If it's possible, contributions are welcome.

    Until then, the only way is to apply the name pattern yourself when POSTing via JSON API.

Production build 0.71.5 2024