Uploaded image for project: 'RESTEasy'
  1. RESTEasy
  2. RESTEASY-3365

Ensure a ParamConverter is used on the client side for proxies

XMLWordPrintable

      From the PR:

      At the moment the resteasy-client doesn't retrieve ParamConverters for objects different from Collection and array if the ParamConverterProvider requires the genericType or the annotations. This PR fixes the issue.
      I haven't found any tests for the retrieval of ParamConverters on client-side. If there are any could you just point me to them so that I can add a test case for this.

      This seems to be an issue with proxied clients.

      Provider
      @Provider
      public class UnmodifiedSinceParamConverterProvider implements ParamConverterProvider {
      
          @Override
          public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
              if (rawType.isAssignableFrom(OffsetDateTime.class) && Optional.ofNullable(annotations)
                      .stream()
                      .flatMap(Stream::of)
                      .anyMatch(this::match)) {
                  //noinspection unchecked
                  return (ParamConverter<T>) new UnmodifiedSinceParamConverter();
              }
              return null;
          }
      
          private boolean match(Annotation annotation) {
              return annotation instanceof HeaderParam && "If-Unmodified-Since".equals(((HeaderParam) annotation).value());
          }
      
          private static class UnmodifiedSinceParamConverter implements ParamConverter<OffsetDateTime> {
      
              @Override
              public OffsetDateTime fromString(String value) {
                  return value == null ? null : OffsetDateTime.parse(value, DateTimeFormatter.RFC_1123_DATE_TIME);
              }
      
              @Override
              public String toString(OffsetDateTime value) {
                  return value == null ? null : value.format(DateTimeFormatter.RFC_1123_DATE_TIME);
              }
          }
      }
      
      TestClient
      @Path("/test")
      public interface TestClient {
      
          @POST
          String update(@HeaderParam("If-Unmodified-Since") OffsetDateTime ifUnmodifiedSince);
      }
      
      Example Usage
      final TestClient client = ((ResteasyWebTarget) ClientBuilder.newBuilder()
              .register(UnmodifiedSinceParamConverterProvider.class)
              .build()
              .target("http://localhost:8080/contacts/api/")).proxy(TestClient.class);
      System.out.println(client.update(OffsetDateTime.now()));
      

              jperkins-rhn James Perkins
              jperkins-rhn James Perkins
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: