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

Add ability to link references to abstract super types of repository entities [DATAREST-202]

Open
#584 5 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

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

Description

Aaron Loes opened DATAREST-202 and commented

Given the following example: i have an object that represents a farm and has a corresponding repository. That farm object has a reference to a collection of animals but there is a repository for each animal instance (e.g. horse, cow, pig, etc.). With spring data rest and spring data mongo, i am able to save instances of specific animals to the animal collection of the farm and the proper references are made in the database

{
  "_id" : "blah blah blah",
  "_class" : "org.blah.test.Farm",
  "name" : "Old MacDonalds Farm E I E I O",
  "animals" : [
    {
      "$ref" : "horse",
      "$id" : "132432"
    },{
      "$ref" : "cow",
      "$id" : "587657354"
    },{
      "$ref" : "cow",
      "$id" : "24323523"
    },{
      "$ref" : "pig",
      "$id" : "3524234"
    }
  ]
}

However, when using Spring Data Rest api's, i make a call to get the farm and instead of there being a reference to the animals in the links, it inlines the animals directly. This is because the class "Animal" is not a mapped resource, but because it is an superclass of mapped resouces, I feel that the API should be able to resolve this and return a link that fetch the list of animals for my farm.

Currently there are only three ways around this. 1) i can create seperate collections in the farm for each type of animal. however this is not ideal because a farm could have a ton of animals that it would need to track. 2) i could potentially create an animal repository and save my animals to that, but I also believe this will cause other problems. 3) I could write my own PersistentEntityJackson2Module class, and as fun as this sounds, i feel this functionality should be part of this library. For now i will have to go with option 3


Affects: 2.0 M1 (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.