Can't get serde working with rocket


#1

Hi all

I’m playing around with serde and rocket but can’t quite get my code to work.

Cargo.toml

[package]
name = "rocket_serde_test"
version = "0.1.0"

[dependencies]
rocket = "0.1.6"
rocket_codegen = "0.1.6"
rocket_contrib = "0.1.6"
serde = "0.9.6"
serde_derive = "0.9.6"

main.rs

#![feature(plugin)]
#![plugin(rocket_codegen)]

extern crate serde;
#[macro_use] extern crate serde_derive;

extern crate rocket;
#[macro_use] extern crate rocket_contrib;

use rocket_contrib::JSON;

#[derive(Deserialize)]
pub struct Test {
    pub email: String
}

#[post("/users", data = "<test>")]
fn test(test: JSON<Test>) -> String {
    "Success".into()
}

fn main() {
    rocket::ignite().mount("/v1", routes![test]).launch();
}

Compiling this on rustc 1.17.0-nightly (0648517fa 2017-02-03) gives me the following error.

error[E0277]: the trait bound `Test: serde::de::Deserialize` is not satisfied
  --> src/main.rs:19:1
   |
19 | #[post("/users", data = "<test>")]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `serde::de::Deserialize` is not implemented for `Test`
   |
   = note: required because of the requirements on the impl of `rocket::data::FromData` for `rocket_contrib::JSON<Test>`

Anyone that can spot what I am doing wrong? I am not able to figure it out :frowning:


#2

You could have different versions of serde, try installing cargo-tree (cargo install cargo-tree) then run cargo tree -i -p serde to see the inverted dependency tree of what versions of serde you have.


#3

Yeh I think you’ll have to downgrade serde from 0.9 to 0.8, it looks like that’s what rocket_contrib expects.

That error is pretty unhelpful. Some cases where you get incompatible versions of the same type have been given a better message but it looks like that case isn’t covered yet.


#4

Thanks

Running cargo-tree give me the following output.

$ cargo tree -i -p serde
error: There are multiple `serde` packages in your project, and the specification `serde` is ambiguous.
Please re-run this command with `-p <spec>` where `<spec>` is one of the following:
  serde:0.8.21
  serde:0.9.6

And downgrading to 0.8.21 allowed me to compile. Can I report this anywhere, are there guidelines for how frameworks and libraries should handle this?


#5

Rocket 0.2.0 includes the update to Serde 0.9 so there is no need to report this. The milestone in their issue tracker is complete so we may just be waiting for a release announcement.


#6

Nice to hear, looking forward to that =)


#7

Just to close the loop here - Rocket 0.2 has been released and works with Serde 0.9.

https://rocket.rs/news/2017-02-06-version-0.2/