Compare commits

..

2 Commits

Author SHA1 Message Date
90ad7cfe45 Bump version 2023-02-10 10:53:18 +01:00
235e32d29a Remove duplicate vertices 2023-02-10 10:52:50 +01:00
3 changed files with 19 additions and 6 deletions

2
Cargo.lock generated
View File

@@ -221,7 +221,7 @@ checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66"
[[package]] [[package]]
name = "openapi-parser" name = "openapi-parser"
version = "0.4.4" version = "0.4.5"
dependencies = [ dependencies = [
"clap", "clap",
"env_logger", "env_logger",

View File

@@ -4,7 +4,7 @@ description = "Extract schemas definitions tree from OpenAPI documents"
authors = ["Pierre Hubert <pierre.hubert.git@communiquons.org>"] authors = ["Pierre Hubert <pierre.hubert.git@communiquons.org>"]
license = "MIT" license = "MIT"
repository = "https://gitea.communiquons.org/pierre/OpenAPI-Parser" repository = "https://gitea.communiquons.org/pierre/OpenAPI-Parser"
version = "0.4.4" version = "0.4.5"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@@ -2,6 +2,7 @@ use clap::{Parser, Subcommand};
use openapi_parser::{ use openapi_parser::{
build_tree, parse_schema, parse_schema_fix_example_issue, NodeType, ObjectChild, TreeNode, build_tree, parse_schema, parse_schema_fix_example_issue, NodeType, ObjectChild, TreeNode,
}; };
use std::collections::HashSet;
use std::fmt::Write; use std::fmt::Write;
/// Dump the tree structure of a schema element as dot file /// 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<String>,
) {
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 { .. }) { if !parent_name.is_empty() && matches!(node.r#type, NodeType::Object { .. }) {
writeln!(out, "\"{}\" -> \"{}\";", parent_name, node.name).unwrap(); 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 } => { NodeType::Array { item } => {
let mut item = item.clone(); let mut item = item.clone();
item.name.push_str(" []"); item.name.push_str(" []");
recurse_export(&item, parent_name, out); recurse_export(&item, parent_name, out, already_processed);
} }
NodeType::Object { children, .. } => { NodeType::Object { children, .. } => {
for child in 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 { fn graphviz_export(tree: &TreeNode) -> String {
let mut out = "digraph G {\n".to_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.push_str("}\n");
out out