Skip to content

fix: ABI generation#146

Merged
mitinarseny merged 2 commits into
masterfrom
fix/abi
Oct 2, 2024
Merged

fix: ABI generation#146
mitinarseny merged 2 commits into
masterfrom
fix/abi

Conversation

@mitinarseny
Copy link
Copy Markdown
Collaborator

@mitinarseny mitinarseny commented Oct 2, 2024

This PR fixes an issue when ABI generation fails due to absence of #[derive(JsonSchema)]

cargo near build
# ...
• Generating ABI
 │ error[E0277]: the trait bound `PermissionedAccounts: JsonSchema` is not satisfied
 │     |
 │ 18  | #[access_control(role_type(Role))]| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 │     | || the trait `JsonSchema` is not implemented for `PermissionedAccounts`| required by a bound introduced by this call
 │     |
 │     = help: the following other types implement trait `JsonSchema`:
 │               &'a T
 │               &'a mut T
                ()
 │               (T0, T1)
 │               (T0, T1, T2)
 │               (T0, T1, T2, T3)
 │               (T0, T1, T2, T3, T4)
 │               (T0, T1, T2, T3, T4, T5)
 │             and 185 others
 │ note: required by a bound in `SchemaGenerator::subschema_for`
 │    --> /Users/mitinarseny/.cargo/registry/src/index.crates.io-6f17d22bba15001f/schemars-0.8.21/src/gen.rs:221:38
 │     |
 │ 221 |     pub fn subschema_for<T: ?Sized + JsonSchema>(&mut self) -> Schema {
 │     |                                      ^^^^^^^^^^ required by this bound in `SchemaGenerator::subschema_for`
 │     = note: this error originates in the attribute macro `access_control` (in Nightly builds, run with -Z macro-backtrace for more info)
 │ 
 │ For more information about this error, try `rustc --explain E0277`.
 │ error: could not compile `defuse-contract` (lib) due to 1 previous error

Basically, in order to properly handle ABI generation via cargo near, you're literally forced to use #[near(serializers = [json])], as its expanded version depends on near-sdk/abi feature, so you can think of expanded version as:

#[derive(::near_sdk::serde::Deserialize, ::near_sdk::serde::Serialize)]
#[serde(crate = "::near_sdk::serde")]
#[cfg_attr(all(feature = "near_sdk/abi", not(target_arch = "wasm32")), derive(::near_sdk::schemars::JsonSchema))]
#[cfg_attr(all(feature = "near_sdk/abi", not(target_arch = "wasm32")), schemars(crate = "::near_sdk::schemars"))]

@mitinarseny mitinarseny requested a review from karim-en October 2, 2024 08:16
@mitinarseny mitinarseny marked this pull request as ready for review October 2, 2024 08:16
@mitinarseny mitinarseny requested a review from aleksuss October 2, 2024 08:19
@aleksuss
Copy link
Copy Markdown
Collaborator

aleksuss commented Oct 2, 2024

Maybe other types should be reworked as well?

@mitinarseny
Copy link
Copy Markdown
Collaborator Author

Maybe other types should be reworked as well?

As I checked, these are the only types exposed in ABI. All other custom types in this crate are needed for events, which are not included in ABI

Copy link
Copy Markdown
Collaborator

@aleksuss aleksuss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mitinarseny mitinarseny merged commit e6e4b07 into master Oct 2, 2024
@mitinarseny mitinarseny deleted the fix/abi branch October 2, 2024 10:22
This was referenced Mar 27, 2026
This was referenced Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants