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

New ModuleCallJsonSchema macro to generate JSON Schemas via schemars #517

Merged
merged 11 commits into from
Jul 19, 2023

Conversation

neysofu
Copy link
Member

@neysofu neysofu commented Jul 18, 2023

Description

This PR defines a new trait ModuleCallJsonSchema, which is intended to be used by all modules that wish to expose a JSON Schema for their call messages. This trait can be either implemented manually or via the new derive macro.

As a proof of concept, the trait is derived for sov_bank::Bank. If this PR is approved, I'll instrument all other modules with the derive macros and macro attributes necessary for JSON Schema generation.

How #[derive(ModuleCallJsonSchema)] works

The derive for ModuleCallJsonSchema) is conceptually very simple, as it offloads the complexity of schema generation to the schemars crate. schemars itself exposes a derivable trait –JsonSchema– which must be derived by CallMessage and all its children types. Occasionally, we also need to pollute our types with schemars–sepcific macro attributes, specifically to help it generate the right type bounds when dealing with sov_modules_api::Context.

The quality of the generated schema(s) seems, at a first glances, very good. schemars supports most serde attribute macros and has crate features to support well-known crates, so most bases are covered. It's possible we might need to patch the schemas in the future. We'll have to decide how to do that when the need arises. Custom macro attributes or hand-written implementations of schemars::JsonSchema could be viable options.

Linked Issues

I don't think there are any? The team discussed this as part of the July roadmap, though.

Testing

There's a new "dummy" crate, sov-module-schemas, which generates JSON Schemas for all supported modules and stores them in a directory. These are tracked by git so it will be very easy to see if a code change results in a schema change. This way, breakages and changes to call interfaces can be tracked easily.

Also, I refactored the get_generics_type_param utility function to generate a better error message when deriving ModuleInfo. This is unrelated to the work on JSON Schemas, but it's small and I figured it made sense to include it in the same PR.

Docs

I added #![deny(missing_docs)] to sov_modules_macros and I patched some of the doc tests to be runnable instead of ignore'd by Cargo.

@neysofu neysofu added enhancement New feature or request sov-modules Design labels Jul 18, 2023
@neysofu neysofu marked this pull request as ready for review July 18, 2023 14:07
@neysofu neysofu changed the title [WIP] New ModuleCallJsonSchema macro to generate JSON Schemas via schemars New ModuleCallJsonSchema macro to generate JSON Schemas via schemars Jul 18, 2023
@codecov
Copy link

codecov bot commented Jul 18, 2023

Codecov Report

Merging #517 (427d49f) into nightly (a649a21) will increase coverage by 0.1%.
The diff coverage is 91.6%.

Impacted Files Coverage Δ
...-system/module-implementations/sov-bank/src/lib.rs 95.0% <0.0%> (-2.5%) ⬇️
module-system/sov-modules-api/src/lib.rs 53.8% <0.0%> (-1.1%) ⬇️
...system/module-implementations/sov-bank/src/call.rs 95.2% <50.0%> (-0.7%) ⬇️
...ystem/module-implementations/sov-bank/src/token.rs 98.5% <50.0%> (-0.8%) ⬇️
module-system/module-schemas/src/lib.rs 100.0% <100.0%> (ø)
module-system/sov-modules-macros/src/common.rs 79.1% <100.0%> (+0.8%) ⬆️
...m/sov-modules-macros/src/dispatch/dispatch_call.rs 100.0% <100.0%> (ø)
...-system/sov-modules-macros/src/dispatch/genesis.rs 100.0% <100.0%> (ø)
...m/sov-modules-macros/src/dispatch/message_codec.rs 100.0% <100.0%> (ø)
module-system/sov-modules-macros/src/lib.rs 98.2% <100.0%> (+0.1%) ⬆️
... and 2 more

... and 1 file with indirect coverage changes

@neysofu neysofu merged commit bf3fb8c into nightly Jul 19, 2023
10 checks passed
@neysofu neysofu deleted the filippo/json-schemas branch July 19, 2023 09:24
citizen-stig pushed a commit that referenced this pull request Jul 20, 2023
…rs` (#517)

* Introduce `ModuleCallJsonSchema` trait

* Refactor `get_generics_type_param`

* Add `ModuleInfo` test

* Allow `#[derive(ModuleCallJsonSchema)]`

* Impl `ModuleCallJsonSchema` for `Bank`

* New crate `sov-module-schemas`

* `#![deny(missing_docs)]` for `sov_modules_macros`

* Improve macro hygiene

* Clearer docs on module type generics

* ci: run coverage on bigger machine
preston-evans98 pushed a commit that referenced this pull request Sep 14, 2023
…rs` (#517)

* Introduce `ModuleCallJsonSchema` trait

* Refactor `get_generics_type_param`

* Add `ModuleInfo` test

* Allow `#[derive(ModuleCallJsonSchema)]`

* Impl `ModuleCallJsonSchema` for `Bank`

* New crate `sov-module-schemas`

* `#![deny(missing_docs)]` for `sov_modules_macros`

* Improve macro hygiene

* Clearer docs on module type generics

* ci: run coverage on bigger machine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants