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

Add ipnetwork support #245

Closed
wants to merge 8 commits into from
Closed
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [0.8.16] - 2023-11-11

### Fixed:

- Reduce size of MIR output (and improve release-mode compile time) when deriving `JsonSchema`

## [0.8.15] - 2023-09-17

### Added:
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

Generate JSON Schema documents from Rust code

> ⚠️ Warning
>
> This is a fork of the official [schemars repository](https://github.com/GREsau/schemars) which only exists until [this PR](https://github.com/GREsau/schemars/pull/250) is merged.
> It is published on crates.io with the name [apistos_schemars](https://crates.io/crates/apistos-schemars) and [apistos_schemars_derive](https://crates.io/crates/apistos-schemars-derive)
> This fork is based on schemars master and rebased from time to time.

## Basic Usage

If you don't really care about the specifics, the easiest way to generate a JSON schema for your types is to `#[derive(JsonSchema)]` and use the `schema_for!` macro. All fields of the type must also implement `JsonSchema` - Schemars implements this for many standard library types.
Expand Down Expand Up @@ -260,6 +266,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 docs/4-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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
9 changes: 7 additions & 2 deletions schemars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "schemars"
description = "Generate JSON Schemas from Rust code"
homepage = "https://graham.cool/schemars/"
repository = "https://github.com/GREsau/schemars"
version = "0.8.15"
version = "0.8.16"
authors = ["Graham Esau <[email protected]>"]
edition = "2021"
license = "MIT"
Expand All @@ -14,14 +14,15 @@ build = "build.rs"
rust-version = "1.60"

[dependencies]
schemars_derive = { version = "=0.8.15", optional = true, path = "../schemars_derive" }
schemars_derive = { version = "=0.8.16", optional = true, path = "../schemars_derive" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.25"
dyn-clone = "1.0"

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" }
ipnetwork = { version = "0.20", optional = true }
either = { version = "1.3", default-features = false, optional = true }
uuid08 = { version = "0.8", default-features = false, optional = true, package = "uuid" }
uuid1 = { version = "1.0", default-features = false, optional = true, package = "uuid" }
Expand Down Expand Up @@ -79,6 +80,10 @@ required-features = ["indexmap"]
name = "indexmap2"
required-features = ["indexmap2"]

[[test]]
name = "ipnetwork"
required-features = ["ipnetwork"]

[[test]]
name = "either"
required-features = ["either"]
Expand Down
63 changes: 60 additions & 3 deletions schemars/src/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,66 @@ impl Schema {
} else if is_null_type(&other) {
return self;
}
let s1: SchemaObject = self.into();
let s2: SchemaObject = other.into();
Schema::Object(s1.merge(s2))
let mut s1: SchemaObject = self.into();
let mut s2: SchemaObject = other.into();

let s1_subschemas = &mut s1.subschemas;
let s1_all_of = s1_subschemas.as_mut().and_then(|sub_sch| sub_sch.all_of.as_mut());
let s2_subschemas = &mut s2.subschemas;
let s2_all_of = s2_subschemas.as_mut().and_then(|sub_sch| sub_sch.all_of.as_mut());

match (s1_all_of, s2_all_of) {
(Some(s1_all_of), Some(s2_all_of)) => {
let mut s1_all_of = std::mem::take(s1_all_of);
s1_all_of.append(s2_all_of);
Schema::Object(SchemaObject {
subschemas: Some(Box::new(SubschemaValidation {
all_of: Some(s1_all_of),
..Default::default()
})),
..Default::default()
})
}
(Some(s1_all_of), None) => {
let mut s1_all_of = std::mem::take(s1_all_of);
s1_all_of.push(Schema::Object(s2));
Schema::Object(SchemaObject {
subschemas: Some(Box::new(SubschemaValidation {
all_of: Some(s1_all_of),
..Default::default()
})),
..Default::default()
})
}
(None, Some(s2_all_of)) => {
let mut s2_all_of = std::mem::take(s2_all_of);
s2_all_of.push(Schema::Object(s1));
Schema::Object(SchemaObject {
subschemas: Some(Box::new(SubschemaValidation {
all_of: Some(s2_all_of),
..Default::default()
})),
..Default::default()
})
}
(None, None) => {
match (s1_subschemas.is_some(), s2_subschemas.is_some()) {
(true, true) => Schema::Object(SchemaObject {
subschemas: Some(Box::new(SubschemaValidation {
all_of: Some(vec![
Schema::Object(s1),
Schema::Object(s2),
]),
..Default::default()
})),
..Default::default()
}),
(true, false) |
(false, true) |
(false, false) => Schema::Object(s1.merge(s2)),
}
}
}
}
}

Expand Down
60 changes: 60 additions & 0 deletions schemars/src/json_schema_impls/ipnetwork.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use ipnetwork::{IpNetwork, Ipv4Network, Ipv6Network};
use crate::gen::SchemaGenerator;
use crate::JsonSchema;
use crate::schema::{InstanceType, Schema, SchemaObject, SubschemaValidation};

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()
}
}

