Skip to content
This repository has been archived by the owner on May 28, 2018. It is now read-only.

Commit

Permalink
Issue JERSEY-1220 fixed.
Browse files Browse the repository at this point in the history
Improved string to object conversion for request parameters.

Change-Id: I5fbff9e5f0fe397fdaf69ecab82b04ac94995eef
  • Loading branch information
mmatula committed Jun 18, 2012
1 parent c22c705 commit 7652780
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ public String getDefaultValueString() {
}

protected final T fromString(String value) {
return valueReader.fromString(value);
T result = valueReader.fromString(value);
if (result == null) {
return defaultValue();
}
return result;
}

protected final boolean isDefaultValueRegistered() {
Expand All @@ -99,4 +103,4 @@ protected final boolean isDefaultValueRegistered() {
protected final T defaultValue() {
return (defaultValueString == null) ? null : valueReader.fromString(defaultValueString);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Collection<T> extract(MultivaluedMap<String, String> parameters) {
final Collection<T> valueList = getInstance();
if (stringList != null) {
for (String v : stringList) {
valueList.add((v.length() == 0) ? null : fromString(v));
valueList.add(fromString(v));
}
} else if (isDefaultValueRegistered()) {
valueList.add(defaultValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
/**
* Extract parameter value as a specific {@code String} Java collection type.
* <p />
* This class can be seen as a special, optimized, case of {@link CollectionStringReaderExtractor}.
* This class can be seen as a special, optimized, case of {@link CollectionExtractor}.
*
* @author Paul Sandoz
* @author Marek Potociar (marek.potociar at oracle.com)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public T fromString(String value) {
try {
return _fromString(value);
} catch (InvocationTargetException ex) {
// if the value is an empty string, return null
if (value.length() == 0) {
return null;
}
Throwable target = ex.getTargetException();
if (target instanceof WebApplicationException) {
throw (WebApplicationException) target;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

package org.glassfish.jersey.server.internal.inject;

import java.util.List;
import java.util.concurrent.ExecutionException;

import javax.ws.rs.GET;
Expand All @@ -66,6 +67,11 @@ private Parameter(CharSequence cs) {
public static Parameter fromString(String s) {
return new Parameter(s + "fromString");
}

@Override
public String toString() {
return s;
}
}

@Path("/")
Expand All @@ -76,6 +82,22 @@ public String doGet(
assertEquals("3.145fromString", p.s);
return "content";
}

@GET
@Path("list")
public String doGet(@QueryParam("arg") List<Parameter> p) {
System.err.println(p.toString());
assertEquals("[afromString, fromString, bfromString]", p.toString());
return "content";
}

@GET
@Path("empty")
public String doGetEmpty(
@QueryParam("arg1") Parameter p) {
assertEquals("fromString", p.s);
return "content";
}
}

@Test
Expand All @@ -84,4 +106,18 @@ public void testFromStringGet() throws ExecutionException, InterruptedException

_test("/?arg1=3.145");
}

@Test
public void testListFromStringGet() throws ExecutionException, InterruptedException {
initiateWebApplication(ResourceString.class);

_test("/list/?arg=a&arg=&arg=b");
}

@Test
public void testFromEmptyStringGet() throws ExecutionException, InterruptedException {
initiateWebApplication(ResourceString.class);

_test("/empty/?arg1=");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ public String doGetString(@QueryParam("args") List<BigDecimal> args) {
}
}

@Path("/")
public static class ResourceStringNull {
@GET
public String doGet(
@QueryParam("arg1") BigDecimal arg1,
@QueryParam("arg2") BigInteger arg2) {
assertEquals(null, arg1);
assertEquals(null, arg2);
return "content";
}
}

@Path("/")
public static class ResourceStringNullDefault {
@GET
Expand Down Expand Up @@ -179,6 +191,13 @@ public void testStringConstructorListEmptyGet() throws ExecutionException, Inter
_test("/?args&args&args", "application/stringlist");
}

@Test
public void testStringConstructorNullGet() throws ExecutionException, InterruptedException {
initiateWebApplication(ResourceStringNull.class);

_test("/?arg1=&arg2=");
}

@Test
public void testStringConstructorNullDefault() throws ExecutionException, InterruptedException {
initiateWebApplication(ResourceStringNullDefault.class);
Expand Down

0 comments on commit 7652780

Please sign in to comment.