Uploaded image for project: 'Nutch'
  1. Nutch
  2. NUTCH-2471

Returning a bare string meant to be application/json doesn't properly quote the string

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Blocker
    • Resolution: Auto Closed
    • 2.3.1
    • 2.5
    • nutch server
    • None
    • Patch Available
    • Patch

    Description

      Nutch Server resources are JAX-RS resources and that they proclaims to produce JSON even for a resource method that returns a String......

      @Produces({ MediaType.APPLICATION_JSON })
      public abstract class AbstractResource {
      .....
      }
      
      @Path("/config")
      public class ConfigResource extends AbstractResource {
      ...
      @GET
        @Path("/{configId}/{propertyId}")
        public String getProperty(@PathParam("configId") String configId,
            @PathParam("propertyId") String propertyId) {
          return configManager.getAsMap(configId).get(propertyId);
        }
      }
      

      the HTTP response indicates it is an application/json response, however the string returned is not properly quoted for JSON. We also get de-serialization errors on the client side.

      server: Restlet-Framework/2.2.3
      date: Wed, 06 Dec 2017 10:30:13 GMT
      content-type: application/json; charset=UTF-8
      content-length: 21
      accept-ranges: bytes
      
      There was an error parsing JSON data
      Unexpected token B in JSON at position 0
      

      The JAX-RS resources are configured to use JacksonJsonProvider for writing the JSON message body. JacksonJsonProvider.isWriteable() is indicating that it cannot write the value because _untouchables includes String.class. It appears String.class was put back into the _untouchables list as a result of a bug. That bug, however, appears to have been targeted at dealing with an XML issue, not a JSON issue.

      It should have been ideally taken care by the Jackson providers. However, it's not and hence proposing this fix.

      • Create our own custom NutchJsonProvider based on JacksonJsonProvider and remove the String.class from the _untouchables
      • Register this NutchJsonProvider with NutchServer to be used while writing JSON message body

      I have attached the patch along with this issue

      The other option is to JsonEscape the string and pad it with double quotes before returning. This has to be done at all places wherever string is returned as Json response and also is prone to errors in future if anybody misses this escaping and padding.

      There already is : issue created on the JAX-RS

      Attachments

        Activity

          People

            Unassigned Unassigned
            ninaadj@gmail.com Ninaad Joshi
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: