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

Fix schema for IpAddr type #277

wants to merge 1 commit into from

Conversation

Soneji
Copy link

@Soneji Soneji commented Mar 28, 2024

The IpAddr type returns

"format": "ip"

which isn't a valid JSON format type. See here for the correct implementation.

I have changed this type to instead use

"anyOf": [
  {
    "type": "string",
    "format": "ipv4"
  },
  {
    "type": "string",
    "format": "ipv6"
  }
]

Copy link
Contributor

@ahl ahl left a comment

Choose a reason for hiding this comment

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

I'd suggest this not merge. From the JSON Schema draft 7 spec:

https://json-schema.org/draft-07/draft-handrews-json-schema-validation-01#rfc.section.7.2

Implementations MAY add custom format attributes. Save for agreement between parties, schema authors SHALL NOT expect a peer implementation to support this keyword and/or custom format attributes.

Is there a reason the "ip" format is causing issues for the author? We've found "ip" to be useful.

Comment on lines +73 to +76
any_of: Some(vec![
gen.subschema_for::<Ipv4Addr>(),
gen.subschema_for::<Ipv6Addr>(),
]),
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>(),
]),

Comment on lines +70 to +80
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()
};
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()
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants