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

Projections do not support generics

Open
#2,003 3 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Sep 13, 2023.

status: feedback-provided status: waiting-for-triage
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

I don't know if it by design, but projections do not (properly?) support generics. For example, if I have:

public interface GenericCollectionSummary<E> {
	List<E> getMembers();
}

interface SpecialisedProjection extends GenericProjection<ValueProjection> {
}

Then the resulting JSON will not represent values as ValueProjection, but as the JSON format for its backing type.

So if we have:

class Person {
  String name;
  String surname;
  String getName(){}
  String getSurname(){}
}

class Club {
  List<Person> members;
  List<Person> getMembers(){}
}

and

@Projection(name = "summary", types = Person.class)
interface PersonSummary {
  @Value("#{target.name + ' ' + target.surname}")
  String getFullName();
}

@Projection(name = "summary", types = Club.class)
public interface ClubSummary extends GenericCollectionSummary<PersonSummary> {
}

So given a list of clubs using such projection such as { members: [{name: 'John', surname: 'Doe'}] }, you'll get the following projection:

{
  "values": [
    {
      "name": "John",
      "surname": "Doe"
    }
  ]
}

rathern than the expected:

{
  "values": [
    {
      "fullName": "John Doe"
    }
  ]
}

See attached demo for a runtime demonstration.

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.