jersey icon indicating copy to clipboard operation
jersey copied to clipboard

IllegalArgumentException thrown from ParamConverter#fromString is converted into a default value by SingleValueExtractor

Open bobtiernay-okta opened this issue 8 years ago • 2 comments

From ParamConverter#fromString

IllegalArgumentException - if the supplied string cannot be parsed or is null.

And from SingleValueExtractor#extract:

    /**
     * {@inheritDoc}
     * <p/>
     * This implementation extracts the value of the parameter applying the underlying
     * {@link ParamConverter param converter} to the first value found in the list of potential multiple
     * parameter values. Any other values in the multi-value list will be ignored.
     *
     * @param parameters map of parameters.
     * @return extracted single parameter value.
     */
    @Override
    public T extract(final MultivaluedMap<String, String> parameters) {
        final String value = parameters.getFirst(getName());
        try {
            return fromString((value == null && isDefaultValueRegistered()) ? getDefaultValueString() : value);
        } catch (final WebApplicationException | ProcessingException ex) {
            throw ex;
        } catch (final IllegalArgumentException ex) {
            return defaultValue();
        } catch (final Exception ex) {
            throw new ExtractorException(ex);
        }

I'm trying to understand if this is a bug or expected behavior. I was expecting that this would get mapped to a 400 by default, but instead it silently gets converted to the default value.

Either way, I think both sets of JavaDoc should be enhanced to clarify this case as it seems to violate the principle of least surprise.

bobtiernay-okta avatar Nov 16 '17 13:11 bobtiernay-okta

And you don't have a @DefaultValue annotation for the param, right? Assuming that's true, I agree that this is confusing. As it is...throwing an IllegalArgumentException has no affect, i.e., the incoming request passes right through to the resource class for normal processing

chrisolson443 avatar Dec 13 '17 00:12 chrisolson443

I can't recall anymore, but the code stands on it's own :)

bobtiernay-okta avatar Dec 13 '17 03:12 bobtiernay-okta