Skip to content
Merged
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
22 changes: 13 additions & 9 deletions typify-impl/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -942,10 +942,7 @@ impl TypeSpace {
if has_null {
self.convert_null(metadata)
} else {
Err(Error::InvalidSchema {
type_name: type_name.into_option(),
reason: "empty enum array".to_string(),
})
self.convert_never(type_name, original_schema)
}
} else {
let mut ty = TypeEntryEnum::from_metadata(
Expand Down Expand Up @@ -1948,9 +1945,12 @@ impl TypeSpace {
metadata: &'a Option<Box<Metadata>>,
enum_values: &[serde_json::Value],
) -> Result<(TypeEntry, &'a Option<Box<Metadata>>)> {
if enum_values.is_empty() {
return self.convert_never(type_name, original_schema);
}

// We're here because the schema didn't have a type; that's a bummer,
// but we'll do our best to roll with the punches.
assert!(!enum_values.is_empty());

// Let's hope all these values are the same type.
let mut instance_types = enum_values
Expand All @@ -1976,10 +1976,14 @@ impl TypeSpace {
.cloned()
.collect::<Vec<_>>();

let (type_entry, metadata) =
self.convert_unknown_enum(type_name, original_schema, metadata, &enum_values)?;
let type_entry = self.type_to_option(type_entry);
Ok((type_entry, metadata))
if enum_values.is_empty() {
self.convert_null(metadata)
} else {
let (type_entry, metadata) =
self.convert_unknown_enum(type_name, original_schema, metadata, &enum_values)?;
let type_entry = self.type_to_option(type_entry);
Ok((type_entry, metadata))
}
} else {
match (instance_types.len(), instance_types.iter().next()) {
(1, Some(InstanceType::String)) => self.convert_enum_string(
Expand Down
7 changes: 7 additions & 0 deletions typify/tests/schemas/various-enums.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@
"NeverEver": {
"not": true
},
"AnyOfNothing": {
"enum": []
},
"AnyOfNoStrings": {
"type": "string",
"enum": []
},
"ShouldBeExclusive": {
"type": "object",
"properties": {
Expand Down
59 changes: 59 additions & 0 deletions typify/tests/schemas/various-enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,65 @@ impl ::std::default::Default for AlternativeEnum {
AlternativeEnum::Choice2
}
}
#[doc = "`AnyOfNoStrings`"]
#[doc = r""]
#[doc = r" <details><summary>JSON schema</summary>"]
#[doc = r""]
#[doc = r" ```json"]
#[doc = "{"]
#[doc = " \"type\": \"string\","]
#[doc = " \"enum\": []"]
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(
:: serde :: Deserialize,
:: serde :: Serialize,
Clone,
Copy,
Debug,
Eq,
Hash,
Ord,
PartialEq,
PartialOrd,
)]
#[serde(deny_unknown_fields)]
pub enum AnyOfNoStrings {}
impl ::std::convert::From<&Self> for AnyOfNoStrings {
fn from(value: &AnyOfNoStrings) -> Self {
value.clone()
}
}
#[doc = "`AnyOfNothing`"]
#[doc = r""]
#[doc = r" <details><summary>JSON schema</summary>"]
#[doc = r""]
#[doc = r" ```json"]
#[doc = "{"]
#[doc = " \"enum\": []"]
#[doc = "}"]
#[doc = r" ```"]
#[doc = r" </details>"]
#[derive(
:: serde :: Deserialize,
:: serde :: Serialize,
Clone,
Copy,
Debug,
Eq,
Hash,
Ord,
PartialEq,
PartialOrd,
)]
#[serde(deny_unknown_fields)]
pub enum AnyOfNothing {}
impl ::std::convert::From<&Self> for AnyOfNothing {
fn from(value: &AnyOfNothing) -> Self {
value.clone()
}
}
#[doc = "`CommentedVariants`"]
#[doc = r""]
#[doc = r" <details><summary>JSON schema</summary>"]
Expand Down