Skip to content

Unable to handle null return types from Rust #581

@dansteren

Description

@dansteren

Describe the bug
When using the JS agent to make requests to Rust canisters methods that return null, the agent throws the error: Wrong number of return values.

To Reproduce

  1. Create a new rust canister using dfx: dfx new --type=rust actor_test
  2. Enter the directory: cd actor_test
  3. Update the canister's lib.rs file to contain one method that returns the unit type (which according to the candid reference is the equivalent of motoko's Null type):
    #[ic_cdk_macros::query]
    fn get_null() -> () {
        return ();
    }
  4. Update the candid file to match:
    service : {
        "get_null": () -> (null) query;
    }
    
  5. Start up the replica: dfx start
  6. Deploy the canister: dfx deploy
  7. Open the generated candid UI website in your browser (likely at http://127.0.0.1:8000/?canisterId=r7inp-6aaaa-aaaaa-aaabq-cai&id=rrkah-fqaaa-aaaaa-aaaaq-cai)
  8. Open the dev tools to see the console output
  9. Click the "QUERY" button in the UI to initiate a call to the get_null canister method
  10. Note that the error message "Wrong number of return values" is displayed in the UI and in the console.
  11. Make the equivalent call using DFX and note that there aren't any problems: dfx canister call actor_test get_null => (null: null)

Expected behavior
The agent should handle the null return value instead of throwing an error, and both the call and the result (null) should be displayed in the output log in the Candid UI.

Screenshots
image
Screenshot from 2022-06-08 16-55-30

Desktop (please complete the following information):

Additional context
This is only a problem for Rust and Azle canisters. The agent handles null responses from Motoko canisters without problem. For example, consider the following Motoko canister:

actor ActorTest {
  public query func get_null(): async Null {
    return null;
  };
}

The candid file for both this motoko canister and the provided Rust canister are the same, namely:

service : {
  "get_null": () -> (null) query;
}

Therefore, it should be that the the agent can handle both canisters the same. However, only the null value being returned by the Motoko canister is correctly handled by the JS Agent.

Also worth noting is that although the JS Agent doesn't handle the Rust return values, both canisters return (null: null) when executing dfx canister call agent_test get_null from the command line.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions