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

Repository finders no longer support non-entity fields [DATAREST-173]

Open
#558 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

Willie Wheeler opened DATAREST-173 and commented

In previous SDR versions (at least 1.0.0.RELEASE), I was able to declare the following query on a repository:

@Query("select g, size(apps) from ApplicationGroup g left join g.applications apps group by g.id order by g.name, g.key")
@RestResource(rel = "findWithSizes", path = "find-with-sizes")
List<Object[]> findWithSizes();

This kind of query is useful for generating summary views (e.g., table of application groups with the associated number of applications, table of forums with the associated number of messages, etc.).

Note that Spring Data JPA supports the query.

With Spring Data REST 2.0.0.BUILD-SNAPSHOT, the query above generates a NullPointerException. I've traced it:

RepositorySearchController.executeQueryMethod() >
AbstractRepositoryRestController.resultToResources() >
AbstractRepositoryRestController.entitiesToResources() >
PersistentEntityResourceAssembler.toResource()

The problem is that resultToResources() assumes that just because the result is an Iterable, it must iterate over entities, which is not true in the example I offer above. Therefore when PersistentEntityResourceAssembler.toResource() goes to do the entity lookup by class, it fails, since the "entity" is an Object[].

Hopefully this is just an oversight and Spring Data REST will still support queries with Object[]-valued results and non-entity result elements. I think if there's an Iterable result set, what we want to do is check whether the individual result are entities. If so, great. If they are Object[]s, then map the entity elements in the Object[] to resources, but leave nonentities as-is (i.e. identity map).


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.