Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

RepositoryRestResource GET request issues #2049

Closed
dmitry-s opened this issue Nov 19, 2019 · 1 comment · Fixed by #2080
Closed

RepositoryRestResource GET request issues #2049

dmitry-s opened this issue Nov 19, 2019 · 1 comment · Fixed by #2080

Comments

@dmitry-s
Copy link
Contributor

RepositoryRestResource GET requests are failing if the id type is not Key. For example this would fail:

@RepositoryRestResource(collectionResourceRel = "traders", path = "traders")
public interface TraderRepository extends SpannerRepository<Trader, String> {
}

Error:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [com.google.cloud.spanner.Key] to type [java.lang.String]
	at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321) ~[spring-core-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194) ~[spring-core-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174) ~[spring-core-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.data.repository.support.ReflectionRepositoryInvoker.convertId(ReflectionRepositoryInvoker.java:289) ~[spring-data-commons-2.2.0.RELEASE.jar:2.2.0.RELEASE]
	at org.springframework.data.repository.support.CrudRepositoryInvoker.invokeFindById(CrudRepositoryInvoker.java:92) ~[spring-data-commons-2.2.0.RELEASE.jar:2.2.0.RELEASE]
	at org.springframework.data.rest.core.support.UnwrappingRepositoryInvokerFactory$UnwrappingRepositoryInvoker.invokeFindById(UnwrappingRepositoryInvokerFactory.java:94) ~[spring-data-rest-core-3.2.0.RELEASE.jar:3.2.0.RELEASE]
	at org.springframework.data.rest.webmvc.RepositoryEntityController.getItemResource(RepositoryEntityController.java:518) ~[spring-data-rest-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
	at org.springframework.data.rest.webmvc.RepositoryEntityController.getItemResource(RepositoryEntityController.java:331) ~[spring-data-rest-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888) ~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) ~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE]

To make it work, use Key for id type.

@RepositoryRestResource(collectionResourceRel = "traders", path = "traders")
public interface TraderRepository extends SpannerRepository<Trader, Key> {
}
@dmitry-s
Copy link
Contributor Author

dmitry-s commented Nov 21, 2019

The problem is we use a custom BackendIdConverter to enable String to Key conversion. But the rest repository could have different ID type. We support 'virtual' keys in order to allow composite keys.
Because of that we allow users to create repositories with the following id types: Key, Object[],Iterable

Also we support regular id types, such as String.

BackendIdConverter doesn't know what was the ID type on the repository, it only has access to the entity.
After Spring gets id of type Key from BackendIdConverter it tries to convert it to the actual ID type of the repository (ReflectionRepositoryInvoker.java, Object convertId(Object id)). But it doesn't use the global conversion service so we can't provide a converter that can be used there.

@mp911de do you have any recommendations?

dmitry-s added a commit that referenced this issue Dec 16, 2019
* support non-Key ID types in rest repositories; fixes #2049
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging a pull request may close this issue.

2 participants