fix(docgen): handle heterogeneous tuples

Fix template rendering for tuples (arrays with prefixItems) that contain
non-primitive types like $ref or anyOf, which were incorrectly detected
as homogeneous tuples.

Template Logic Fix:
- Check if `item.type` and `first_type.type` are both defined before comparing
- Treat items with $ref, anyOf, or missing type as heterogeneous
- Apply fix in all three locations: render_type_badge, render_array_items_inline,
  and render_array_items macros

Display Name Enhancement:
- Strip trailing digits from ALL type names, not just wrapper types
- Types like `DisplayFormat2` now display as `DisplayFormat`
- Only adds `_display_name` when it differs from original name
This commit is contained in:
LGUG2Z committed 2025-12-22 09:31:29 -08:00
1 parent 5d606f0d87
commit f32d94d929
3 files changed
+132 -8

No files matched your search

+105
View File
@@ -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"
);
}
}
+9 -5
View File
@@ -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(),
+18 -3
View File
@@ -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 -%}