export_service! macro should correctly name records wrapped in `ManualReply`s

Open
#375 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust

Research direction

Reproduce the issue with the Rust example using candid::export_service! and run cargo test to inspect the generated test.did file. Start by tracing how export_service! handles ManualReply return types; done means the generated candid names the wrapped record User rather than ManualReply or an incremented variant.

Written by the indexing model from the issue text.

Description

Describe the bug
The candid export service doesn't correctly handle canister methods with a return type of ManualReply. Instead of exporting the record type in the canister method signature, it exports a generic ManualReply type. Subsequent ManualReplys will be given incrementing names of format ManualReply_n where n increases for the amount of records.

To Reproduce
Steps to reproduce the behavior:

  1. Create a Rust canister with the following contents:

    use ic_cdk::api::call::{self, ManualReply};
    
    #[derive(candid::CandidType)]
    struct User {
        id: String,
    }
    
    #[ic_cdk_macros::query]
    #[candid::candid_method(query)]
    async fn method() -> ManualReply<User> {
        let user = User {
            id: "a".to_string(),
        };
        call::reply((user,));
        ManualReply::empty()
    }
    
    candid::export_service!();
    #[ic_cdk_macros::query(name = "__get_candid_interface_tmp_hack")]
    fn export_candid() -> String {
        __export_service()
    }
    
    #[cfg(test)]
    mod tests {
        use super::*;
        #[test]
        fn write_candid_to_disk() {
            std::fs::write("test.did", export_candid()).unwrap();
        }
    }
    

    Full example can be found at https://github.com/dansteren/candid_export_bug

  2. Run cargo test

  3. Inspect the generated candid file at canisters/tests/test.did

  4. Notice that it contains a type ManualReply which doesn't have the same name as the User struct in canisters/test/src/lib.rs

    type ManualReply = record { id : text };
    service : { method : () -> (ManualReply) query }
    

Expected behavior
The candid file should name the user type User not ManualReply. I.e.

type User = record { id : text };
service : { method : () -> (User) query }

Screenshots
N/A

Platform

  • OS: Ubuntu 22.04
  • Rust Version: rustc 1.63.0 (4b91a6ea7 2022-08-08)
  • DFX Version: 0.11.0
  • Candid Crate Version: 0.7.4

Additional context
The problem occurs regardless of whether the function is a Query or an Update. The problem does not occur if another function also returns that type (See https://github.com/dansteren/candid_export_bug/tree/include_type_in_other_function_sig).

PS: Your Bug issue template isn't working so I was unable to apply the Bug label.

Dominant language
Rust
Stars
301
Forks
85
Avg merge
1h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dfinity/candid

All issues in dfinity/candid

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.