spring-data-rest icon indicating copy to clipboard operation
spring-data-rest copied to clipboard

Projections do not support generics

Open alienisty opened this issue 3 years ago • 3 comments

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.

alienisty avatar Apr 22 '21 04:04 alienisty

If you would like us to spend some time helping you to diagnose the problem, please spend some time describing it and, ideally, providing a minimal sample that reproduces the problem. Please let us know in which context you're using projections (repositories, custom controller methods, …) so we can understand the use case.

mp911de avatar Apr 26 '21 14:04 mp911de

Sorry about that, I had to draft quickly bicause of time constraints and I didn't want to forget it. I've improved the description.

Also, here is an example demonstrating the issue demo.zip

alienisty avatar Apr 27 '21 06:04 alienisty

Any update on this one?

alienisty avatar Jan 05 '22 10:01 alienisty