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

Fix schema for IpAddr type #277

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 27 additions & 1 deletion schemars/src/json_schema_impls/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,33 @@ simple_impl!(PathBuf => String);

simple_impl!(Ipv4Addr => String, "ipv4");
simple_impl!(Ipv6Addr => String, "ipv6");
simple_impl!(IpAddr => String, "ip");

impl JsonSchema for IpAddr {
no_ref_schema!();

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

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed("IpAddr")
}

fn json_schema(gen: &mut SchemaGenerator) -> Schema {
let schema_object = SchemaObject {
instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::String))),
subschemas: Some(Box::new(SubschemaValidation {
any_of: Some(vec![
gen.subschema_for::<Ipv4Addr>(),
gen.subschema_for::<Ipv6Addr>(),
]),
Comment on lines +73 to +76
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this be a oneOf?

Suggested change
any_of: Some(vec![
gen.subschema_for::<Ipv4Addr>(),
gen.subschema_for::<Ipv6Addr>(),
]),
one_of: Some(vec![
gen.subschema_for::<Ipv4Addr>(),
gen.subschema_for::<Ipv6Addr>(),
]),

..Default::default()
})),
..Default::default()
};
Comment on lines +70 to +80
Copy link
Contributor

Choose a reason for hiding this comment

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

You might consider this to label the subschemas; this makes it easier for code generators to infer what's going on.

Suggested change
let schema_object = SchemaObject {
instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::String))),
subschemas: Some(Box::new(SubschemaValidation {
any_of: Some(vec![
gen.subschema_for::<Ipv4Addr>(),
gen.subschema_for::<Ipv6Addr>(),
]),
..Default::default()
})),
..Default::default()
};
let schema_object = SchemaObject {
instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::String))),
subschemas: Some(Box::new(SubschemaValidation {
any_of: Some(vec![
SchemaObject {
metadata: Some(Box::new(Metadata {
title: Some("V4".into()),
..Default::default()
})),
subschemas: Some(Box::new(SubschemaValidation {
all_of: Some(vec![gen.subschema_for::<Ipv4Addr>()]),
..Default::default()
})),
..Default::default()
}
.into(),
SchemaObject {
metadata: Some(Box::new(Metadata {
title: Some("V6".into()),
..Default::default()
})),
subschemas: Some(Box::new(SubschemaValidation {
all_of: Some(vec![gen.subschema_for::<Ipv6Addr>()]),
..Default::default()
})),
..Default::default()
}
.into(),
]),
..Default::default()
})),
..Default::default()
};

Schema::Object(schema_object)
}
}

simple_impl!(SocketAddr => String);
simple_impl!(SocketAddrV4 => String);
Expand Down
Loading