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

Failed to parse NaiveDateTime #436

Closed
chikko80 opened this issue Nov 30, 2022 · 4 comments
Closed

Failed to parse NaiveDateTime #436

chikko80 opened this issue Nov 30, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@chikko80
Copy link
Contributor

Expected Behavior

Parsing of NaiveDateTime Object

Actual Behavior

Does not parse successfully
Error message:

parse request payload error: failed to parse "string(naive-date-time)": input contains invalid characters (occurred while parsing "TestStruct")

Steps to Reproduce the Problem

use chrono::{NaiveDate, NaiveTime};
use poem::{listener::TcpListener, Route, Server};
use poem_openapi::{
    payload::{Json, PlainText},
    Object, OpenApi, OpenApiService,
};

struct Api;

#[derive(Object, Debug)]
#[oai(example)]
pub struct TestStruct {
    pub date: chrono::NaiveDateTime,
}

impl poem_openapi::types::Example for TestStruct {
    fn example() -> Self {
        Self {
            date: chrono::NaiveDateTime::new(
                NaiveDate::from_ymd_opt(2023, 5, 1).unwrap(),
                NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
            ),
        }
    }
}

#[OpenApi]
impl Api {
    #[oai(path = "/hello", method = "post")]
    async fn index(&self, test: Json<TestStruct>) -> PlainText<String> {
        PlainText(format!("{:?}", test))
    }
}

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
    if std::env::var_os("RUST_LOG").is_none() {
        std::env::set_var("RUST_LOG", "poem=debug");
    }
    tracing_subscriber::fmt::init();

    let api_service =
        OpenApiService::new(Api, "Hello World", "1.0").server("http://localhost:3000/api");
    let ui = api_service.swagger_ui();

    Server::new(TcpListener::bind("127.0.0.1:3000"))
        .run(Route::new().nest("/api", api_service).nest("/", ui))
        .await
}
@chikko80 chikko80 added the bug Something isn't working label Nov 30, 2022
@sunli829
Copy link
Collaborator

The format must be as follows:

let dt = NaiveDate::from_ymd(2015, 9, 18).and_hms(23, 56, 4);
assert_eq!("2015-09-18T23:56:04".parse::<NaiveDateTime>(), Ok(dt));

@sunli829 sunli829 removed the bug Something isn't working label Nov 30, 2022
@chikko80
Copy link
Contributor Author

chikko80 commented Nov 30, 2022

It's a bit confusing that the example schema is generated with "2015-09-18 23:56:04".
Is that something related to chrono or poem?
Is it possible to improve the example schema generation to generate the correct format (2015-09-18T23:56:04) for the example NaiveDateTime Object?

Edit:
Seems like NaiveDateTime.to_string() (display trait) gives the wrong format while the debug trait gives the correct format

@sunli829
Copy link
Collaborator

sunli829 commented Dec 1, 2022

Released in poem-openapi@2.0.21 🙂

@sunli829 sunli829 added the bug Something isn't working label Dec 1, 2022
@chikko80
Copy link
Contributor Author

chikko80 commented Dec 1, 2022

You are awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants