spring-projects / spring-projects/spring-data-rest

Should object references be made in a separate call [DATAREST-282]

Open
#662 0 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

type: bug
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

Aaron Loes opened DATAREST-282 and commented

When working on validation logic for our entities, the following thought occurred (and potential future problem). In the situation where three calls to the API are made:

  1. create entity A
  2. create entity B
  3. associate entity B to entity A

Validation is going to fire twice against entity A (onCreate & onSave [<- should be onUpdate]).

Given this situation, i can see the following validation rules not being easy to implement (and possibly not possible). If an entity has a collection of child entities ( [A] 1--* [B] ) and it is required that there be at least a minimum value of child entities, then the creation of entity A would fail validation as the association of B entities to A comes after entity A's creation. Likewise if an existing A entity is being modified and its associations are being fully replaced.

More simply, the same goes for if an entity relationship is not allowed to be null. The creation of the first entity will fail because the association call has yet to be made (and may never be made).

There really could be any number of validation logic that can be very troublesome to implement given this association approach.

This approach can leave data in a poisoned state. There are really only three ways I can see around this. The first being some sort of transaction the API caller could instantiate prior to making any calls and changes are only committed upon a commit call. This doesn't really seam feasible due to the nature of spring-data-rest not being tied to any particular database. The second would be to have no relations at all. Again, not really helpful. The last option would be to not have a separate association call but have associations made on the call to create/edit the owning entity.

Pseudo Example:

{
  "name" : "Old MacDonalds Farm",
  "_associations" : {
    "farmer" : "http://localhost:8080/farmers/2k3l42lj43",
    "animals" : [
      "http://localhost:8080/horses/s465df43df4s65fs",
      "http://localhost:8080/cows/a3h1g35yuf3hf5h"
      "http://localhost:8080/pigs/335sd13fs5435efgd"
      "http://localhost:8080/pigs/1as35r43253f1sd"
    ]
  }
}

Affects: 2.0 RC1 (Codd)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.