near / cargo-near

Cargo extension for building Rust smart contracts on NEAR
Apache License 2.0
36 stars 17 forks source link

ABI not generated when functions return a vector of objects or regular JSON #136

Open corydickson opened 8 months ago

corydickson commented 8 months ago

When running cargo near build --embed-abi as well as cargo near abi on the following contract sources: https://github.com/archetype-org/attestation-registry/blob/main/src/lib.rs

I get the following error:

error[E0277]: the trait bound `Attestation: JsonSchema` is not satisfied
 │    --> src/lib.rs:224:10
 │     |
 │ 224 |     ) -> Attestations {
 │     |          ^^^^^^^^^^^^ the trait `JsonSchema` is not implemented for `Attestation`
 │     |
 │     = help: the following other types implement trait `JsonSchema`:
 │               bool
 │               char
 │               isize
 │               i8
 │               i16
 │               i32
 │               i64
 │               i128
 │             and 165 others
 │     = note: required for `std::vec::Vec<Attestation>` to implement `JsonSchema`
 │ note: required by a bound in `SchemaGenerator::subschema_for`
 │    --> /Users/c8r7/.cargo/registry/src/index.crates.io-6f17d22bba15001f/schemars-0.8.16/src/gen.rs:221:38
 │     |
 │ 221 |     pub fn subschema_for<T: ?Sized + JsonSchema>(&mut self) -> Schema {
 │     |                                      ^^^^^^^^^^ required by this bound in `SchemaGenerator::subschema_for`
 │ 
 │ error[E0277]: the trait bound `Attestation: JsonSchema` is not satisfied
 │    --> src/lib.rs:241:10
 │     |
 │ 241 |     ) -> Attestation {
 │     |          ^^^^^^^^^^^ the trait `JsonSchema` is not implemented for `Attestation`
 │     |
 │     = help: the following other types implement trait `JsonSchema`:
 │               bool
 │               char
 │               isize
 │               i8
 │               i16
 │               i32
 │               i64
 │               i128
 │             and 165 others
 │ note: required by a bound in `SchemaGenerator::subschema_for`
 │    --> /Users/c8r7/.cargo/registry/src/index.crates.io-6f17d22bba15001f/schemars-0.8.16/src/gen.rs:221:38
 │     |
 │ 221 |     pub fn subschema_for<T: ?Sized + JsonSchema>(&mut self) -> Schema {
 │     |                                      ^^^^^^^^^^ required by this bound in `SchemaGenerator::subschema_for`
 │ 
 │ For more information about this error, try `rustc --explain E0277`.
 │ error: could not compile `attestation-registry` (lib) due to 2 previous errors

Similar code also fails due to the same reason: https://github.com/NEAR-DevHub/neardevhub-contract

error[E0277]: the trait bound `access_control::AccessControl: JsonSchema` is not satisfied
 │    --> src/access_control/mod.rs:22:46
 │     |
 │ 22  |     pub fn get_access_control_info(&self) -> &AccessControl {
 │     |                                              ^^^^^^^^^^^^^^ the trait `JsonSchema` is not implemented for `access_control::AccessControl`
 │     |
 │     = 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 163 others
 │     = note: required for `&access_control::AccessControl` to implement `JsonSchema`
 │ note: required by a bound in `SchemaGenerator::subschema_for`
 │    --> /Users/ebraem/.cargo/registry/src/index.crates.io-6f17d22bba15001f/schemars-0.8.15/src/gen.rs:221:38
 │     |
 │ 221 |     pub fn subschema_for<T: ?Sized + JsonSchema>(&mut self) -> Schema {
 │     |                                      ^^^^^^^^^^ required by this bound in `SchemaGenerator::subschema_for`
 │
 │ error[E0277]: the trait bound `RulesList: JsonSchema` is not satisfied
 │    --> src/access_control/mod.rs:34:51
 │     |
 │ 34  |     pub fn set_restricted_rules(&mut self, rules: RulesList) {
 │     |                                                   ^^^^^^^^^ the trait `JsonSchema` is not implemented for `RulesList`
 │     |
 │     = 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 163 others

I believe this is because the abigen does not support the same types of schema traits as borsh. Is this a limitation with the standard or will support be added in the future.

timurguvenkaya commented 4 days ago

I am experiencing the same issue. If you derive below on struct and use that struct as an argument to a public function, ABI generation fails.

        #[derive(near_sdk::serde::Serialize, near_sdk::serde::Deserialize, Debug)]
        #[serde(crate = "near_sdk::serde")]
        SomeContext{with_some_fields}

       pub fn (context: SomeContext){}

If you use #[near(serializers=[json,borsh])] -> all works fine