From 235e32d29adaf45464cc3b01a5ba41ba369f44b8 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Fri, 10 Feb 2023 10:52:50 +0100 Subject: [PATCH] Remove duplicate vertices --- src/main.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index c86ef01..0cd3ce7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use clap::{Parser, Subcommand}; use openapi_parser::{ build_tree, parse_schema, parse_schema_fix_example_issue, NodeType, ObjectChild, TreeNode, }; +use std::collections::HashSet; use std::fmt::Write; /// Dump the tree structure of a schema element as dot file @@ -78,7 +79,17 @@ fn main() { } } -fn recurse_export(node: &TreeNode, parent_name: &str, out: &mut String) { +fn recurse_export( + node: &TreeNode, + parent_name: &str, + out: &mut String, + already_processed: &mut HashSet, +) { + if already_processed.contains(&node.name) { + return; + } + already_processed.insert(node.name.to_string()); + if !parent_name.is_empty() && matches!(node.r#type, NodeType::Object { .. }) { writeln!(out, "\"{}\" -> \"{}\";", parent_name, node.name).unwrap(); } @@ -87,11 +98,11 @@ fn recurse_export(node: &TreeNode, parent_name: &str, out: &mut String) { NodeType::Array { item } => { let mut item = item.clone(); item.name.push_str(" []"); - recurse_export(&item, parent_name, out); + recurse_export(&item, parent_name, out, already_processed); } NodeType::Object { children, .. } => { for child in children { - recurse_export(&child.node, &node.name, out); + recurse_export(&child.node, &node.name, out, already_processed); } } @@ -102,7 +113,9 @@ fn recurse_export(node: &TreeNode, parent_name: &str, out: &mut String) { fn graphviz_export(tree: &TreeNode) -> String { let mut out = "digraph G {\n".to_string(); - recurse_export(tree, "", &mut out); + let mut already_processed = HashSet::new(); + + recurse_export(tree, "", &mut out, &mut already_processed); out.push_str("}\n"); out