Skip to content

Commit

Permalink
Add ipnetwork support
Browse files Browse the repository at this point in the history
  • Loading branch information
rlebran committed Jun 25, 2024
1 parent 9633026 commit d59983e
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 49 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ Schemars can implement `JsonSchema` on types from several popular crates, enable
- `either1` - [either](https://crates.io/crates/either) (^1.3)
- `enumset1` - [enumset](https://crates.io/crates/enumset) (^1.0)
- `indexmap2` - [indexmap](https://crates.io/crates/indexmap) (^2.0)
- `ipnetwork` - [ipnetwork](https://crates.io/crates/ipnetwork) (^0.20)
- `rust_decimal1` - [rust_decimal](https://crates.io/crates/rust_decimal) (^1.0)
- `semver1` - [semver](https://crates.io/crates/semver) (^1.0.9)
- `smallvec1` - [smallvec](https://crates.io/crates/smallvec) (^1.0)
Expand Down
1 change: 1 addition & 0 deletions docs/_v0/4-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Schemars can implement `JsonSchema` on types from several popular crates, enable
- `chrono` - [chrono](https://crates.io/crates/chrono) (^0.4)
- `indexmap1` - [indexmap](https://crates.io/crates/indexmap) (^1.2)
- `indexmap2` - [indexmap](https://crates.io/crates/indexmap) (^2.0)
- `ipnetwork` - [ipnetwork](https://crates.io/crates/ipnetwork) (^0.20)
- `either` - [either](https://crates.io/crates/either) (^1.3)
- `uuid08` - [uuid](https://crates.io/crates/uuid) (^0.8)
- `uuid1` - [uuid](https://crates.io/crates/uuid) (^1.0)
Expand Down
1 change: 1 addition & 0 deletions schemars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ chrono04 = { version = "0.4", default-features = false, optional = true, package
either1 = { version = "1.3", default-features = false, optional = true, package = "either" }
enumset1 = { version = "1.0", default-features = false, optional = true, package = "enumset" }
indexmap2 = { version = "2.0", default-features = false, optional = true, package = "indexmap" }
ipnetwork = { version = "0.20", optional = true }
rust_decimal1 = { version = "1", default-features = false, optional = true, package = "rust_decimal"}
semver1 = { version = "1.0.9", default-features = false, optional = true, package = "semver" }
smallvec1 = { version = "1.0", default-features = false, optional = true, package = "smallvec" }
Expand Down
97 changes: 53 additions & 44 deletions schemars/src/json_schema_impls/ipnetwork.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,69 @@
use ipnetwork::{IpNetwork, Ipv4Network, Ipv6Network};
use crate::gen::SchemaGenerator;
use crate::JsonSchema;
use crate::schema::{InstanceType, Schema, SchemaObject, SubschemaValidation};
use crate::{json_schema, JsonSchema, Schema};
use ipnetwork::{IpNetwork, Ipv4Network, Ipv6Network};
use std::borrow::Cow;

impl JsonSchema for IpNetwork {
no_ref_schema!();

fn schema_name() -> String {
"Ip".to_string()
}

fn json_schema(gen: &mut SchemaGenerator) -> Schema {
SchemaObject {
subschemas: Some(Box::new(SubschemaValidation {
one_of: Some(vec![
Ipv4Network::json_schema(gen),
Ipv6Network::json_schema(gen),
]),
..Default::default()
})),
..Default::default()
}
.into()
}
always_inline!();

fn schema_name() -> Cow<'static, str> {
"Ip".into()
}

fn schema_id() -> Cow<'static, str> {
"ipnetwork::Ip".into()
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
json_schema!({
"oneOf": [
{
"type": "string",
"format": "ipv4"
}
,{
"type": "string",
"format": "ipv6"
}
]
})
}
}

impl JsonSchema for Ipv4Network {
no_ref_schema!();
always_inline!();

fn schema_name() -> Cow<'static, str> {
"IpV4".into()
}

fn schema_name() -> String {
"IpV4".to_string()
}
fn schema_id() -> Cow<'static, str> {
"ipnetwork::IpV4".into()
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::String.into()),
format: Some("ipv4".to_string()),
..Default::default()
fn json_schema(_: &mut SchemaGenerator) -> Schema {
json_schema!({
"type": "string",
"format": "ipv4"
})
}
.into()
}
}

impl JsonSchema for Ipv6Network {
no_ref_schema!();
always_inline!();

fn schema_name() -> String {
"IpV6".to_string()
}
fn schema_name() -> Cow<'static, str> {
"IpV6".into()
}

fn schema_id() -> Cow<'static, str> {
"ipnetwork::Ipv6Network".into()
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::String.into()),
format: Some("ipv6".to_string()),
..Default::default()
fn json_schema(_: &mut SchemaGenerator) -> Schema {
json_schema!({
"type": "string",
"format": "ipv6"
})
}
.into()
}
}
3 changes: 3 additions & 0 deletions schemars/src/json_schema_impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ forward_impl!((<T: enumset1::EnumSetType + crate::JsonSchema> crate::JsonSchema
#[cfg(feature = "indexmap2")]
mod indexmap2;

#[cfg(feature = "ipnetwork")]
mod ipnetwork;

#[cfg(feature = "semver1")]
mod semver1;

Expand Down
2 changes: 1 addition & 1 deletion schemars/tests/enum.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod util;
extern crate apistos_schemars as schemars;
use std::collections::BTreeMap;
use schemars::JsonSchema;
use std::collections::BTreeMap;
use util::*;

// Ensure that schemars_derive uses the full path to std::string::String
Expand Down
2 changes: 1 addition & 1 deletion schemars/tests/enum_deny_unknown_fields.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod util;
extern crate apistos_schemars as schemars;
use std::collections::BTreeMap;
use schemars::JsonSchema;
use std::collections::BTreeMap;
use util::*;

// Ensure that schemars_derive uses the full path to std::string::String
Expand Down
2 changes: 1 addition & 1 deletion schemars/tests/expected/ipnetwork.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Ip",
"oneOf": [
{
Expand Down
2 changes: 1 addition & 1 deletion schemars/tests/expected/ipv4network.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "IpV4",
"type": "string",
"format": "ipv4"
Expand Down
2 changes: 1 addition & 1 deletion schemars/tests/expected/ipv6network.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "IpV6",
"type": "string",
"format": "ipv6"
Expand Down

0 comments on commit d59983e

Please sign in to comment.