spring-projects / spring-projects/spring-data-commons

Custom implementation of CrudRepository.save() not called if generics not declared exactly (reopener) [DATACMNS-1225]

Open
#1,664 1 comment 0 reactions 1 assignee View on GitHub

@mp911de is already working on this.

Since Dec 30, 2020.

in: repository type: bug
Dominant language
Java
Stars
838
Forks
730
PR merge metrics
No merged PRs in 30d

Description

Sebastian J opened DATACMNS-1225 and commented

This is a reopener to DATACMNS-1008 (unsure if the old one should be reopened or a new ticket should be created).

The issue is still the same and can be reproduced with these classes:

public interface FooRepository extends CrudRepository<Foo, Long>, FooRepositoryCustom {
	// Workaround as per DATACMNS-1008 still works
	//<X extends Foo> X save(X foo);
}

public interface FooRepositoryCustom {
	Foo doNonstandardThing(Foo foo);
}

public class FooRepositoryImpl implements FooRepositoryCustom {
	
    public Foo save(Foo foo) {
        // Do things
    	return foo;
    }
}

@Service
public class FooService {
    private final FooRepository fooRepo;
    
    @Autowired
    public FooService(FooRepository fooRepo) {
        this.fooRepo = fooRepo;
    }

    public void processFoo(Foo foo) {
    	fooRepo.doNonstandardThing(foo);
        fooRepo.save(foo);
    }
}

As previously, the save method in the custom implementation is never called.

From debugging it looks to me that the error occurs here: org.springframework.data.repository.core.support.RepositoryComposition.RepositoryFragments.invoke(Method, Object[]) as ReflectionUtils.findMethod in org.springframework.data.repository.core.support.RepositoryFragment.hasMethod(Method) will never find the custom method.
It is looking for a method save(Object) method (due to the generic erasure) but the custom method's parameter is of type Foo


Affects: 2.0.1 (Kay SR1), 2.0.2 (Kay SR2)

Reference URL: https://github.com/derjust/spring-data-dynamodb/tree/issue_77/src/test/java/org/socialsignin/spring/data/dynamodb/issue77

Issue Links:

  • DATACMNS-1008 Custom implementation of CrudRepository.save(…) not called if generics not declared exactly

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.