【问题标题】:the trait `JsonSchema` is not implemented for `chrono::DateTime<Utc>``chrono::DateTime<Utc>` 没有实现特征`JsonSchema`
【发布时间】:2022-08-18 23:59:26
【问题描述】:

当我尝试为这样的 rust 结构添加 JsonSchema 时:

use std::time::Duration;
use chrono::{DateTime, Utc};
use tokio::time;
use rocket_okapi::okapi::schemars;
use rocket_okapi::okapi::schemars::JsonSchema;

#[tokio::main]
async fn main() {

}

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

使用 cargo build 命令编译时显示错误:

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

我应该怎么做才能为ArticleResponse 添加JsonSchema?这是 toml 的依赖项:

[package]
name = \"rust-learn\"
version = \"0.1.0\"
edition = \"2018\"

[dependencies]
rocket = { version = \"=0.5.0-rc.2\", features = [\"json\"] }

serde = { version = \"1.0.64\", features = [\"derive\"] }
serde_json = \"1.0.64\"
serde_derive = \"1.0\"
# database
diesel = { version = \"1.4.7\", features = [\"postgres\",\"serde_json\"] }
dotenv = \"0.15.0\"
chrono = { version = \"0.4\", features = [\"serde\"] }
diesel_full_text_search = { git = \"https://github.com/RedDwarfTech/diesel_full_text_search.git\"}
rust_wheel = { git = \"https://github.com/jiangxiaoqiang/rust_wheel.git\" }
tokio = { version = \"1.17.0\", features = [\"full\"] }

okapi = { git = \"https://github.com/GREsau/okapi.git\"}
schemars = \"0.7\"
rocket_okapi = { git = \"https://github.com/GREsau/okapi.git\", features = [\"swagger\", \"rapidoc\"] }

    标签: rust okapi


    【解决方案1】:

    引用the docs

    例如,要在 chrono 中的类型上实现 JsonSchema,请将其作为 Cargo.toml 中的 schemars 依赖项中的一项功能启用,如下所示:

    [dependencies]
    schemars = { version = "0.8", features = ["chrono"] }
    

    【讨论】:

      猜你喜欢
      • 2018-10-11
      • 2015-04-29
      • 2023-03-21
      • 2021-07-18
      • 2022-01-26
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      • 2019-12-08
      相关资源
      最近更新 更多