diff --git a/src/lib.rs b/src/lib.rs index 0e6eb485..6577e203 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -380,4 +380,109 @@ mod tests { let props = schema.properties().unwrap(); assert!(props.contains_key("name")); } + + #[test] + fn test_tuple_with_ref_items() { + let schema: Schema = serde_json::from_str( + r##"{ + "type": "object", + "properties": { + "coords": { + "type": "array", + "prefixItems": [ + { "$ref": "#/$defs/X" }, + { "$ref": "#/$defs/Y" } + ] + } + }, + "$defs": { + "X": { "type": "number" }, + "Y": { "type": "number" } + } + }"##, + ) + .unwrap(); + + let result = generate_html(&schema, &Options::default()); + assert!(result.is_ok(), "Should handle $ref in prefixItems"); + } + + #[test] + fn test_tuple_with_mixed_items() { + let schema: Schema = serde_json::from_str( + r##"{ + "type": "object", + "properties": { + "pair": { + "type": "array", + "prefixItems": [ + { "$ref": "#/$defs/Name" }, + { "type": "integer" } + ] + } + }, + "$defs": { + "Name": { "type": "string" } + } + }"##, + ) + .unwrap(); + + let result = generate_html(&schema, &Options::default()); + assert!( + result.is_ok(), + "Should handle mixed $ref and type in prefixItems" + ); + } + + #[test] + fn test_tuple_with_anyof_items() { + let schema: Schema = serde_json::from_str( + r##"{ + "type": "object", + "properties": { + "optional_pair": { + "type": "array", + "prefixItems": [ + { "type": "string" }, + { "anyOf": [{ "type": "number" }, { "type": "null" }] } + ] + } + } + }"##, + ) + .unwrap(); + + let result = generate_html(&schema, &Options::default()); + assert!(result.is_ok(), "Should handle anyOf in prefixItems"); + } + + #[test] + fn test_homogeneous_tuple_still_works() { + let schema: Schema = serde_json::from_str( + r#"{ + "type": "object", + "properties": { + "point": { + "type": "array", + "prefixItems": [ + { "type": "number" }, + { "type": "number" }, + { "type": "number" } + ] + } + } + }"#, + ) + .unwrap(); + + let result = generate_html(&schema, &Options::default()); + assert!(result.is_ok()); + let html = result.unwrap(); + // Homogeneous tuples should show as "array float; 3" not "Tuple Items" + assert!( + html.contains("float; 3") || html.contains("number; 3"), + "Homogeneous tuple should display as [T; N] format" + ); + } } diff --git a/src/prepare.rs b/src/prepare.rs index 6ec80d2a..52dc26aa 100644 --- a/src/prepare.rs +++ b/src/prepare.rs @@ -27,15 +27,19 @@ pub fn prepare_for_template(value: &Value) -> Value { prepared } -/// Add display names to wrapper types by stripping numeric suffixes +/// Add display names to types by stripping numeric suffixes /// -/// For wrapper types like `PerAnimationPrefixConfig2`, this adds a -/// `_display_name` field with the base name `PerAnimationPrefixConfig`. +/// For types like `PerAnimationPrefixConfig2` or `DisplayFormat2`, this adds a +/// `_display_name` field with the base name (e.g., `PerAnimationPrefixConfig`). +/// This applies to all types with trailing digits, not just wrapper types. fn add_wrapper_display_names(schema: &mut Value) { if let Some(defs) = schema.get_mut("$defs").and_then(|v| v.as_object_mut()) { for (name, def) in defs.iter_mut() { - if def.get("_wrapper_type") == Some(&Value::Bool(true)) { - let display_name = name.trim_end_matches(|c: char| c.is_ascii_digit()); + // Strip trailing digits from ALL type names (not just wrapper types) + let display_name = name.trim_end_matches(|c: char| c.is_ascii_digit()); + + // Only add _display_name if it differs from the original name + if display_name != name { if let Some(obj) = def.as_object_mut() { obj.insert( "_display_name".to_string(), diff --git a/src/templates/macros.html b/src/templates/macros.html index a265591d..3cf7c354 100644 --- a/src/templates/macros.html +++ b/src/templates/macros.html @@ -56,7 +56,12 @@ {%- set first_type = node.prefixItems | first -%} {%- set_global is_homogeneous = true -%} {%- for item in node.prefixItems -%} - {%- if item.type != first_type.type -%} + {%- if item.type is defined and first_type.type is defined -%} + {%- if item.type != first_type.type -%} + {%- set_global is_homogeneous = false -%} + {%- endif -%} + {%- else -%} + {# If either lacks a direct type (uses $ref, anyOf, etc.), not homogeneous #} {%- set_global is_homogeneous = false -%} {%- endif -%} {%- endfor -%} @@ -160,7 +165,12 @@ {%- set first_type = node.prefixItems | first -%} {%- set_global is_homogeneous = true -%} {%- for item in node.prefixItems -%} - {%- if item.type != first_type.type -%} + {%- if item.type is defined and first_type.type is defined -%} + {%- if item.type != first_type.type -%} + {%- set_global is_homogeneous = false -%} + {%- endif -%} + {%- else -%} + {# If either lacks a direct type (uses $ref, anyOf, etc.), not homogeneous #} {%- set_global is_homogeneous = false -%} {%- endif -%} {%- endfor -%} @@ -238,7 +248,12 @@ {%- set first_type = node.prefixItems | first -%} {%- set_global is_homogeneous = true -%} {%- for item in node.prefixItems -%} - {%- if item.type != first_type.type -%} + {%- if item.type is defined and first_type.type is defined -%} + {%- if item.type != first_type.type -%} + {%- set_global is_homogeneous = false -%} + {%- endif -%} + {%- else -%} + {# If either lacks a direct type (uses $ref, anyOf, etc.), not homogeneous #} {%- set_global is_homogeneous = false -%} {%- endif -%} {%- endfor -%}