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

Infinite recursion when requesting application/vnc.api+json data [DATAREST-767]

Open
#1,141 6 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

Gabriel Bauman opened DATAREST-767 and commented

I have a test Spring Boot application with a single entity Contact and CrudRepository at /contact. There are no entity relationships.

When there are no entities in the database, the request:

curl 'http://localhost:8080/contacts' -H 'Accept: application/vnd.api+json'

Returns:

{
  "links" : [ {
    "rel" : "self",
    "href" : "http://localhost:8080/contacts"
  }, {
    "rel" : "profile",
    "href" : "http://localhost:8080/profile/contacts"
  } ],
  "content" : [ {
    "collectionValue" : true,
    "rel" : null,
    "relTargetType" : "sdr.Contact",
    "value" : [ ]
  } ]
}

But, when I add a record to the database, the same curl request is infinitely recursive:

{
  "links" : [ {
    "rel" : "self",
    "href" : "http://localhost:8080/contacts"
  }, {
    "rel" : "profile",
    "href" : "http://localhost:8080/profile/contacts"
  } ],
  "content" : [ {
    "id" : 1,
    "firstName" : "Test",
    "lastName" : "Person",
    "embeddeds" : {
      "content" : [ ]
    },
    "propertyAccessor" : {
      "bean" : {
        "id" : 1,
        "firstName" : "Test",
        "lastName" : "Person"
      }
    },
    "persistentEntity" : {
      "idProperty" : {
        "name" : "id",
        "rawType" : "java.lang.Long",
        "field" : {
          "name" : "id",
          "type" : "java.lang.Long",
          "modifiers" : 2,
          "annotations" : [ ],
          "declaredAnnotations" : [ ],
          "synthetic" : false,
          "declaringClass" : "sdr.Contact",
          "enumConstant" : false,
          "genericType" : "java.lang.Long",
          "annotatedType" : {
            "type" : "java.lang.Long",
            "annotations" : [ ],
            "declaredAnnotations" : [ ]
          },
          "accessible" : true
        },
        "association" : false,
        "owner" : {
          "idProperty" : {
            "name" : "id",
            "rawType" : "java.lang.Long",
            "field" : {
              "name" : "id",
              "type" : "java.lang.Long",
              "modifiers" : 2,
              "annotations" : [ ],
              "declaredAnnotations" : [ ],
              "synthetic" : false,
              "declaringClass" : "sdr.Contact",
              "enumConstant" : false,
              "genericType" : "java.lang.Long",
              "annotatedType" : {
                "type" : "java.lang.Long",
                "annotations" : [ ],
                "declaredAnnotations" : [ ]
              },
              "accessible" : true
            },
            "association" : false,
            "owner" : {
              "idProperty" : {
                "name" : "id",
                "rawType" : "java.lang.Long",
                "field" : {
                  "name" : "id",
                  "type" : "java.lang.Long",
                  "modifiers" : 2,
                  "annotations" : [ ],
                  "declaredAnnotations" : [ ],
                  "synthetic" : false,
                  "declaringClass" : "sdr.Contact",
                  "enumConstant" : false,
                  "genericType" : "java.lang.Long",
                  "annotatedType" : {
                    "type" : "java.lang.Long",
                    "annotations" : [ ],
                    "declaredAnnotations" : [ ]
                  },
                  "accessible" : true
                },
                "association" : false,
                "owner" : {
... CONTINUED RECURSION ...

The server stack trace looks like this:

DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: Infinite recursion (StackOverflowError) (through reference chain: org.springframework.data.jpa.mapping.JpaPersistentEntityImpl["idProperty"]->org.springframework.data.jpa.mapping.JpaPersistentPropertyImpl["owner"]->org.springframework.data.jpa.mapping.JpaPersistentEntityImpl["idProperty"]->org.springframework.data.jpa.mapping.JpaPersistentPropertyImpl["owner"]->... recursion

Affects: 2.4.2 (Gosling SR2), 2.4.4 (Gosling SR4)

Reference URL: http://stackoverflow.com/questions/35349908/why-does-requesting-jsonapi-metadata-from-a-spring-data-rest-repository-return-a

Attachments:

2 votes, 5 watchers

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.