ProbablyClem / utoipauto

Rust Macros to automate the addition of Paths/Schemas to Utoipa crate, simulating Reflection during the compilation phase
Apache License 2.0
108 stars 10 forks source link

Add custom ToSchema/ToResponse implementation support #12

Closed saman3d closed 7 months ago

saman3d commented 7 months ago

some times users can't use derive macro ToSchema on custom Serialized/Deserialized Data or wrapper types that inner type is outside library and doesn't implement to schema or serilization:

#[derive(Debug)]
pub struct UUID(pub uuid::Uuid);

impl<'__s> ToSchema<'__s> for UUID {
    fn schema() -> (&'__s str, utoipa::openapi::RefOr<utoipa::openapi::schema::Schema>) {
        (
            "UUID",
            utoipa::openapi::ObjectBuilder::new()
                .schema_type(utoipa::openapi::SchemaType::String)
                .into()
        )
    }
}

impl Into<uuid::Uuid> for UUID {
    fn into(self) -> uuid::Uuid {
        self.0
    }
}

impl<'de> Deserialize<'de> for UUID {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de> {
        <&str>::deserialize(deserializer).and_then(|s| {
            let u = uuid::Uuid::from_str(s).map_err(|e| de::Error::custom(format!("invalid uuid string: {}, {}", s, e)))?;

            Ok(Self(u))
        })
    }
}

i added the support in this pull request by discovering implementations of ToSchema and ToResponse

ProbablyClem commented 7 months ago

Hey, thanks for your contribution. I added test cases and they are failing. For some reason, it only works with one implementation but not the second...

ProbablyClem commented 7 months ago

Ok nvm, it's just that my impl blocks were identical for both struct so utoipa would not count them as two distinct. Thanks for your work. Will release a new version