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
14 changes: 7 additions & 7 deletions typify-impl/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ impl TypeSpace {
type_id,
validation,
original_schema.clone(),
),
)?,
metadata,
))
}
Expand Down Expand Up @@ -1062,7 +1062,7 @@ impl TypeSpace {
variants,
false,
original_schema.clone(),
);
)?;

if has_null {
ty = self.type_to_option(ty);
Expand Down Expand Up @@ -1435,7 +1435,7 @@ impl TypeSpace {
properties,
deny_unknown_fields,
original_schema.clone(),
),
)?,
&None,
))
}
Expand Down Expand Up @@ -1777,7 +1777,7 @@ impl TypeSpace {
type_id,
enum_values,
original_schema.clone(),
);
)?;

Ok((newtype_entry, metadata))
}
Expand Down Expand Up @@ -1836,7 +1836,7 @@ impl TypeSpace {
type_id,
enum_values,
original_schema.clone(),
);
)?;

Ok((newtype_entry, metadata))
}
Expand Down Expand Up @@ -1998,7 +1998,7 @@ impl TypeSpace {
vec![],
true,
schema.clone(),
);
)?;
Ok((ty, &None))
}

Expand Down Expand Up @@ -2031,7 +2031,7 @@ impl TypeSpace {
type_id,
enum_values,
original_schema.clone(),
);
)?;

Ok((
newtype_entry,
Expand Down
19 changes: 11 additions & 8 deletions typify-impl/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,16 @@ impl TypeSpace {
})
.collect::<Option<Vec<_>>>()?;

Some(TypeEntryEnum::from_metadata(
TypeEntryEnum::from_metadata(
self,
type_name,
enum_metadata,
EnumTagType::External,
variants,
deny_unknown_fields,
original_schema.clone(),
))
)
.ok()
}

/// Return the variant details and a bool indicating if the schema denies
Expand Down Expand Up @@ -357,15 +358,16 @@ impl TypeSpace {
.collect::<Result<Vec<_>>>()
.ok()?;

Some(TypeEntryEnum::from_metadata(
TypeEntryEnum::from_metadata(
self,
type_name,
metadata,
EnumTagType::Internal { tag: tag.clone() },
variants,
deny_unknown_fields,
original_schema.clone(),
))
)
.ok()
}

fn internal_variant(
Expand Down Expand Up @@ -483,15 +485,16 @@ impl TypeSpace {
.collect::<Result<Vec<_>>>()
.ok()?;

Some(TypeEntryEnum::from_metadata(
TypeEntryEnum::from_metadata(
self,
type_name,
metadata,
EnumTagType::Adjacent { tag, content },
variants,
deny_unknown_fields,
original_schema.clone(),
))
)
.ok()
}

fn adjacent_variant(
Expand Down Expand Up @@ -659,7 +662,7 @@ impl TypeSpace {
})
.collect();

Ok(TypeEntryEnum::from_metadata(
TypeEntryEnum::from_metadata(
self,
// TODO should this be tmp_type_name?
type_name,
Expand All @@ -668,7 +671,7 @@ impl TypeSpace {
variants,
deny_unknown_fields,
original_schema.clone(),
))
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions typify-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand All @@ -667,7 +667,7 @@ impl TypeSpace {
metadata,
subtype_id,
schema.clone(),
)
)?
}
};
// TODO need a type alias?
Expand Down
2 changes: 1 addition & 1 deletion typify-impl/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl TypeSpace {
properties,
false,
original_schema.clone(),
),
)?,
metadata,
))
}
Expand Down
29 changes: 14 additions & 15 deletions typify-impl/src/type_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down Expand Up @@ -95,7 +95,7 @@ impl TypeEntryEnum {
variants: Vec<Variant>,
deny_unknown_fields: bool,
schema: Schema,
) -> TypeEntry {
) -> Result<TypeEntry> {
// Let's find some decent names for variants. We first try the simple
// sanitization.
let mut ident_names = variants
Expand Down Expand Up @@ -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))
}
}

Expand All @@ -196,7 +196,7 @@ impl TypeEntryStruct {
properties: Vec<StructProperty<TypeId>>,
deny_unknown_fields: bool,
schema: Schema,
) -> TypeEntry {
) -> Result<TypeEntry> {
let name = get_type_name(&type_name, metadata).unwrap();
let description = metadata_description(metadata);
let default = metadata
Expand All @@ -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))
}
}

Expand All @@ -231,7 +231,7 @@ impl TypeEntryNewtype {
type_id: TypeId,
constraints: NewtypeConstraints,
schema: Schema,
) -> TypeEntry {
) -> Result<TypeEntry> {
let name = get_type_name(&type_name, metadata).unwrap();
let description = metadata_description(metadata);

Expand All @@ -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(
Expand All @@ -255,7 +254,7 @@ impl TypeEntryNewtype {
metadata: &Option<Box<Metadata>>,
type_id: TypeId,
schema: Schema,
) -> TypeEntry {
) -> Result<TypeEntry> {
Self::make(
type_space,
type_name,
Expand All @@ -273,7 +272,7 @@ impl TypeEntryNewtype {
type_id: TypeId,
enum_values: &[serde_json::Value],
schema: Schema,
) -> TypeEntry {
) -> Result<TypeEntry> {
let constraints = NewtypeConstraints::AllowList(
enum_values.iter().cloned().map(JsonValue::new).collect(),
);
Expand All @@ -294,7 +293,7 @@ impl TypeEntryNewtype {
type_id: TypeId,
enum_values: &[serde_json::Value],
schema: Schema,
) -> TypeEntry {
) -> Result<TypeEntry> {
let constraints =
NewtypeConstraints::DenyList(enum_values.iter().cloned().map(JsonValue::new).collect());
Self::make(
Expand All @@ -314,7 +313,7 @@ impl TypeEntryNewtype {
type_id: TypeId,
validation: &schemars::schema::StringValidation,
schema: Schema,
) -> TypeEntry {
) -> Result<TypeEntry> {
let schemars::schema::StringValidation {
max_length,
min_length,
Expand Down
29 changes: 29 additions & 0 deletions typify-impl/tests/test_build_errors.rs
Original file line number Diff line number Diff line change
@@ -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);
}
Loading