fastapi-jsonrpc icon indicating copy to clipboard operation
fastapi-jsonrpc copied to clipboard

Possible incorrect parsing of JSON RPC request: params considered required

Open mike-hart opened this issue 3 years ago • 0 comments

Hi,

With the following test code:

import fastapi_jsonrpc as jsonrpc

app = jsonrpc.API()
api = jsonrpc.Entrypoint("/")

@api.method()
def simple() -> None:
    return None

app.bind_entrypoint(api)

executed as bin/uvicorn example_service:app --port 6666 and accessed using curl, we get the following:

$ curl -sX POST -d '{"jsonrpc": "2.0", "id": 0, "method": "simple", "params":[]}' -H 'Content-Type: application/json' http://127.0.0.1:6666/ | jq
{
  "jsonrpc": "2.0",
  "result": null,
  "id": 0
}

But if we try calling without a params element (as described legal in the JSON RPC spec, emphasis mine) - https://www.jsonrpc.org/specification#request_object

params A Structured value that holds the parameter values to be used during the invocation of the method. This member MAY be omitted.

We get the following:

curl -sX POST -d '{"jsonrpc": "2.0", "id": 0, "method": "simple"}' -H 'Content-Type: application/json' http://127.0.0.1:6666/ | jq
{
  "jsonrpc": "2.0",
  "error": {
    "code": -32600,
    "message": "Invalid Request",
    "data": {
      "errors": [
        {
          "loc": [
            "params"
          ],
          "msg": "field required",
          "type": "value_error.missing"
        }
      ]
    }
  },
  "id": 0
}

I believe this may be able to be fixed by changing https://github.com/smagafurov/fastapi-jsonrpc/blob/master/fastapi_jsonrpc/init.py#L368 from:

    params: dict

to

    params: dict = Field(default_factory=dict)

But I'm not 100% sure what follow-on effects this may have (if any).

mike-hart avatar Feb 16 '22 15:02 mike-hart