Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonbConfig().withSerializers() / withDeserializers() and lambda as parameter value cause exception. #624

Open
api-from-the-ion opened this issue Oct 12, 2023 · 0 comments
Labels
bug Something isn't working right

Comments

@api-from-the-ion
Copy link

api-from-the-ion commented Oct 12, 2023

Describe the bug
In case when parameter of the withSerializers() / withDeserializers() call is lambda [() → {}], we have an JsonbException because of ClassCastException in ReflectionUtils.findParameterizedType(ReflectionUtils.java:342).

The reason: lambdas have no runtime information - comparing to the (anonymous) classes. Therefore, Class.getGenericInterfaces() just get us the lambda's implemented interface itself, not the generic interfaces of the implemented class or interface. So we can't cast it and get an exception. And the given class itself has no generic information. So in case of lambdas, we have no chance to get the generic information through the reflection.

To Reproduce
I duplicated the existing two deserializes tests with lambdas instead of the anonymous classes. Of course, this tests asserts the exception now.

Expected behavior
There should be no difference between the deserializer implemented as class, anonymous class or lambda.

Additional context
Because of Java internals, which in case of lambda don't specify how to handle the runtime information of lambdas or specifying to having none, there would be no chance to get this information during the runtime.

I can think about one possible solution, which is already used in Gson (see https://www.javadoc.io/static/com.google.code.gson/gson/2.10.1/com.google.gson/com/google/gson/GsonBuilder.html#registerTypeAdapter(java.lang.reflect.Type,java.lang.Object) for example): to have an additional method with directly given type for a serializer / deserializer / adapter (the last isn't important, because adapter can't be implemented with lambda). This way, one don't have to go through reflection to get the generic interface of the parameter's implementation. Because it was already given as another parameter.

I understand, that this means a change in the upstream / implemented API. But I think that this is an issue, which another implementation should also see at this point.

Maybe there are also another solutions.

@api-from-the-ion api-from-the-ion added the bug Something isn't working right label Oct 12, 2023
api-from-the-ion added a commit to api-from-the-ion/yasson that referenced this issue Oct 12, 2023
Signed-off-by: Anton Pinsky <anton.pinsky@ionos.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working right
Projects
None yet
Development

No branches or pull requests

1 participant