Entity validatios sometimes overwrite values and entity is not saved correctly

Created on 21 January 2025, about 1 month ago

Problem/Motivation

When creating or updating an entity, sometime validators overwrite field values and saving leads to an error.

Steps to reproduce

You can use "data_field" module to reproduce the problem.
Create a new content type with a DataField field type and inside this field a entity reference sub-field to a taxonomy.

When you create a new instance of this entity via JSON:API, you need to submit the tid for the sub-field and you can check that on database table is saved the number 1 (always saves 1 if tid passed is correct), instead of tid number.

This is beacause validator of sibfield loads the taxonomy instance and override the subfield (which has the tid) with the taxonomy object.

Proposed resolution

A proposed solution is cloning the entity to create/update before validating:

public function createIndividual(ResourceType $resource_type, Request $request) {
    ...
    static::validate(clone $parsed_entity);
   ...
}

public function patchIndividual(ResourceType $resource_type, EntityInterface $entity, Request $request) {
    ...
    static::validate(clone $entity, $field_names);
    ...
}
🐛 Bug report
Status

Active

Version

10.5

Component

jsonapi.module

Created by

🇪🇸Spain mrjey Sevilla

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

Comments & Activities

Production build 0.71.5 2024