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

MappingJackson2MessageConverter fails with null pointer in Native mode #30975

Closed
MatejNedic opened this issue Aug 1, 2023 · 5 comments
Closed
Labels
for: external-project Needs a fix in external project status: invalid An issue that we don't feel is valid

Comments

@MatejNedic
Copy link

MatejNedic commented Aug 1, 2023

Hello,
Possible enhancement?
When using MappingJackson2MessageConverter to convert payload to Message type I have noticed that in native mode MappingJackson2MessageConverter is failing when calling:
public final Message<?> toMessage(Object payload, @Nullable MessageHeaders headers) {
Only way to fix this is to provide reflection hints for class which is sent in method as Object payload.
Is this normal behaviour?

Related to awspring/spring-cloud-aws#856 providing Native hints for Spring Cloud AWS SQS integration.

I can provide example if needed.

Edit: Since it uses reflection under the hood it kinda makes sense that given class needs to be enabled for reflection?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 1, 2023
@bclozel
Copy link
Member

bclozel commented Aug 1, 2023

Yes that is correct. If Spring has no way to know about reflection being performed on a concrete type (we can detect that for controller method arguments for example), the application or library must contribute the AOT hints itself.

I'm not sure I understand the full picture behind your PR but I guess it makes sense in this case. Also see https://docs.spring.io/spring-framework/reference/core/aot.html#aot.hints

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Aug 1, 2023
@bclozel bclozel added status: invalid An issue that we don't feel is valid for: external-project Needs a fix in external project and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Aug 1, 2023
@MatejNedic
Copy link
Author

MatejNedic commented Aug 2, 2023

Thanks for quick answer, much appreciate it! To give more context I am one of the maintainers for Spring Cloud AWS and I have started to tackle the native hint support. Regarding PR I still have Spring Cloud AWS SQS hints on my branch not pushed, that is why it is probably confusing, only have pushed config and core. I opened issue because Spring Cloud AWS SQS is developed on top of the Spring Messaging, (Template calls converter in order to convert from Message Spring object to AWS SQS message object, MappingJackson2MessageConverter is used to convert payload to String representation). I have not went deep into Spring Messaging since @tomazfernandes wrote our integration.

Could you please provide me where to look for we can detect that for controller method arguments for example), since I will also probably need this for other integrations inside of Spring Cloud AWS. 😅

Will open discussion is Spring Kafka, since they are using similar way of converting to see how they handled it, to not spam here. Thanks again!

@bclozel
Copy link
Member

bclozel commented Aug 2, 2023

I see, thanks.
If you're mostly using messaging templates with well-known types in your library, I think that the approach you're taking is the right one.

As for "automatic detection", Spring projects that define an annotation-backed programming model (like @Controller) provide infrastructure that scans beans and contribute the relevant metadata at build time. For example, Spring for GraphQL provides flexible method signatures for controllers; since the concrete types are present on types marked as beans, we can detect those and contribute the runtime hints at build time.

In your case, the metadata you're contributing seems static, mostly listing well-known types for the library. In this case, maybe contributing the GraalVM config files (like reflect-config.json, see examples here) is actually enough.

@sbrannen
Copy link
Member

sbrannen commented Aug 2, 2023

Could you please provide me where to look for we can detect that for controller method arguments for example), since I will also probably need this for other integrations inside of Spring Cloud AWS.

Spring MVC's @RequestMapping is annotated with @Reflective(ControllerMappingReflectiveProcessor.class), and it is the ControllerMappingReflectiveProcessor class that registers runtime hints for controller method arguments and return types.

(update: @bclozel and I posted at the same time. So see his comments as well)

@MatejNedic
Copy link
Author

MatejNedic commented Aug 2, 2023

Big Thanks for links and explanation it helped!

If you're mostly using messaging templates with well-known types in your library, I think that the approach you're taking is the right one.

SqsTemplate receives T payload when send method is used and end user of library will provide their own object/class.
Tbh I was looking how to avoid making end users put their classes in @RegisterReflectionForBinding, but after exploring more in detail I see this is way to go. Guess dynamic scanning is not possible without annotations and it does not make sense to introduce them since then you could just put @RegisterReflectionForBinding.

Spring MVC's @RequestMapping is annotated with @Reflective(ControllerMappingReflectiveProcessor.class), and it is the ControllerMappingReflectiveProcessor class that registers runtime hints for controller method arguments and return types.

I think this is exactly what we need since we have some custom annotation for SNS HTTP endpoint that work with @Controller such as @NotificationMessageMapping.

I don't want to bug anymore with this, thanks again on all the tips will check links more thoroughly and work something out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project Needs a fix in external project status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

4 participants