Skip to content

Support Optional in PayloadMethodArgumentResolver #28945

Closed

Description

@artembilan

Sometime the data for payload method argument is optional and we cover that with a @Payload(required = false).
It would be nice if this PayloadMethodArgumentResolver would treat a @Nullable payload same way as that required = false.

Another improvement is around an Optional: the isEmptyPayload(@Nullable Object payload) should add a check for Optional.isEmpty().
Technically we cannot transfer a null in messaging at the moment, so the Optional.empty() is good compromise for us.
However this one probably should have an effect only if method param type is not an Optional, so no checks no conversions necessary. I assume this is an existing behavior.
What we need is just an automatic Optional resolution to null if @Nullable or @Payload(required = false).

Thanks

Activity

  1. added
    status: waiting-for-triage
    on Aug 9, 2022
  2. added
    in: messaging
    on Jan 24, 2023
  3. self-assigned this
    on Jan 24, 2023
  4. rstoyanchev commented on Jan 24, 2023

    @rstoyanchev
    Contributor

    I think you're suggesting to turn Optional into null if the method parameter is not Optional, and is not required. I think we could do that. Wouldn't we want the opposite too, i.e. wrap with Optional if the method parameter is Optional?

    As for @Nullable @Payload payload this has to be decided more broadly for such annotated handler methods, including web where we also don't support that currently.

  5. artembilan commented on Jan 24, 2023

    @artembilan
    MemberAuthor

    Hi @rstoyanchev !

    Right, I concur with all your arguments.
    Plus we'd like to have that check for Optional.isEmpty() in the isEmptyPayload(). In Spring Integration we are going to implement a Null payload feature: some users are requesting to be able to produce nulls from their POJO methods as valid result for a reply message.
    Yes, we can revise @Nullable payload in the separate issue.

  6. added this to the 6.0.5 milestone on Jan 30, 2023
  7. removed
    status: waiting-for-triage
    on Jan 30, 2023
  8. rstoyanchev commented on Jan 30, 2023

    @rstoyanchev
    Contributor

    I've scheduled it for the Optional support improvements but I'll discuss the @Nullable and provide and update or create another issue if necessary.

  9. changed the title [-]Improve org.springframework.messaging.handler.annotation.support.PayloadMethodArgumentResolver for better end-user experience[/-] [+]Improve `PayloadMethodArgumentResolver` for better end-user experience[/+] on Jan 30, 2023
  10. changed the title [-]Improve `PayloadMethodArgumentResolver` for better end-user experience[/-] [+]Support `Optional` in `PayloadMethodArgumentResolver`[/+] on Feb 7, 2023
  11. added
    type: enhancement
    and removed on Feb 7, 2023
  12. added a commit that references this issue on Feb 13, 2023
  13. added a commit that references this issue on Sep 17, 2026
    ccbb4bd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: messagingtype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions