jersey
  1. jersey
  2. JERSEY-1563

MessageBodyWriter got incorrect genericType argument

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Blocker Blocker
    • Resolution: Works as designed
    • Affects Version/s: 2.0-m09
    • Fix Version/s: 2.0-rc1
    • Component/s: core
    • Labels:
      None

      Description

      Spec. Section 3.3.3 defines conversion between resource method returned entity and aguments of MessageBodyWriter#isWritable
      With the example of

      Return Type Returned Instance Raw Type Generic Type
      GenericEntity GenericEntity<List<String>> ArrayList<?> List<String>

      We can create a test

      @Path("resource")
      public class Resource {
      	@Path("genericentity/linkedlist")
      	@GET
      	public GenericEntity<LinkedList<String>> checkGeneric() {
      		LinkedList<String> list = new LinkedList<String>();
      		list.add("linked");
      		list.add("list");
      		GenericEntity<LinkedList<String>> gells;
      		gells = new GenericEntity<LinkedList<String>>(list, List.class);
      		return gells;
      	}
      }
      @Provider
      public class CollectionWriter implements
      		MessageBodyWriter<Collection<?>> {
      	@Override
      	public boolean isWriteable(Class<?> type, Type genericType,
      			Annotation[] annotations, MediaType mediaType) {
                  //type is correctly class java.util.LinkedList
                  //genericType is javax.ws.rs.core.GenericEntity<java.util.LinkedList<java.lang.String>> which is not what the spec defines.
              }
      }
      

        Issue Links

          Activity

          Hide
          Miroslav Fuksa added a comment - - edited

          Required functionality works as desired. There is a bug in the test. There are few problems:

          • Generic entity is consturcted in a bad way. Should be
            bells = new GenericEntity<LinkedList<String>>(list) {};

            ).

          This just setup generic type as a java.util.List which is not a generic type.

          • Asseting in isWriteable is incorrect. Actually this method can be called many time with different types (String, Integer, ...) and it does not mean something should be wrong in jersey. Method just need to decide whether it can write given type or not. Assertion should be done in writeTo(). The listed javax.ws.rs.core.GenericEntity<java.util.LinkedList<java.lang.String>> is called during start of the application when media type is trying to be determined.

          Anyway, another bug was found. For such a wrongly constructed entity GenericEntity<List> was passed to writeTo. This is now fixed and method will receive method return generic type, which is LinkedList<String>. Using method return type is a jersey fallback mechanism for cases when generic type is not actually a generic type of the entity raw type.

          Show
          Miroslav Fuksa added a comment - - edited Required functionality works as desired. There is a bug in the test. There are few problems: Generic entity is consturcted in a bad way. Should be bells = new GenericEntity<LinkedList< String >>(list) {}; ). This just setup generic type as a java.util.List which is not a generic type. Asseting in isWriteable is incorrect. Actually this method can be called many time with different types (String, Integer, ...) and it does not mean something should be wrong in jersey. Method just need to decide whether it can write given type or not. Assertion should be done in writeTo(). The listed javax.ws.rs.core.GenericEntity<java.util.LinkedList<java.lang.String>> is called during start of the application when media type is trying to be determined. Anyway, another bug was found. For such a wrongly constructed entity GenericEntity<List> was passed to writeTo. This is now fixed and method will receive method return generic type, which is LinkedList<String>. Using method return type is a jersey fallback mechanism for cases when generic type is not actually a generic type of the entity raw type.
          Hide
          Miroslav Fuksa added a comment -

          works as designed, there is a bug in the test. Anyway, antoher bug was found and fixed. See commments.

          Show
          Miroslav Fuksa added a comment - works as designed, there is a bug in the test. Anyway, antoher bug was found and fixed. See commments.

            People

            • Assignee:
              Miroslav Fuksa
              Reporter:
              jan.supol
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - 3 hours Original Estimate - 3 hours
                3h
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 6 hours
                6h