GREsau / okapi

OpenAPI (AKA Swagger) document generation for Rust projects
MIT License
631 stars 112 forks source link

How to impl JsonSchema for chrono::DateTime<Utc> or other lib struct? #125

Closed ArkadyRudenko closed 1 year ago

ArkadyRudenko commented 1 year ago

I have a code:

#[derive(Debug, Clone, Deserialize, Serialize, Queryable, JsonSchema)]
#[serde(crate = "rocket::serde")]
struct NewFork {
    id: i32,
    place_at:  chrono::DateTime<Utc>,
    data: Option<serde_json::Value>,
}

and endpoint

#[openapi(tag = "Forks", ignore = "db")]
#[post("/insert_data", format = "json", data = "<data>")]
async fn insert(mut db: PgConnection, data: Json<NewDbFork>) -> Result<Created<Json<NewFork>>, Json<ApiError>> {
    todo!()
}

And I`ve got a error:

error[E0277]: the trait bound `chrono::DateTime<Utc>: JsonSchema` is not satisfied
   --> src/main.rs:59:16
    |
59  |     place_at:  chrono::DateTime<Utc>,
    |                ^^^^^^^^^^^^^^^^^^^^^ the trait `JsonSchema` is not implemented for `chrono::DateTime<Utc>`

How can I fix it? I can not impl JsonSchema for type from other lib

ArkadyRudenko commented 1 year ago

duplicate of https://stackoverflow.com/questions/73405960/how-to-use-chronodatetimeutc-with-schemarsjsonschema?rq=1