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.

Comments

ro-no-lo created an issue. See original summary.

mxh’s picture

Version: 1.3.1 » 1.3.x-dev
Category: Bug report » Support request
Status: Active » Postponed (maintainer needs more info)
ro-no-lo’s picture

Issue summary: View changes
mxh’s picture

Title: Pattern creation of 'name' seems not to work with JSONAPI » Add support to apply automatic name pattern when using POST via JSON API
Category: Support request » Feature request
Status: Postponed (maintainer needs more info) » Active

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 would need to be 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. Or you could try using an arbitrary placeholder for the name property, maybe it's being automatically replaced during the saving process.