Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server fn call fails with empty HashSet #2337

Closed
gitmalong opened this issue Feb 18, 2024 · 3 comments
Closed

server fn call fails with empty HashSet #2337

gitmalong opened this issue Feb 18, 2024 · 3 comments

Comments

@gitmalong
Copy link

gitmalong commented Feb 18, 2024

#[server(SettingsApi, "/api")]
pub async fn settings_api(settings: Settings) -> Result<(), ServerFnError> {
    // do something
    Ok(())
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Settings {
    pub currency: String,
    pub time_zone: String,
    /// Default valuation method
    pub valuation: ValuationMethod,
    /// Custom valuation methods
    pub valuations: HashSet<CustomValuationMethod>,
}

When calling settings_api from client side with a Settings struct that has a empty valuations HashSet(created via Default::default()) the call fails.

The browser sends only the following payload

year: 2021
settings[currency]: EUR
settings[time_zone]: Europe/Berlin
settings[valuation]: Fifo

As valuations is not an Option<HashSet<CustomValuationMethod>> value and it's therefore impossible to compile a program that does not set this field I consider this as a bug.

@gbj
Copy link
Collaborator

gbj commented Feb 18, 2024

valuations should have the #[serde(default)] annotation added to it, and I think this will fix it. This is a quirk of serde_qs, the library we use for URL encoding form data.

@gitmalong
Copy link
Author

gitmalong commented Feb 18, 2024

Seems I was mistaken, thanks.I thought such a default annotation does not exist due to this comment: serde-rs/serde#1098 (comment)

However the serde default workaround works fine.

I found a related issue on serde_qs's side samscott89/serde_qs#78

@gbj
Copy link
Collaborator

gbj commented Feb 19, 2024

For future readers, this is documented here.

@gbj gbj closed this as completed Feb 19, 2024
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

No branches or pull requests

2 participants