Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Generate JSON Schema for NEAR RPC API #12939

Open
1 task done
frol opened this issue Feb 16, 2025 · 1 comment
Open
1 task done

Feature: Generate JSON Schema for NEAR RPC API #12939

frol opened this issue Feb 16, 2025 · 1 comment
Assignees
Labels
community Issues created by community

Comments

@frol
Copy link
Collaborator

frol commented Feb 16, 2025

Feature Request

To improve the consistency of NEAR RPC clients, we need to define NEAR RPC Schema.

Ideally, I would generate the schema from the source code to avoid drifts between the schema and the implementation.

There are a number of issues with the current state of things:

  • two Rust clients are currently maintained: internal in nearcore for testing, and public one
  • too many dependencies for the public Rust crate due to transitive dependencies of nearcore crates, that are not necessarily used in the JSON RPC client contexts
  • lack of strict types for non-Rust clients (there are deeply nested types, e.g. Block, Chunks, RPC errors, and Transaction errors)
  • impossible to verify API compatibility and catch the potential drifts at the PR to nearcore stage - with autogenerated schema we will be able to write a simple insta-snapshot test on it and make sure that any change there is expected

Implementation Ideas

There is a nice Rust crate that should help us to achieve it: https://graham.cool/schemars/. It was previously discussed back in 2020 in #1972, but since that time Rust crates have evolved quite a bit.

Here is how I see it implemented:

  1. All RPC-involved structs should get JsonSchema derive or manual trait implementation (if automatic derive does not play well)
  2. near-jsonrpc implementation should aggregate all the JsonSchema results for each exposed method into OpenAPI spec like paperclip or similar crates do. Unfortunately, OpenRPC is has not evolved enough to compete with OpenAPI ecosystem, so I recommend we try to fit OpenAPI our needs by defining the method names as independent POST paths (and I would suggest the RPC implementation ignore the path and let any POST /* requests to be handled by JSON-RPC handler); see how it is already done here: Generate Open RPC specification nervosnetwork/ckb#4149 (comment)
  3. near-jsonrpc server should have a method to expose GET /openapi-spec.json endpoint with the derived schema

With that, we can use the generated schema at nearcore compilation to generate internal client on the fly as well as external clients in a many programming languages. There are quite a few codegen solutions for OpenAPI: 1, 2, ... there are more dedicated and better or worse ones to choose from.

Beneficiary

Priority

Let's discuss it here first. This can be sponsored and contributed through NEAR DevHub.

Update: there is also a discussion in Telegram group: https://t.me/NEAR_Tools_Community_Group/4428

Contributing

  • Yes, I am interested
@frol
Copy link
Collaborator Author

frol commented Feb 18, 2025

For visibility: @PolyProgrammist will work on a proof-of-concept to see if we have any major blockers for applying schemars::JsonSchema to the relevant structs to enable automatic introspection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community Issues created by community
Projects
None yet
Development

No branches or pull requests

2 participants