open-rpc / inspector

🕵️‍♂️ OpenRPC Inspector is a simple tool to create, modify and execute JSON-RPC requests.
https://inspector.open-rpc.org/
Apache License 2.0
21 stars 8 forks source link

Resolving schema object with definitions field got error #254

Closed sanlee42 closed 3 years ago

sanlee42 commented 3 years ago

Describe the bug The schema object with definitions field would got a resolving error:

ono.js:64 Uncaught (in promise) SyntaxError: Error resolving $ref pointer "https://playground.open-rpc.org/#/definitions/MyType". 
Token "definitions" does not exist.

but "definitions" field is valid in json schema draft-07.

To Reproduce Steps to reproduce the behavior:

  1. Go to https://playground.open-rpc.org/
  2. Copy the schema into it.
    
    {
    "openrpc": "1.1.0",
    "info": {
    "title": "My JSON-RPC",
    "version": "0.1"
    },
    "methods": [
    {
      "name": "hello.method.call",
      "params": [
        {
          "name": "first_param",
          "schema": {
            "$schema": "http://json-schema.org/draft-07/schema#",
            "title": "MyParam",
            "type": "object",
            "required": [
              "my_bool",
              "my_int",
              "my_type"
            ],
            "properties": {
              "my_bool": {
                "type": "boolean"
              },
              "my_int": {
                "type": "integer",
                "format": "int32"
              },
              "my_type": {
                "$ref": "#/definitions/MyType"
              }
            },
            "definitions": {
              "MyType": {
                "type": "object",
                "required": [
                  "my_int"
                ],
                "properties": {
                  "my_int": {
                    "type": "integer",
                    "format": "int32"
                  }
                }
              }
            }
          }
        }
      ],
      "result": {
        "name": "ret",
        "schema": {
          "$schema": "http://json-schema.org/draft-07/schema#",
          "title": "MyRet",
          "type": "object",
          "required": [
            "success"
          ],
          "properties": {
            "success": {
              "type": "boolean"
            }
          }
        }
      }
    }
    ]
    }
shanejonas commented 3 years ago

"$ref": "#/definitions/MyType"

this is not relative, it uses the document as the root, which doesn't exist. (it would have to be something like #/methods/0/params/0/schema/definitions/MyType)

I'd recommend using the components section to separate out reusable snippets like schemas:

Also, you dont need $schema

{
  "openrpc": "1.1.0",
  "info": {
    "title": "My JSON-RPC",
    "version": "0.1"
  },
  "methods": [
    {
      "name": "hello.method.call",
      "params": [
        {
          "name": "first_param",
          "schema": {
            "title": "MyParam",
            "type": "object",
            "required": [
              "my_bool",
              "my_int",
              "my_type"
            ],
            "properties": {
              "my_bool": {
                "type": "boolean"
              },
              "my_int": {
                "type": "integer",
                "format": "int32"
              },
              "my_type": {
                "$ref": "#/components/schemas/MyType"
              }
            }
          }
        }
      ],
      "result": {
        "name": "ret",
        "schema": {
          "title": "MyRet",
          "type": "object",
          "required": [
            "success"
          ],
          "properties": {
            "success": {
              "type": "boolean"
            }
          }
        }
      }
    }
  ],
  "components": {
    "schemas": {
      "MyType": {
        "type": "object",
        "required": [
          "my_int"
        ],
        "properties": {
          "my_int": {
            "type": "integer",
            "format": "int32"
          }
        }
      }
    }
  }
}
sanlee42 commented 3 years ago

@shanejonas, Really appreciate your answer. I was working on generating the open rpc schema for my json rpc methods automatically. And the schema objects are generated by the json schema library. It seems that I can't use them directly, since I need to put them into the components section.

shanejonas commented 3 years ago

@fikgol if you are using golang there is this library to generate the openrpc document from your code: https://github.com/etclabscore/go-openrpc-reflect

Also the advanced section of the webinar is all about go-openrpc-reflect and generating documents from code: https://open-rpc.org/webinar/

sanlee42 commented 3 years ago

@shanejonas Thanks for the suggestion. I use rust, and I read the codes of go-openrpc-reflect, which uses inline subschemas instead of components section or definitions field to work around this issue. This may produce some duplicate codes, but it works.