Skip to content

Commit c6ee5c0

Browse files
committed
Fix PathTrie copy operations
1 parent 29f5410 commit c6ee5c0

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/utils/path_trie.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ PathTrie::PathTrie(const PathTrie& other)
1616
: root_(nullptr)
1717
{
1818
if (other.root_) {
19-
root_ = new Node(*other.root_);
2019
CopyNode(root_, other.root_);
20+
} else {
21+
root_ = new Node();
2122
}
2223
}
2324

@@ -27,11 +28,11 @@ PathTrie& PathTrie::operator=(const PathTrie& other)
2728
return *this;
2829

2930
DeleteNode(root_);
31+
root_ = nullptr;
3032
if (other.root_) {
31-
root_ = new Node(*other.root_);
3233
CopyNode(root_, other.root_);
3334
} else {
34-
root_ = nullptr;
35+
root_ = new Node();
3536
}
3637

3738
return *this;
@@ -156,6 +157,9 @@ PathTrie::~PathTrie()
156157
void PathTrie::DeleteNode(Node* node)
157158
{
158159
#ifndef LUA_OAS_VALIDATOR // LUA manages garbage collection itself
160+
if (!node) {
161+
return;
162+
}
159163
for (auto& pair : node->children) {
160164
DeleteNode(pair.second);
161165
}

test/unittest/src/utils/path_trie.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,32 @@ TEST_F(PathTrieTest, InsertAndSearchMultiParamPath)
6565
EXPECT_EQ(std::string(param_idxs[3].beg, param_idxs[3].end), "123");
6666
EXPECT_EQ(std::string(param_idxs[5].beg, param_idxs[5].end), "update");
6767
}
68+
69+
// Verify copy constructor correctly duplicates the trie
70+
TEST_F(PathTrieTest, CopyConstructor)
71+
{
72+
trie_.Insert("/api/data/{id}");
73+
PathTrie copied(trie_);
74+
std::string oas_path;
75+
std::unordered_map<size_t, ParamRange> param_idxs;
76+
std::string search_path = "/api/data/456";
77+
EXPECT_TRUE(copied.Search(search_path.data(), search_path.data() + search_path.size(), oas_path, param_idxs));
78+
EXPECT_EQ(oas_path, "/api/data/{id}");
79+
ASSERT_TRUE(param_idxs.find(3) != param_idxs.end());
80+
EXPECT_EQ(std::string(param_idxs[3].beg, param_idxs[3].end), "456");
81+
}
82+
83+
// Verify copy assignment correctly duplicates the trie
84+
TEST_F(PathTrieTest, CopyAssignment)
85+
{
86+
PathTrie other;
87+
other.Insert("/api/info/{name}");
88+
trie_ = other;
89+
std::string oas_path;
90+
std::unordered_map<size_t, ParamRange> param_idxs;
91+
std::string search_path = "/api/info/john";
92+
EXPECT_TRUE(trie_.Search(search_path.data(), search_path.data() + search_path.size(), oas_path, param_idxs));
93+
EXPECT_EQ(oas_path, "/api/info/{name}");
94+
ASSERT_TRUE(param_idxs.find(2) != param_idxs.end());
95+
EXPECT_EQ(std::string(param_idxs[2].beg, param_idxs[2].end), "john");
96+
}

0 commit comments

Comments
 (0)