WIP: Add support for generating rpc.discover method
#1608
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR implements optional generation of
rpc.discovermethod which returns OpenRPC specification from trait definition. For thatopen-rpccrate was forked to fix compatibility issues with official examples and to add some useful conversion functions (about that a little bit later).This works by using
utoipa::schema!andutoipa::ToSchemacrate macros, that generateutoipa::openapi::Schemastructure at runtime, which later converted toopen_rpc::Schemaand, with information from server trait definition, toopen_rpc::ContentDescriptor.With these information about each trait method,
open_rpc::OpenRpcstructure is constructed insiderpc.discovermethod at runtime. For now it's constructed on eachrpc.discovercall, but this can be improved by adding some lazy static variable.Functionality
For example, you can take a look at
examples/examples/rpc_discover.rs, run it:then send request to server:
Response
{ "openrpc": "1.0.0-rc1", "info": { "title": "Rpc", "version": "" }, "servers": [], "methods": [ { "name": "foo", "description": "", "params": [ { "name": "param_a", "required": true, "schema": { "type": "integer", "minimum": 0 } }, { "name": "param_b", "required": true, "schema": { "type": "string" } } ], "result": { "name": "return", "required": true, "schema": { "type": "integer", "minimum": 0 } } }, { "name": "add", "description": "", "params": [ { "name": "request", "required": true, "schema": { "type": "object", "properties": { "a": { "type": "integer", "minimum": 0 }, "b": { "type": "integer", "minimum": 0 } }, "required": [ "a", "b" ] } } ], "result": { "name": "return", "required": true, "schema": { "type": "object", "properties": { "sum": { "type": "integer", "minimum": 0 } }, "required": [ "sum" ] } } }, { "name": "calculate", "description": "", "params": [ { "name": "args", "required": true, "schema": { "type": "array", "items": { "type": "integer" } } }, { "name": "operation", "required": true, "schema": { "type": "string", "enum": [ "add", "mul", "sub" ] } } ], "result": { "name": "return", "required": true, "schema": { "type": "integer" } } } ] }copy response, and introspect it, for example, using OpenRPC Playground.