Skip to content

Improve error messages for missing JsonSchema trait in ABI generation#387

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/fix-abi-generation-issue-again
Draft

Improve error messages for missing JsonSchema trait in ABI generation#387
Copilot wants to merge 5 commits intomainfrom
copilot/fix-abi-generation-issue-again

Conversation

Copy link
Contributor

Copilot AI commented Jan 14, 2026

When custom types used in contract function signatures lack JsonSchema implementation, ABI generation fails with an unhelpful compiler error that doesn't explain the fix.

Changes

Enhanced error detection (cargo-near-build/src/cargo_native/compile.rs)

  • Detect JsonSchema trait bound errors during compilation
  • Display actionable hint box with two fix options:
    • #[near(serializers = [json])] for near-sdk 5.0+
    • Manual JsonSchema derive from schemars

Added test (integration-tests/tests/abi/json_schema.rs)

  • test_schema_vec_of_custom_types() demonstrates correct usage of Vec<CustomType>

Updated CHANGELOG

Example

Before:

error[E0277]: the trait bound `Attestation: JsonSchema` is not satisfied
   --> src/lib.rs:224:10

After:

error[E0277]: the trait bound `Attestation: JsonSchema` is not satisfied
   --> src/lib.rs:224:10
 │
 │ ╭─────────────────────────────────────────────────────────────────╮
 │ │ HINT: The error above indicates that a type used in your       │
 │ │ contract's public interface does not implement the JsonSchema  │
 │ │ trait, which is required for ABI generation.                   │
 │ │                                                                 │
 │ │ To fix this, add the JsonSchema derive to your struct/enum:    │
 │ │                                                                 │
 │ │ Option 1 (Recommended - works with near-sdk 5.0+):             │
 │ │   #[near(serializers = [json])]                                │
 │ │   pub struct YourType { ... }                                  │
 │ ╰─────────────────────────────────────────────────────────────────╯

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • s3-us-west-1.amazonaws.com
    • Triggering command: /home/REDACTED/work/cargo-near/cargo-near/target/debug/build/near-workspaces-a8b4a713a403149e/build-script-build /home/REDACTED/work/cargo-near/cargo-near/target/debug/build/near-workspaces-a8b4a713a403149e/build-script-build -I libgit2/deps/xdiff 64-REDACTED-linux-gnu/bin/gcc-ld/ld.lld 40df�� go-near/target/d-g ebug/build/libgi-gdwarf-4 l/include son c-rendered-ansi,-O0 h2-sys-e90d28c77-ffunction-sections /usr/libexec/gcc-fdata-sections sl-s�� t2-sys-bc666140e-g t2-sys-bc666140e-gdwarf-4 stable-x86_64-un-fno-omit-frame-pointer ude ude 163292.so stable-x86_64-un--log-level (dns block)
    • Triggering command: /home/REDACTED/work/cargo-near/.codeql-scratch/dbs/rust/working/target/debug/build/near-workspaces-2094825312d8cd28/build-script-build /home/REDACTED/work/cargo-near/.codeql-scratch/dbs/rust/working/target/debug/build/near-workspaces-2094825312d8cd28/build-script-build --gdwarf-4 --64 --noexecstack -o /libdbus-sys-9c66e14519a1242e/out/include /tmp/ccXmTMob.s (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>ABI not generated when functions return a vector of objects or regular JSON</issue_title>
<issue_description>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.
</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits January 14, 2026 00:17
Co-authored-by: r-near <163825889+r-near@users.noreply.github.com>
Co-authored-by: r-near <163825889+r-near@users.noreply.github.com>
Co-authored-by: r-near <163825889+r-near@users.noreply.github.com>
@r-near
Copy link
Contributor

r-near commented Jan 14, 2026

@copilot the error message is nice, but couldn't we also be smart about this and support it in the same way borsh does?

Copilot AI changed the title [WIP] Fix ABI not generated for vector of objects Improve error messages for missing JsonSchema trait in ABI generation Jan 14, 2026
Copilot AI requested a review from r-near January 14, 2026 00:33
Copy link
Contributor Author

Copilot AI commented Jan 14, 2026

@copilot the error message is nice, but couldn't we also be smart about this and support it in the same way borsh does?

The automatic schema derivation like borsh does happens in the near-sdk macros during compilation, not in cargo-near. When users apply #[near] to a struct, near-sdk auto-derives BorshSchema. Similarly, #[near(serializers = [json])] auto-derives JsonSchema.

The issue here is that the user is using manual derives (#[derive(Serialize, Deserialize)]) instead of the #[near] macro, which bypasses the automatic JsonSchema derivation. Since cargo-near runs after macro expansion, we can't retroactively add the trait implementation.

The enhanced error message in this PR is the most helpful approach cargo-near can provide - it clearly guides users to add #[near(serializers = [json])] or manually derive JsonSchema.

For fully automatic support like borsh, that would need to be implemented in near-sdk itself to handle cases where users use manual derives. Would you like me to create an issue in the near-sdk-rs repo to discuss this enhancement?

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.

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

2 participants