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

_embedded relation attribute results are different each execution. [DATAREST-839]

Open
#1,211 0 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

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

Description

MyeongHyeonLee opened DATAREST-839 and commented

This bug is caused by complexity reasons.
I wrote test case what is problem.

This test case is so wired.

check out :
https://github.com/LeeMyeongHyeon/spring-data-rest-resourcemapping-bug/blob/master/src/test/java/com/example/sample/IntegrationTest.java

There are one parent(Animal) and two child(Dog, Cat) classes.

I want to expose 'animals' under '_embedded' json value ; not dogs, cats

like this :

"_embedded" : {
    "animals" : [ {
      "name" : "dog",
      "message" : "hi",
      "_links" : {
        "self" : {
          "href" : "/animals/com.example.sample.Animal$Id@2d07aacc"
        }
      }
    }, {
      "name" : "cat",
      "message" : "hi2",
      "_links" : {
        "self" : {
          "href" : "/animals/com.example.sample.Animal$Id@ff5d4f1"
        }
      }
    } ]
  }
}

I used @Relation on entities, implements @RestController manually and id is @EmbeddedId.

First call result is ok but, after calls are different.

second call :

"_embedded" : {
    "cats" : [ {
      "name" : "cat",
      "message" : "hi2",
      "_links" : {
        "self" : {
          "href" : "/animals/com.example.sample.Animal$Id@553fbe94"
        }
      }
    } ],
    "dogs" : [ {
      "name" : "dog",
      "message" : "hi",
      "_links" : {
        "self" : {
          "href" : "/animals/com.example.sample.Animal$Id@7cdc4070"
        }
      }
    } ]
  }
}
  • Reason
  1. I implements @RestController manually that sub types aren't registered cache.
    (PersistentEntitiesResourceMappings.hasMappingFor() is false)
  2. So RelProvider choose AnnotationRelProvider; second priority.
  3. Finally PersistentEntityJackson2Module call PersistentEntitiesResourceMappings.getMetadataFor() then put to cache these sub types.
    (Because of @EmbeddedId is Association)
  4. Second call's result are different. RepositoryRelProvidre find these types in cache that RepositoryRelProvider choosed

Affects: 2.4.4 (Gosling SR4)

Reference URL: https://github.com/myeonhyeonLee/spring-data-rest-resourcemapping-bug

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.