Skip to content

Commit

Permalink
Handle unsatisfiable schemas by generating an empty enum (#419)
Browse files Browse the repository at this point in the history
 i.e. a type that can never be instantiated
  • Loading branch information
ahl authored Oct 6, 2023
1 parent 5e4af2b commit 259e75d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
18 changes: 16 additions & 2 deletions typify-impl/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ impl TypeSpace {
}

Schema::Bool(true) => self.convert_permissive(&None),
// TODO Not sure what to do here... need to return something toxic?
Schema::Bool(false) => todo!(),
Schema::Bool(false) => self.convert_never(type_name),
}
}

Expand Down Expand Up @@ -1537,6 +1536,21 @@ impl TypeSpace {
Ok((TypeEntryDetails::JsonValue.into(), metadata))
}

fn convert_never<'a>(
&mut self,
type_name: Name,
) -> Result<(TypeEntry, &'a Option<Box<Metadata>>)> {
let ty = TypeEntryEnum::from_metadata(
self,
type_name,
&None,
EnumTagType::External,
vec![],
true,
);
Ok((ty, &None))
}

fn convert_typed_enum<'a>(
&mut self,
type_name: Name,
Expand Down
1 change: 1 addition & 0 deletions typify-impl/src/type_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ impl TypeEntryEnum {
self.bespoke_impls = [
// Not untagged with all simple variants.
(self.tag_type != EnumTagType::Untagged
&& !self.variants.is_empty()
&& self
.variants
.iter()
Expand Down
10 changes: 8 additions & 2 deletions typify/tests/schemas/various-enums.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@
"properties": {
"prop": {
"type": "object",
"enum": [{}]
"enum": [
{}
]
}
}
},
Expand Down Expand Up @@ -159,6 +161,10 @@
},
"ReferenceDef": {
"type": "string"
},
"Never": false,
"NeverEver": {
"not": true
}
}
}
}
16 changes: 16 additions & 0 deletions typify/tests/schemas/various-enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,22 @@ impl From<std::collections::HashMap<String, i64>> for JankNames {
Self::Variant2(value)
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(deny_unknown_fields)]
pub enum Never {}
impl From<&Never> for Never {
fn from(value: &Never) -> Self {
value.clone()
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(deny_unknown_fields)]
pub enum NeverEver {}
impl From<&NeverEver> for NeverEver {
fn from(value: &NeverEver) -> Self {
value.clone()
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct NullStringEnumWithUnknownFormat(pub Option<NullStringEnumWithUnknownFormatInner>);
impl std::ops::Deref for NullStringEnumWithUnknownFormat {
Expand Down

0 comments on commit 259e75d

Please sign in to comment.