Skip to content

Commit 49b46fb

Browse files
authored
handle empty enum values (#842)
1 parent aea4f5a commit 49b46fb

3 files changed

Lines changed: 79 additions & 9 deletions

File tree

typify-impl/src/convert.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -942,10 +942,7 @@ impl TypeSpace {
942942
if has_null {
943943
self.convert_null(metadata)
944944
} else {
945-
Err(Error::InvalidSchema {
946-
type_name: type_name.into_option(),
947-
reason: "empty enum array".to_string(),
948-
})
945+
self.convert_never(type_name, original_schema)
949946
}
950947
} else {
951948
let mut ty = TypeEntryEnum::from_metadata(
@@ -1948,9 +1945,12 @@ impl TypeSpace {
19481945
metadata: &'a Option<Box<Metadata>>,
19491946
enum_values: &[serde_json::Value],
19501947
) -> Result<(TypeEntry, &'a Option<Box<Metadata>>)> {
1948+
if enum_values.is_empty() {
1949+
return self.convert_never(type_name, original_schema);
1950+
}
1951+
19511952
// We're here because the schema didn't have a type; that's a bummer,
19521953
// but we'll do our best to roll with the punches.
1953-
assert!(!enum_values.is_empty());
19541954

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

1979-
let (type_entry, metadata) =
1980-
self.convert_unknown_enum(type_name, original_schema, metadata, &enum_values)?;
1981-
let type_entry = self.type_to_option(type_entry);
1982-
Ok((type_entry, metadata))
1979+
if enum_values.is_empty() {
1980+
self.convert_null(metadata)
1981+
} else {
1982+
let (type_entry, metadata) =
1983+
self.convert_unknown_enum(type_name, original_schema, metadata, &enum_values)?;
1984+
let type_entry = self.type_to_option(type_entry);
1985+
Ok((type_entry, metadata))
1986+
}
19831987
} else {
19841988
match (instance_types.len(), instance_types.iter().next()) {
19851989
(1, Some(InstanceType::String)) => self.convert_enum_string(

typify/tests/schemas/various-enums.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@
166166
"NeverEver": {
167167
"not": true
168168
},
169+
"AnyOfNothing": {
170+
"enum": []
171+
},
172+
"AnyOfNoStrings": {
173+
"type": "string",
174+
"enum": []
175+
},
169176
"ShouldBeExclusive": {
170177
"type": "object",
171178
"properties": {

typify/tests/schemas/various-enums.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,65 @@ impl ::std::default::Default for AlternativeEnum {
110110
AlternativeEnum::Choice2
111111
}
112112
}
113+
#[doc = "`AnyOfNoStrings`"]
114+
#[doc = r""]
115+
#[doc = r" <details><summary>JSON schema</summary>"]
116+
#[doc = r""]
117+
#[doc = r" ```json"]
118+
#[doc = "{"]
119+
#[doc = " \"type\": \"string\","]
120+
#[doc = " \"enum\": []"]
121+
#[doc = "}"]
122+
#[doc = r" ```"]
123+
#[doc = r" </details>"]
124+
#[derive(
125+
:: serde :: Deserialize,
126+
:: serde :: Serialize,
127+
Clone,
128+
Copy,
129+
Debug,
130+
Eq,
131+
Hash,
132+
Ord,
133+
PartialEq,
134+
PartialOrd,
135+
)]
136+
#[serde(deny_unknown_fields)]
137+
pub enum AnyOfNoStrings {}
138+
impl ::std::convert::From<&Self> for AnyOfNoStrings {
139+
fn from(value: &AnyOfNoStrings) -> Self {
140+
value.clone()
141+
}
142+
}
143+
#[doc = "`AnyOfNothing`"]
144+
#[doc = r""]
145+
#[doc = r" <details><summary>JSON schema</summary>"]
146+
#[doc = r""]
147+
#[doc = r" ```json"]
148+
#[doc = "{"]
149+
#[doc = " \"enum\": []"]
150+
#[doc = "}"]
151+
#[doc = r" ```"]
152+
#[doc = r" </details>"]
153+
#[derive(
154+
:: serde :: Deserialize,
155+
:: serde :: Serialize,
156+
Clone,
157+
Copy,
158+
Debug,
159+
Eq,
160+
Hash,
161+
Ord,
162+
PartialEq,
163+
PartialOrd,
164+
)]
165+
#[serde(deny_unknown_fields)]
166+
pub enum AnyOfNothing {}
167+
impl ::std::convert::From<&Self> for AnyOfNothing {
168+
fn from(value: &AnyOfNothing) -> Self {
169+
value.clone()
170+
}
171+
}
113172
#[doc = "`CommentedVariants`"]
114173
#[doc = r""]
115174
#[doc = r" <details><summary>JSON schema</summary>"]

0 commit comments

Comments
 (0)