dfinity / candid

Candid Library for the Internet Computer
Apache License 2.0
278 stars 81 forks source link

Support serde rename_all #66

Open chenyan-dfinity opened 4 years ago

chenyan-dfinity commented 4 years ago
#[serde(rename_all = "snake_case")]
pub enum CanisterStatusType {
    Running,
    Stopping,
    Stopped,
}

Discussed https://github.com/dfinity-lab/dfinity/pull/4812

ufoscout commented 1 year ago

We experienced this problem today. This issue makes it impossible to have a struct that derives both CandidType and serde Serialize. For example:

#[derive(CandidType, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Person {
    first_name: String,
    last_name: String,
}

In this case:

Is there any plan to fix it?

chenyan-dfinity commented 1 year ago

As a workaround, you can use #[serde(rename = "")] instead.

struct Person {
    #[serde(rename = "firstName")]
    first_name: String,
    #[serde(rename = "lastName")]
    last_name: String,
}
ufoscout commented 1 year ago

@chenyan-dfinity Yes, that's a valid workaround even if some of my structs have more than 30 fields :disappointed_relieved:

Anyway, while this is not supported, you should break the compilation or at least inform the user with a warning, in fact, this compiles but fails at runtime when a canister calls an endpoint that uses this type:

#[derive(CandidType, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Person {
    first_name: String,
    last_name: String,
}
maksymar commented 1 week ago

+1 to initial request to support #[serde(rename_all = "snake_case")].