1

I'm trying to implement JsonSchema for a struct like this:

use chrono::{DateTime, Utc};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Default, Clone, JsonSchema)]
pub struct ArticleResponse {
    pub pub_time: Option<DateTime<Utc>>,
}

But this outputs an error when compiling:

error[E0277]: the trait bound `DateTime<Utc>: JsonSchema` is not satisfied
  --> src/main.rs:4:50
   |
 4 | #[derive(Serialize, Deserialize, Default, Clone, JsonSchema)]
   |                                                  ^^^^^^^^^^ the trait `JsonSchema` is not implemented for `DateTime<Utc>`

What should I do to implement JsonSchema for ArticleResponse?

kmdreko
  • 42,554
  • 6
  • 57
  • 106
Dolphin
  • 29,069
  • 61
  • 260
  • 539

1 Answers1

4

To quote the docs:

For example, to implement JsonSchema on types from chrono, enable it as a feature in the schemars dependency in your Cargo.toml like so:

[dependencies]
schemars = { version = "0.8", features = ["chrono"] }
kmdreko
  • 42,554
  • 6
  • 57
  • 106
isaactfa
  • 5,461
  • 1
  • 10
  • 24