schemars icon indicating copy to clipboard operation
schemars copied to clipboard

impl JsonSchema for basic camino types

Open dbanty opened this issue 2 years ago • 4 comments

I'd like to be able to use Utf8Path and Utf8PathBuf like the std equivalents when deriving JsonSchema. I believe the implementation is the same as for those types over in primitives.

There should be a camino feature that gates this based on the optional dependency.

dbanty avatar Apr 12 '23 17:04 dbanty

Is this gonna be merged soon ? TBH We use a lot Utf8PathBuf instead of PathBuf in our apps. And since we are planning to migrate to axum and use aide (OpenApi/Swagger) we absolutely need this feature.

AzHicham avatar Dec 01 '23 11:12 AzHicham

I am also looking forward to when this will merge. We use Utf8PathBuf all over the place which prevents me from using schemars until this is merged in.

aidanwolter3 avatar Mar 28 '24 17:03 aidanwolter3

(camino maintainer here)

Meanwhile, you can use this really 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 schemas with #[schemars(schema_with = "path_schema")].

You can do something similar for Option<Utf8PathBuf>, Vec<Utf8PathBuf> etc.

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

sunshowers avatar Mar 30 '24 04:03 sunshowers

sunshowers's comment works for me! Thank you @sunshowers .

aidanwolter3 avatar Apr 01 '24 20:04 aidanwolter3