camino-rs / camino

Like Rust's std::path::Path, but UTF-8.
https://docs.rs/camino
Apache License 2.0
435 stars 16 forks source link

impl JsonSchema on Utf8Path[Buf] #91

Closed aidanwolter3 closed 6 months ago

aidanwolter3 commented 6 months ago

I would like to use the schemars crate in a project that heavily uses camino, but am currently prevented from doing so because Utf8Path[Buf] does not implement schemars::JsonSchema.

This PR in schemars has not gone anywhere because the maintainer has not reviewed it. https://github.com/GREsau/schemars/pull/214

Could we add these impls in camino instead?

sunshowers commented 6 months ago

Thanks for the question! Sadly no, that wouldn't be the right thing to do, because it would mean that none of the many crates that schemars depends on could depend on camino.

However you can do what I did at work, and use this very simple impl:

fn path_schema(gen: &mut SchemaGenerator) -> Schema {
    let mut schema: SchemaObject = <String>::json_schema(gen).into();
    schema.format = Some("Utf8PathBuf".to_owned());
    schema.into()
}

and annotate your schema with #[schemars(schema_with = "path_schema")].

This is too trivial to be copyrightable, but in case it ever is, consider this to be CC0-1.0.