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

impl JsonSchema for basic camino types #214

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

dbanty
Copy link

@dbanty dbanty commented Apr 12, 2023

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.

@AzHicham
Copy link

AzHicham commented Dec 1, 2023

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.

@aidanwolter3
Copy link

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.

@sunshowers
Copy link

sunshowers commented Mar 30, 2024

(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.

no_ref_schema!();

fn schema_name() -> String {
"String".to_owned()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So hmmmm...

I think this shouldn't be String, I believe -- it should be something more specific like Utf8PathBuf.

This goes into the format field in the OpenAPI and JSON schema spec. There are some defined formats, but otherwise it's an open universe (based on https://swagger.io/docs/specification/data-models/data-types/ and https://json-schema.org/understanding-json-schema/reference/string#format).

But maybe this should be exposed as a custom field on the side, e.g. x-rust-type. @ahl do you have thoughts?

@aidanwolter3
Copy link

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

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

Successfully merging this pull request may close these issues.

4 participants