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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ Schemars can implement `JsonSchema` on types from several popular crates, enable
- `bigdecimal04` - [bigdecimal](https://crates.io/crates/bigdecimal) (^0.4)
- `smol_str` - [smol_str](https://crates.io/crates/smol_str) (^0.1.17)
- `semver` - [semver](https://crates.io/crates/semver) (^1.0.9)
- `camino` - [camino](https://crates.io/crates/camino) (^1.1)

For example, to implement `JsonSchema` on types from `chrono`, enable it as a feature in the `schemars` dependency in your `Cargo.toml` like so:

Expand Down
1 change: 1 addition & 0 deletions schemars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.25"
dyn-clone = "1.0"

camino = { version = "1.1", optional = true }
chrono = { version = "0.4", default-features = false, optional = true }
indexmap = { version = "1.2", features = ["serde-1"], optional = true }
indexmap2 = { version = "2.0", features = ["serde"], optional = true, package = "indexmap" }
Expand Down
36 changes: 36 additions & 0 deletions schemars/src/json_schema_impls/camino.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use camino::{Utf8Path, Utf8PathBuf};

impl JsonSchema for Utf8Path {
no_ref_schema!();

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

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::String.into()),
..Default::default()
}
.into()
}
}

impl JsonSchema for Utf8PathBuf {
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?

}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::String.into()),
..Default::default()
}
.into()
}
}
2 changes: 2 additions & 0 deletions schemars/src/json_schema_impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,5 @@ mod uuid08;
#[cfg(feature = "uuid1")]
mod uuid1;
mod wrapper;
#[cfg(feature = "camino")]
mod camino;
Loading