From 0ee86f994a6d272cf158d06d6244728a9fdd37cd Mon Sep 17 00:00:00 2001 From: "Adam H. Leventhal" Date: Sun, 15 Nov 2026 21:31:29 -0800 Subject: [PATCH 1/2] surface typespace build errors instead of panicking The from_metadata constructors in type_entry.rs return the error typespace reports for an invalid type (duplicate item names, invalid identifiers, and the like) so it reaches the caller as Error::Typespace rather than a panic inside the generator. --- typify-impl/src/convert.rs | 14 ++++++------- typify-impl/src/enums.rs | 19 ++++++++++------- typify-impl/src/lib.rs | 4 ++-- typify-impl/src/structs.rs | 2 +- typify-impl/src/type_entry.rs | 27 ++++++++++++------------ typify-impl/tests/test_build_errors.rs | 29 ++++++++++++++++++++++++++ 6 files changed, 63 insertions(+), 32 deletions(-) create mode 100644 typify-impl/tests/test_build_errors.rs diff --git a/typify-impl/src/convert.rs b/typify-impl/src/convert.rs index bcef446b..17c2f18a 100644 --- a/typify-impl/src/convert.rs +++ b/typify-impl/src/convert.rs @@ -989,7 +989,7 @@ impl TypeSpace { type_id, validation, original_schema.clone(), - ), + )?, metadata, )) } @@ -1062,7 +1062,7 @@ impl TypeSpace { variants, false, original_schema.clone(), - ); + )?; if has_null { ty = self.type_to_option(ty); @@ -1435,7 +1435,7 @@ impl TypeSpace { properties, deny_unknown_fields, original_schema.clone(), - ), + )?, &None, )) } @@ -1777,7 +1777,7 @@ impl TypeSpace { type_id, enum_values, original_schema.clone(), - ); + )?; Ok((newtype_entry, metadata)) } @@ -1836,7 +1836,7 @@ impl TypeSpace { type_id, enum_values, original_schema.clone(), - ); + )?; Ok((newtype_entry, metadata)) } @@ -1998,7 +1998,7 @@ impl TypeSpace { vec![], true, schema.clone(), - ); + )?; Ok((ty, &None)) } @@ -2031,7 +2031,7 @@ impl TypeSpace { type_id, enum_values, original_schema.clone(), - ); + )?; Ok(( newtype_entry, diff --git a/typify-impl/src/enums.rs b/typify-impl/src/enums.rs index accbc4ea..9f657d9b 100644 --- a/typify-impl/src/enums.rs +++ b/typify-impl/src/enums.rs @@ -224,7 +224,7 @@ impl TypeSpace { }) .collect::>>()?; - Some(TypeEntryEnum::from_metadata( + TypeEntryEnum::from_metadata( self, type_name, enum_metadata, @@ -232,7 +232,8 @@ impl TypeSpace { variants, deny_unknown_fields, original_schema.clone(), - )) + ) + .ok() } /// Return the variant details and a bool indicating if the schema denies @@ -357,7 +358,7 @@ impl TypeSpace { .collect::>>() .ok()?; - Some(TypeEntryEnum::from_metadata( + TypeEntryEnum::from_metadata( self, type_name, metadata, @@ -365,7 +366,8 @@ impl TypeSpace { variants, deny_unknown_fields, original_schema.clone(), - )) + ) + .ok() } fn internal_variant( @@ -483,7 +485,7 @@ impl TypeSpace { .collect::>>() .ok()?; - Some(TypeEntryEnum::from_metadata( + TypeEntryEnum::from_metadata( self, type_name, metadata, @@ -491,7 +493,8 @@ impl TypeSpace { variants, deny_unknown_fields, original_schema.clone(), - )) + ) + .ok() } fn adjacent_variant( @@ -659,7 +662,7 @@ impl TypeSpace { }) .collect(); - Ok(TypeEntryEnum::from_metadata( + TypeEntryEnum::from_metadata( self, // TODO should this be tmp_type_name? type_name, @@ -668,7 +671,7 @@ impl TypeSpace { variants, deny_unknown_fields, original_schema.clone(), - )) + ) } } diff --git a/typify-impl/src/lib.rs b/typify-impl/src/lib.rs index 6342f931..f8a10585 100644 --- a/typify-impl/src/lib.rs +++ b/typify-impl/src/lib.rs @@ -643,7 +643,7 @@ impl TypeSpace { metadata, type_id.clone(), schema.clone(), - ), + )?, TypeEntry::Type(typespace::build::Type::Native(native)) if native_name_match(native, &type_name) => @@ -667,7 +667,7 @@ impl TypeSpace { metadata, subtype_id, schema.clone(), - ) + )? } }; // TODO need a type alias? diff --git a/typify-impl/src/structs.rs b/typify-impl/src/structs.rs index 48521865..1713cae9 100644 --- a/typify-impl/src/structs.rs +++ b/typify-impl/src/structs.rs @@ -288,7 +288,7 @@ impl TypeSpace { properties, false, original_schema.clone(), - ), + )?, metadata, )) } diff --git a/typify-impl/src/type_entry.rs b/typify-impl/src/type_entry.rs index 5bc6f8e3..6b5a5ae8 100644 --- a/typify-impl/src/type_entry.rs +++ b/typify-impl/src/type_entry.rs @@ -95,7 +95,7 @@ impl TypeEntryEnum { variants: Vec, deny_unknown_fields: bool, schema: Schema, - ) -> TypeEntry { + ) -> crate::Result { // Let's find some decent names for variants. We first try the simple // sanitization. let mut ident_names = variants @@ -182,9 +182,9 @@ impl TypeEntryEnum { if deny_unknown_fields { type_enum = type_enum.deny_unknown_fields(); } - let typ = type_enum.build().unwrap(); + let typ = type_enum.build()?; - TypeEntry::Type(typ) + Ok(TypeEntry::Type(typ)) } } @@ -196,7 +196,7 @@ impl TypeEntryStruct { properties: Vec>, deny_unknown_fields: bool, schema: Schema, - ) -> TypeEntry { + ) -> crate::Result { let name = get_type_name(&type_name, metadata).unwrap(); let description = metadata_description(metadata); let default = metadata @@ -216,10 +216,10 @@ impl TypeEntryStruct { if deny_unknown_fields { type_struct = type_struct.deny_unknown_fields(); } - let mut typ = type_struct.build().unwrap(); + let mut typ = type_struct.build()?; typ.set_default(default); - TypeEntry::Type(typ) + Ok(TypeEntry::Type(typ)) } } @@ -231,7 +231,7 @@ impl TypeEntryNewtype { type_id: TypeId, constraints: NewtypeConstraints, schema: Schema, - ) -> TypeEntry { + ) -> crate::Result { let name = get_type_name(&type_name, metadata).unwrap(); let description = metadata_description(metadata); @@ -243,10 +243,9 @@ impl TypeEntryNewtype { .extra_derives(type_patch.derives) .extra_attrs(type_patch.attrs) .constraints(constraints) - .build() - .unwrap(); + .build()?; - TypeEntry::Type(typ) + Ok(TypeEntry::Type(typ)) } pub(crate) fn from_metadata( @@ -255,7 +254,7 @@ impl TypeEntryNewtype { metadata: &Option>, type_id: TypeId, schema: Schema, - ) -> TypeEntry { + ) -> crate::Result { Self::make( type_space, type_name, @@ -273,7 +272,7 @@ impl TypeEntryNewtype { type_id: TypeId, enum_values: &[serde_json::Value], schema: Schema, - ) -> TypeEntry { + ) -> crate::Result { let constraints = NewtypeConstraints::AllowList( enum_values.iter().cloned().map(JsonValue::new).collect(), ); @@ -294,7 +293,7 @@ impl TypeEntryNewtype { type_id: TypeId, enum_values: &[serde_json::Value], schema: Schema, - ) -> TypeEntry { + ) -> crate::Result { let constraints = NewtypeConstraints::DenyList(enum_values.iter().cloned().map(JsonValue::new).collect()); Self::make( @@ -314,7 +313,7 @@ impl TypeEntryNewtype { type_id: TypeId, validation: &schemars::schema::StringValidation, schema: Schema, - ) -> TypeEntry { + ) -> crate::Result { let schemars::schema::StringValidation { max_length, min_length, diff --git a/typify-impl/tests/test_build_errors.rs b/typify-impl/tests/test_build_errors.rs new file mode 100644 index 00000000..ce77e0ee --- /dev/null +++ b/typify-impl/tests/test_build_errors.rs @@ -0,0 +1,29 @@ +// Copyright 2026 Oxide Computer Company + +//! A type that typespace refuses to build is an error, not a panic. + +use schemars::schema::RootSchema; +use typify_impl::{Error, TypeSpace, TypeSpaceSettings}; + +/// Two properties that sanitize to the same Rust field name. +#[test] +fn test_duplicate_field_names() { + let schema: RootSchema = serde_json::from_value(serde_json::json!({ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Thing", + "type": "object", + "properties": { + "fooBar": { "type": "string" }, + "foo_bar": { "type": "integer" } + } + })) + .unwrap(); + + let mut type_space = TypeSpace::new(&TypeSpaceSettings::default()); + let result = type_space.add_type_with_name( + &schemars::schema::Schema::Object(schema.schema), + Some("Thing".to_string()), + ); + + assert!(matches!(result, Err(Error::Typespace(_))), "{:?}", result); +} From 323b7b1b59b6009b4b2e59537f4148be56cf68a0 Mon Sep 17 00:00:00 2001 From: "Adam H. Leventhal" Date: Sun, 15 Nov 2026 21:48:52 -0800 Subject: [PATCH 2/2] self-review --- typify-impl/src/type_entry.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/typify-impl/src/type_entry.rs b/typify-impl/src/type_entry.rs index 6b5a5ae8..d8280e2e 100644 --- a/typify-impl/src/type_entry.rs +++ b/typify-impl/src/type_entry.rs @@ -24,7 +24,7 @@ use schemars::schema::{Metadata, Schema}; use unicode_ident::is_xid_continue; use crate::{ - Case, Name, TypeId, TypeSpace, sanitize, + Case, Name, Result, TypeId, TypeSpace, sanitize, util::{TypePatch, get_type_name, metadata_description, unique}, }; @@ -95,7 +95,7 @@ impl TypeEntryEnum { variants: Vec, deny_unknown_fields: bool, schema: Schema, - ) -> crate::Result { + ) -> Result { // Let's find some decent names for variants. We first try the simple // sanitization. let mut ident_names = variants @@ -196,7 +196,7 @@ impl TypeEntryStruct { properties: Vec>, deny_unknown_fields: bool, schema: Schema, - ) -> crate::Result { + ) -> Result { let name = get_type_name(&type_name, metadata).unwrap(); let description = metadata_description(metadata); let default = metadata @@ -231,7 +231,7 @@ impl TypeEntryNewtype { type_id: TypeId, constraints: NewtypeConstraints, schema: Schema, - ) -> crate::Result { + ) -> Result { let name = get_type_name(&type_name, metadata).unwrap(); let description = metadata_description(metadata); @@ -254,7 +254,7 @@ impl TypeEntryNewtype { metadata: &Option>, type_id: TypeId, schema: Schema, - ) -> crate::Result { + ) -> Result { Self::make( type_space, type_name, @@ -272,7 +272,7 @@ impl TypeEntryNewtype { type_id: TypeId, enum_values: &[serde_json::Value], schema: Schema, - ) -> crate::Result { + ) -> Result { let constraints = NewtypeConstraints::AllowList( enum_values.iter().cloned().map(JsonValue::new).collect(), ); @@ -293,7 +293,7 @@ impl TypeEntryNewtype { type_id: TypeId, enum_values: &[serde_json::Value], schema: Schema, - ) -> crate::Result { + ) -> Result { let constraints = NewtypeConstraints::DenyList(enum_values.iter().cloned().map(JsonValue::new).collect()); Self::make( @@ -313,7 +313,7 @@ impl TypeEntryNewtype { type_id: TypeId, validation: &schemars::schema::StringValidation, schema: Schema, - ) -> crate::Result { + ) -> Result { let schemars::schema::StringValidation { max_length, min_length,