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

std::time::SystemTime support #1222

Open
SamuelMarks opened this issue Dec 1, 2024 · 5 comments
Open

std::time::SystemTime support #1222

SamuelMarks opened this issue Dec 1, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@SamuelMarks
Copy link
Contributor

the trait bound SystemTime: ToSchema is not satisfied

error[E0277]: the trait bound SystemTime: PartialSchema is not satisfied

I've tried enabling crate features of chrono or time; to no avail.

#[derive(
    Debug,
    Clone,
    serde::Serialize,
    serde::Deserialize,
    diesel::AsChangeset,
    utoipa::ToSchema,
    PartialEq,
    Default,
)]
pub struct S {
    pub t: Option<std::time::SystemTime>,
}

How do I get this to work?

@juhaku
Copy link
Owner

juhaku commented Dec 1, 2024

This is actually same issue as with any other type that does not implement ToSchema by default. See more details here: #1205 (comment)

There would be a need to add support for the SystemTime to utoipa-gen the way the support has been added to chono and other similar types.

@juhaku juhaku added the enhancement New feature or request label Dec 1, 2024
@SamuelMarks
Copy link
Contributor Author

@juhaku Hmm, so something like:

#[derive(ToSchema)]
#[schema(as = SystemTime, value_type = SystemTime)]
struct MySystemTime(SystemTime);

Or more something like:

impl PrimitiveType {
    pub fn new(path: &Path) -> Option<PrimitiveType> {
        let last_segment = path.segments.last().unwrap_or_else(|| {
            panic!(
                "Path for DefaultType must have at least one segment: `{path}`",
                path = path.to_token_stream()
            )
        });

        let name = &*last_segment.ident.to_string();

        let ty: syn::Type = match name {
            "String" | "str" | "char" => syn::parse_quote!(#path),

            "bool" => syn::parse_quote!(#path),

            "i8" | "i16" | "i32" | "i64" | "i128" | "isize" | "u8" | "u16" | "u32" | "u64"
            | "u128" | "usize" => syn::parse_quote!(#path),
            "f32" | "f64" => syn::parse_quote!(#path),

            "SystemTime" => syn::parse_quote!(#SystemTime),

Or even combine the two:

"SystemTime" => syn::parse_quote!(#MySystemTime),

@SamuelMarks
Copy link
Contributor Author

FWIW: In the interim; for this project; I've switched to chrono from diesel up to utoipa.

@juhaku
Copy link
Owner

juhaku commented Dec 1, 2024

How the SytemTime renders when it is serialized to JSON? Is it a number in seconds or is it a string? Depending on the type you can locally alias it with value_type.

[derive(ToSchema)]
#[schema(as = SystemTime, value_type = String)] // This will treat it as a String, 
struct MySystemTime(SystemTime);

@juhaku
Copy link
Owner

juhaku commented Dec 1, 2024

But in order to make utoipa support the SystemTime internally, the schema type conversion should be implemented here:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants