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

When an association resource is not exported (e.g. @RestResource(exported = false)), custom links (RepresentationModelProcessor) are not handled properly. [DATAREST-1488]

Open
#1,847 2 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

jvanheesch opened DATAREST-1488 and commented

This issue was discussed in https://gitter.im/spring-projects/spring-data.

Please see reference url for quick reproducer.

I defined two aggregate roots: Car and Engine.
Car has an engine.
I then chose not to export this association resource (@RestResource(exported = false)).
This modifies car's representation (in both collection resource and item resource): it removes the engine link and inlines engine as a regular property.
However, in the resulting json, engine has a _links property, which is invalid HAL.
Example: GET to http://localhost:8080/cars/2 returns

{
  "name": "carName",
  "engine": {
    "name": "engineName",
    "_links": {
      "engineLink": {
        "href": "http://www.google.com"
      }
    }
  },
  "_links": {
    "self": {
      "href": "http://localhost:8080/cars/2"
    },
    "car": {
      "href": "http://localhost:8080/cars/2"
    }
  }
}

EDIT: I just realised the same holds for _embedded: both _embedded and _links of an association resource are rendered when the association resource is not exported.
I created a new branch EnginePart.
I created an Entity EnginePart, which has an excerptProjection that renders its name.
Engine now has a @OneToOne EnginePart.

GET http://localhost:8080/cars/3 now returns the following json:

{
  "name": "carName",
  "engine": {
    "name": "engineName",
    "_embedded": {
      "enginePart": {
        "name": "enginePartName",
        "_links": {
          "self": {
            "href": "http://localhost:8080/engineParts/1{?projection}",
            "templated": true
          }
        }
      }
    },
    "_links": {
      "engineLink": {
        "href": "http://www.google.com"
      },
      "enginePart": {
        "href": "http://localhost:8080/engineParts/1{?projection}",
        "templated": true
      }
    }
  },
  "_links": {
    "self": {
      "href": "http://localhost:8080/cars/3"
    },
    "car": {
      "href": "http://localhost:8080/cars/3"
    }
  }
}

I believe both _links and _embedded should only be present as "top level properties" in valid HAL.

TL;DR: when inlining an association resource, only its content should be included, not its _links and _embedded.


Reference URL: https://github.com/jvanheesch/spring-data-rest-inlined-resource-custom-links

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.