rest icon indicating copy to clipboard operation
rest copied to clipboard

Support java.util.Optional as parameter type in resource methods

Open glassfishrobot opened this issue 11 years ago • 14 comments

Jersey fills in missing optional resource method arguments with 'null'. Dealing with "possibly null" variables can be a bit of an annoyance, so more than often I do

Optional<String> optArg1 = Optional.ofNullable(arg1); Optional<String> optArg2 = Optional.ofNullable(arg2); ...

at the top of my resource methods. This opens up for use of many convenience methods such as map, orElse, orElseThrow, ifPresent etc.

So, request for enhancement: Let Jersey handle java.util.Optional as parameter type in resource methods directly.

glassfishrobot avatar Nov 24 '14 15:11 glassfishrobot

  • Issue Imported From: https://github.com/jax-rs/api/issues/503
  • Original Issue Raised By:@glassfishrobot
  • Original Issue Assigned To: @glassfishrobot

glassfishrobot avatar Feb 10 '18 22:02 glassfishrobot

@glassfishrobot Commented Reported by aioobe_

glassfishrobot avatar Nov 24 '14 15:11 glassfishrobot

@glassfishrobot Commented This issue was imported from java.net JIRA JAX_RS_SPEC-498

glassfishrobot avatar Apr 25 '17 08:04 glassfishrobot

The FormParam, QueryParam, etc. when the parameter instance is nullable, should accept Optional<xxx> as a parameter type.

hantsy avatar Dec 25 '24 04:12 hantsy

@jakartaee/ee4j-rest-committers Shall we adopt this in 5.0? WDYT?

mkarg avatar Dec 25 '24 09:12 mkarg

Better support for Optional<?> is definitely desirable.

spericas avatar Jan 02 '25 14:01 spericas

Supporting Optional makes sense to me as well.

jamezp avatar Jan 02 '25 15:01 jamezp

Jersey should support Optional. @hantsy if you have any use case that does not work, please file a bug on Jersey

jansupol avatar Jan 02 '25 17:01 jansupol

@hantsy Feeling in the mood for a PR changing the Spec, API and TCK?

mkarg avatar Jan 02 '25 18:01 mkarg

FWIW RESTEasy supports it for annotations like @QueryParam, @FormParam, @HeaderParam, @MatixParam and @CookieParam.

jamezp avatar Jan 02 '25 21:01 jamezp

Same for Quarkus REST.

I suggest also supporting it for method return types.

FroMage avatar Jan 06 '25 15:01 FroMage

Same for Quarkus REST.

I suggest also supporting it for method return types.

As a method can return any type already, there could exist third party MBW for Optionals already. These will break, if we now replace them with null. How to solve this?

mkarg avatar Jan 06 '25 16:01 mkarg

The same is true of parameters via ParamConverter. They can be provided by the system with a lower priority than user providers, but it's fairly likely that either version will do the exact same thing.

FroMage avatar Jan 07 '25 11:01 FroMage

Your are correct, we could deal with this using priorities.

mkarg avatar Jan 07 '25 19:01 mkarg