Skip to content

Commit e747a5d

Browse files
authored
Pass through f64 field descriptions (#974)
1 parent f65c6dd commit e747a5d

1 file changed

Lines changed: 82 additions & 4 deletions

File tree

typify-impl/src/convert.rs

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,10 +1165,9 @@ impl TypeSpace {
11651165
}
11661166
}
11671167

1168-
// TODO deal with metadata
11691168
fn convert_number<'a>(
11701169
&self,
1171-
_metadata: &'a Option<Box<Metadata>>,
1170+
metadata: &'a Option<Box<Metadata>>,
11721171
_validation: &Option<Box<schemars::schema::NumberValidation>>,
11731172
format: &Option<String>,
11741173
) -> Result<(TypeEntry, &'a Option<Box<Metadata>>)> {
@@ -1184,8 +1183,8 @@ impl TypeSpace {
11841183
*/
11851184

11861185
match format.as_deref() {
1187-
Some("float") => Ok((TypeEntry::new_float("f32"), &None)),
1188-
_ => Ok((TypeEntry::new_float("f64"), &None)),
1186+
Some("float") => Ok((TypeEntry::new_float("f32"), metadata)),
1187+
_ => Ok((TypeEntry::new_float("f64"), metadata)),
11891188
}
11901189
}
11911190

@@ -2309,4 +2308,83 @@ mod tests {
23092308
let expected = quote! { not::a::real::library::Uuid };
23102309
assert_eq!(actual.to_string(), expected.to_string());
23112310
}
2311+
2312+
#[test]
2313+
fn test_float_type_description() {
2314+
// Test that f64 types preserve their description
2315+
let schema_json = r#"
2316+
{
2317+
"description": "A floating point value",
2318+
"type": "number"
2319+
}
2320+
"#;
2321+
2322+
let schema: RootSchema = serde_json::from_str(schema_json).unwrap();
2323+
2324+
let mut type_space = TypeSpace::default();
2325+
let schema_obj = schemars::schema::Schema::Object(schema.schema.clone());
2326+
let (_, metadata) = type_space
2327+
.convert_schema_object(Name::Unknown, &schema_obj, &schema.schema)
2328+
.unwrap();
2329+
2330+
// Verify that metadata (including description) is preserved
2331+
assert!(metadata.is_some());
2332+
assert_eq!(
2333+
metadata.as_ref().and_then(|m| m.description.as_deref()),
2334+
Some("A floating point value")
2335+
);
2336+
}
2337+
2338+
#[test]
2339+
fn test_float32_type_description() {
2340+
// Test that f32 types preserve their description
2341+
let schema_json = r#"
2342+
{
2343+
"description": "A 32-bit float",
2344+
"type": "number",
2345+
"format": "float"
2346+
}
2347+
"#;
2348+
2349+
let schema: RootSchema = serde_json::from_str(schema_json).unwrap();
2350+
2351+
let mut type_space = TypeSpace::default();
2352+
let schema_obj = schemars::schema::Schema::Object(schema.schema.clone());
2353+
let (_, metadata) = type_space
2354+
.convert_schema_object(Name::Unknown, &schema_obj, &schema.schema)
2355+
.unwrap();
2356+
2357+
// Verify that metadata (including description) is preserved
2358+
assert!(metadata.is_some());
2359+
assert_eq!(
2360+
metadata.as_ref().and_then(|m| m.description.as_deref()),
2361+
Some("A 32-bit float")
2362+
);
2363+
}
2364+
2365+
#[test]
2366+
fn test_integer_type_description() {
2367+
// Test that integer types preserve their description (control test)
2368+
let schema_json = r#"
2369+
{
2370+
"description": "An integer value",
2371+
"type": "integer"
2372+
}
2373+
"#;
2374+
2375+
let schema: RootSchema = serde_json::from_str(schema_json).unwrap();
2376+
2377+
let mut type_space = TypeSpace::default();
2378+
let schema_obj = schemars::schema::Schema::Object(schema.schema.clone());
2379+
let (_, metadata) = type_space
2380+
.convert_schema_object(Name::Unknown, &schema_obj, &schema.schema)
2381+
.unwrap();
2382+
2383+
// Verify that metadata (including description) is preserved
2384+
assert!(metadata.is_some());
2385+
assert_eq!(
2386+
metadata.as_ref().and_then(|m| m.description.as_deref()),
2387+
Some("An integer value")
2388+
);
2389+
}
23122390
}

0 commit comments

Comments
 (0)