Unfetched property cause error for validation when creating or updating entity via Rest API

Hi,

We ran into the following issue when trying to create or update a specific entity through the generic Rest API.

The problem is simplified as follows.

We have an entity A and an entity B. Entity A has an attribute that is a reference to entity B. Entity B has a Boolean attribute. We have created a custom bean validation, which checks that only entity B can be connected to entity A for which this Boolean attribute is true. When creating or updating entity A via the Rest API, this validation fails because the Boolean attribute is not loaded. When creating through the UI, we can complete the fetchplan of entity A so that the Boolean attribute of entity B is also loaded when the detailview is opened. Can this be solved somehow with the Rest API as well?

I have attached a simple Jmix 2.x project in which the Rest API addon is available and the problem can be reproduced. It has two entities, ItemVariant and Item. The custom RuleItemVariant bean validation applied to the ItemVariant class verifies that only those ItemVariant instances can be saved to which the hasVariations attribute of the associated Item instance is true.

The following steps can cause the error.

  • create an Item instance via the UI

  • create an ItemVariant instance via API, whose Item attribute points to this Item instance

  • the API call returns with an error regardless of whether the hasVariations property of the Item instance is true or false, since it is not loaded before the validations run

RestAPIJmixTest.zip (112.1 KB)

Thanks,
Peter

Hi,

Unfetched attribute exception happens because association attributes reloaded with _intance_name fetch plan, which is enough to set the value. As a workaround you can add hasVariations to instance name. You don’t even need display it, the main point that getInstanceName method is annotated with @DependsOnProperties({"itemNumber", "hasVariations"}) thus, hasVariations will be loaded.

I’ve created a GitHub issue.

Regards,
Gleb

Thank you, Gleb.
We will check out this workaround.