jnan77 / jsonrpc4j

Automatically exported from code.google.com/p/jsonrpc4j
0 stars 0 forks source link

NullPointerException when accessing service with one method #4

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

---
1. Create and expose (via JsonServiceExporter as in the example code) a service 
with a single method which accepts multiple named parameters.
2. Attempt to call the method.  (Sample JS client code attached.)
3. ???
4. Distinct lack of profit.

What is the expected output? What do you see instead?

---

Expected: the underlying service method should be called, etc.

Actual:
 SEVERE: Servlet.service() for servlet jsonrpc threw exception
java.lang.NullPointerException
    at org.codehaus.jackson.node.TreeTraversingParser.<init>(TreeTraversingParser.java:72)
    at org.codehaus.jackson.map.ObjectMapper.treeAsTokens(ObjectMapper.java:1247)
    at org.codehaus.jackson.map.ObjectMapper.treeToValue(ObjectMapper.java:1263)
    at com.googlecode.jsonrpc4j.JsonRpcServer.invoke(JsonRpcServer.java:397)
    at com.googlecode.jsonrpc4j.JsonRpcServer.handleObject(JsonRpcServer.java:335)
    at com.googlecode.jsonrpc4j.JsonRpcServer.handleNode(JsonRpcServer.java:186)
    at com.googlecode.jsonrpc4j.JsonRpcServer.handle(JsonRpcServer.java:140)
    at com.googlecode.jsonrpc4j.spring.JsonServiceExporter.handleRequest(JsonServiceExporter.java:72)
    at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:774)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
[... rest of trace removed ...]

What version of the product are you using? On what operating system?

---
- jsonrpc4j 0.11
- Spring 3.0
- Java 6
- OS independent

Please provide any additional information below.

---

Sample client code:

--- snip ---

var url = "http://localhost:8080/TEST/jsonrpc/suggest.json";

var request = {};
request.method = "getSuggestions";
request.params = {};
request.params.maxResults = 10;
request.params.query = "sample";
request.id = 1;
request.jsonrpc = "2.0";

function displaySearchResult(response) {

        if(response.result) {
            alert(response.result);
        }
        else if(response.error) {
            alert("Search error: " + response.error.message);
        }
};

$.post(url, JSON.stringify(request), displaySearchResult, "json");
--- snip ---

The bug appears to be in this section of JsonRpcServer (starting at line 274):

--- snip ---
        if (paramCount==0 || params.isArray() || (methods.size()==1)) {
            method = methods.iterator().next();
            for (int i=0; i<paramCount; i++) {
                paramNodes.add(params.get(i));
            }

        // handle named params
        }
--- snip ---

The assumption that services with only one method will not use named parameters 
is unsafe, resulting in paramNodes being accessed as though it were a list.  
The underlying implementation of JsonNode#get(int) will always return null, 
hence the bug.

Original issue reported on code.google.com by rob...@unc.edu on 4 Apr 2011 at 7:08

GoogleCodeExporter commented 8 years ago
Attached is a patch against the latest SVN revision which I believe fixes the 
issue.

Signed-off-by: Robert Tomsick <robert@unc.edu>

Original comment by rob...@unc.edu on 4 Apr 2011 at 7:58

GoogleCodeExporter commented 8 years ago
I've applied your proposed patch - thanks for the submission and good find.  
Please test the current development version (0.13-SNAPSHOT) and if it's good to 
go I'll cut a new release. I really need to create some unit tests for this 
project.

Original comment by brian.di...@gmail.com on 5 Apr 2011 at 7:31

GoogleCodeExporter commented 8 years ago

Original comment by brian.di...@gmail.com on 25 May 2012 at 2:51

GoogleCodeExporter commented 8 years ago

Original comment by brian.di...@gmail.com on 26 May 2012 at 2:43