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

Add ResponseEntity.ofNullable() to deal with non-Optional nullable objects #29117

Closed
davefranken-ah opened this issue Sep 9, 2022 · 2 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) theme: kotlin An issue related to Kotlin support type: enhancement A general enhancement
Milestone

Comments

@davefranken-ah
Copy link

davefranken-ah commented Sep 9, 2022

Affects: All versions


Context
I like that <T> ResponseEntity<T> ResponseEntity.of(Optional<T> body) gives a 200 OK with a body if it is present inside the optional and a 404 for an empty Optional.

Problem
However, sometimes you cannot or do not want to deal with Optional (for instance when using Kotlin) but still want to have a useful method like this.

Current workaround
The current workaround would be to just wrap it inside an Optional:
ResponseEntity.of(Optional.ofNullable(entity)) but this is pretty ugly and still creates an extra intermediate Optional object.

Proposal
My proposal would be to add a method:
<T> ResponseEntity<T> ResponseEntity.ofNullable(T body) which can be used by both Java and Kotlin to turn a nullable object into a 200 OK with body or 404.

The implementation would be pretty straightforward:

public static <T> ResponseEntity<T> ofNullable(T body) {
  if (body == null) {
    return notFound().build();
  }
  return ResponseEntity.ok(body);
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 9, 2022
@snicoll snicoll changed the title Enhancement: Add ResponseEntity.ofNullable() to deal with non-Optional nullable objects Add ResponseEntity.ofNullable() to deal with non-Optional nullable objects Sep 9, 2022
@sbrannen sbrannen added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement labels Sep 9, 2022
@rstoyanchev rstoyanchev added the theme: kotlin An issue related to Kotlin support label Sep 16, 2022
@sdeleuze
Copy link
Contributor

The use case seems valid to me, and I have don't have a better proposal for the name, we should just make sure to annotate correctly the parameter:

public static <T> ResponseEntity<T> ofNullable(@Nullable T body) {
  if (body == null) {
    return notFound().build();
  }
  return ResponseEntity.ok(body);
}

@poutsma @rstoyanchev Are you ok to introduce this variant?

@sdeleuze sdeleuze self-assigned this Sep 23, 2022
@poutsma
Copy link
Contributor

poutsma commented Sep 26, 2022

Sounds good to me.

@rstoyanchev rstoyanchev added this to the 6.0.5 milestone Jan 31, 2023
@rstoyanchev rstoyanchev removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 31, 2023
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Feb 2, 2023
To deal with non-Optional nullable objects.

Closes spring-projectsgh-29117
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Feb 2, 2023
To deal with non-Optional nullable objects.

Closes spring-projectsgh-29117
mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 2023
To deal with non-Optional nullable objects.

Closes spring-projectsgh-29117
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) theme: kotlin An issue related to Kotlin support type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

6 participants