impl JsonSchema for Ipv4Network {
no_ref_schema!();

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

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

impl JsonSchema for Ipv6Network {
no_ref_schema!();

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

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::String.into()),
format: Some("ipv6".to_string()),
..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 @@ -66,6 +66,8 @@ mod ffi;
mod indexmap;
#[cfg(feature = "indexmap2")]
mod indexmap2;
#[cfg(feature = "ipnetwork")]
mod ipnetwork;
mod maps;
mod nonzero_signed;
mod nonzero_unsigned;
Expand Down
14 changes: 14 additions & 0 deletions schemars/tests/expected/ipnetwork.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Ip",
"oneOf": [
{
"type": "string",
"format": "ipv4"
},
{
"type": "string",
"format": "ipv6"
}
]
}
6 changes: 6 additions & 0 deletions schemars/tests/expected/ipv4network.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "IpV4",
"type": "string",
"format": "ipv4"
}
6 changes: 6 additions & 0 deletions schemars/tests/expected/ipv6network.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "IpV6",
"type": "string",
"format": "ipv6"
}
75 changes: 75 additions & 0 deletions schemars/tests/expected/multiple_enum_flatten.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Flat",
"allOf": [
{
"type": "object",
"oneOf": [
{
"type": "object",
"required": [
"B"
],
"properties": {
"B": {
"type": "boolean"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"S"
],
"properties": {
"S": {
"type": "string"
}
},
"additionalProperties": false
}
],
"required": [
"f"
],
"properties": {
"f": {
"type": "number",
"format": "float"
}
}
},
{
"oneOf": [
{
"type": "object",
"required": [
"U"
],
"properties": {
"U": {
"type": "integer",
"format": "uint32",
"minimum": 0.0
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"F"
],
"properties": {
"F": {
"type": "number",
"format": "double"
}
},
"additionalProperties": false
}
]
}
]
}
17 changes: 17 additions & 0 deletions schemars/tests/ipnetwork.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
mod util;
use util::*;

#[test]
fn ipv4network() -> TestResult {
test_default_generated_schema::<ipnetwork::Ipv4Network>("ipv4network")
}

#[test]
fn ipv6network() -> TestResult {
test_default_generated_schema::<ipnetwork::Ipv6Network>("ipv6network")
}

#[test]
fn ipnetwork() -> TestResult {
test_default_generated_schema::<ipnetwork::IpNetwork>("ipnetwork")
}
33 changes: 33 additions & 0 deletions schemars/tests/multiple_enum_flatten.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
mod util;
use schemars::JsonSchema;
use util::*;

#[allow(dead_code)]
#[derive(JsonSchema)]
#[schemars(rename = "Flat")]
struct Flat {
f: f32,
#[schemars(flatten)]
e1: Enum1,
#[schemars(flatten)]
e2: Enum2,
}

#[allow(dead_code)]
#[derive(JsonSchema)]
enum Enum1 {
B(bool),
S(String),
}

#[allow(dead_code)]
#[derive(JsonSchema)]
enum Enum2 {
U(u32),
F(f64)
}

#[test]
fn test_flat_schema() -> TestResult {
test_default_generated_schema::<Flat>("multiple_enum_flatten")
}
2 changes: 1 addition & 1 deletion schemars_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "schemars_derive"
description = "Macros for #[derive(JsonSchema)], for use with schemars"
homepage = "https://graham.cool/schemars/"
repository = "https://github.com/GREsau/schemars"
version = "0.8.15"
version = "0.8.16"
authors = ["Graham Esau <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down
Loading
Loading