boxdot / openapi-schema

Apache License 2.0
4 stars 1 forks source link

problem with serde(flatten) and nested modules #8

Closed antoine-de closed 5 years ago

antoine-de commented 5 years ago

Hi,

Thanks for the great work on this lib.

I'm stumbling upon a problem with the serde(flatten) support.

The following example does not work:

pub mod nested {
    use openapi_schema::OpenapiSchema;
    use serde::Serialize;
    #[derive(OpenapiSchema, Serialize)]
    pub struct A {
        primitive_field: u64,
    }
    #[derive(OpenapiSchema, Serialize)]
    pub struct B {
        pub inner: A,
    }
}

#[derive(OpenapiSchema, Serialize)]
#[allow(dead_code)]
struct C {
    outer: u64,
    #[serde(flatten)]
    flatten: nested::B,
}

the following expect fails.

It comes from the fact that the type name seems to depend on the scope, so it's either A or nested::A depending on the context.

I'm a complete noob with syn, so I made an ugly fix spliting the ty name by " .. " :see_no_evil:

Isn't there a better way to get an unmangled name ?

Do you want a PR with this ?

boxdot commented 5 years ago

Nice catch. Thank you very much for the fix and the test. I took the chance to take you commit and to put some code on top of it in #11. My fix is also not very nice, but it is type safe.

I think there is a better way in syn to define parsers, like the example in this function https://docs.rs/syn/0.15.33/syn/struct.Path.html#method.parse_mod_style. However, I need first to learn how to do this in our case.

antoine-de commented 5 years ago

great, thanks! can I close this or do you want to keep it open to check parse_mod_style ?

boxdot commented 5 years ago

@antoine-de We can close this.