Repository: GREsau/schemars (Rust)

Issue:
Add optional dependency/handling of url
The https://crates.io/crates/url crate seems to be the common standard through the ecosystem. It would be nice if it could be handled so that we can generate schemas for structs containing URLs.

I would expect this code to compile:

```rust
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use url::Url;

#[derive(JsonSchema, Serialize, Deserialize)]
pub struct Foo {
  pub uri: Url
}
```

though currently it fails with:
```
   --> src/model/api.rs:104:14
    |
104 |     pub uri: Url,
    |              ^^^ the trait `schemars::JsonSchema` is not implemented for `url::Url`
    |
    = note: required by `schemars::JsonSchema::add_schema_as_property`
```


Discussion:
BTW, if you agree it's worth adding, I can try implementing it behind feature flag.
Implemented and published! https://crates.io/crates/schemars/0.8.2



Patch:
diff --git a/README.md b/README.md
index a7946c0e..fc107c19 100644
--- a/README.md
+++ b/README.md
@@ -215,3 +215,4 @@ Schemars can implement `JsonSchema` on types from several popular crates, enable
 - [`uuid`](https://crates.io/crates/uuid) (^0.8)
 - [`smallvec`](https://crates.io/crates/smallvec) (^1.0)
 - [`arrayvec`](https://crates.io/crates/arrayvec) (^0.5)
+- [`url`](https://crates.io/crates/url) (^2.0)
diff --git a/schemars/Cargo.toml b/schemars/Cargo.toml
index 2c5d7903..5f8e0c0c 100644
--- a/schemars/Cargo.toml
+++ b/schemars/Cargo.toml
@@ -24,6 +24,7 @@ either = { version = "1.3", default-features = false, optional = true }
 uuid = { version = "0.8", default-features = false, optional = true }
 smallvec = { version = "1.0", optional = true }
 arrayvec = { version = "0.5", default-features = false, optional = true }
+url = { version = "2.0", default-features = false, optional = true }
 
 [dev-dependencies]
 pretty_assertions = "0.6.1"
@@ -77,5 +78,9 @@ required-features = ["impl_json_schema"]
 name = "ui"
 required-features = ["ui_test"]
 
+[[test]]
+name = "url"
+required-features = ["url"]
+
 [package.metadata.docs.rs]
 all-features = true
diff --git a/schemars/src/json_schema_impls/mod.rs b/schemars/src/json_schema_impls/mod.rs
index d6d6d16c..d493ea00 100644
--- a/schemars/src/json_schema_impls/mod.rs
+++ b/schemars/src/json_schema_impls/mod.rs
@@ -64,6 +64,8 @@ mod serdejson;
 mod smallvec;
 mod time;
 mod tuple;
+#[cfg(feature = "url")]
+mod url;
 #[cfg(feature = "uuid")]
 mod uuid;
 mod wrapper;
diff --git a/schemars/src/json_schema_impls/url.rs b/schemars/src/json_schema_impls/url.rs
new file mode 100644
index 00000000..ffc5426f
--- /dev/null
+++ b/schemars/src/json_schema_impls/url.rs
@@ -0,0 +1,21 @@
+use crate::gen::SchemaGenerator;
+use crate::schema::*;
+use crate::JsonSchema;
+use url::Url;
+
+impl JsonSchema for Url {
+    no_ref_schema!();
+
+    fn schema_name() -> String {
+        "Url".to_owned()
+    }
+
+    fn json_schema(_: &mut SchemaGenerator) -> Schema {
+        SchemaObject {
+            instance_type: Some(InstanceType::String.into()),
+            format: Some("uri".to_owned()),
+            ..Default::default()
+        }
+        .into()
+    }
+}
diff --git a/schemars/src/lib.rs b/schemars/src/lib.rs
index bb189bb3..4f538ede 100644
--- a/schemars/src/lib.rs
+++ b/schemars/src/lib.rs
@@ -210,6 +210,7 @@ Schemars can implement `JsonSchema` on types from several popular crates, enable
 - [`uuid`](https://crates.io/crates/uuid) (^0.8)
 - [`smallvec`](https://crates.io/crates/smallvec) (^1.0)
 - [`arrayvec`](https://crates.io/crates/arrayvec) (^0.5)
+- [`url`](https://crates.io/crates/url) (^2.0)
 */
 
 /// The map type used by schemars types.


Test patch:
diff --git a/schemars/tests/expected/url.json b/schemars/tests/expected/url.json
new file mode 100644
index 00000000..ddb8c5a1
--- /dev/null
+++ b/schemars/tests/expected/url.json
@@ -0,0 +1,14 @@
+{
+  "$schema": "http://json-schema.org/draft-07/schema#",
+  "title": "UrlTypes",
+  "type": "object",
+  "required": [
+    "url"
+  ],
+  "properties": {
+    "url": {
+      "type": "string",
+      "format": "uri"
+    }
+  }
+}
diff --git a/schemars/tests/url.rs b/schemars/tests/url.rs
new file mode 100644
index 00000000..a5b8acd0
--- /dev/null
+++ b/schemars/tests/url.rs
@@ -0,0 +1,14 @@
+mod util;
+use schemars::JsonSchema;
+use url::Url;
+use util::*;
+
+#[derive(Debug, JsonSchema)]
+struct UrlTypes {
+    url: Url,
+}
+
+#[test]
+fn url_types() -> TestResult {
+    test_default_generated_schema::<UrlTypes>("url")
+}