Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MF-5759 fix unstable route order for routes with same name but different versions #345

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions stone/frontend/ir_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1704,10 +1704,6 @@ def _filter_namespaces_by_route_whitelist(self):
namespace.route_by_name = {}
namespace.routes_by_name = {}

# We need a stable sort in order to keep the resultant output
# the same across runs.
routes.sort()

for route in routes:
namespace.add_route(route)

Expand Down
8 changes: 4 additions & 4 deletions stone/ir/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,9 @@ def get_namespaces_imported_by_route_io(self):
def normalize(self):
# type: () -> None
"""
Alphabetizes routes to make route declaration order irrelevant.
Sorts routes to make route declaration order irrelevant.
"""
self.routes.sort(key=lambda route: route.name)
self.routes.sort()
self.data_types.sort(key=lambda data_type: data_type.name)
self.aliases.sort(key=lambda alias: alias.name)
self.annotations.sort(key=lambda annotation: annotation.name)
Expand Down Expand Up @@ -411,9 +411,9 @@ def _compare(self, lhs, rhs):
if not isinstance(rhs, ApiRoute):
raise TypeError("Expected ApiRoute for object: {}".format(rhs))

if lhs.name < rhs.name or lhs.version < rhs.version:
if (lhs.name, lhs.version) < (rhs.name, rhs.version):
return -1
elif lhs.name > rhs.name or lhs.version > rhs.version:
elif (lhs.name, lhs.version) > (rhs.name, rhs.version):
return 1
else:
return 0
Expand Down
23 changes: 23 additions & 0 deletions test/test_api_route.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import unittest

from stone.ir import ApiRoute


class TestApiRoute(unittest.TestCase):
def test_stable_sort(self):
"""
Tests API Route sorts according to name and then version
"""
routes = [
ApiRoute("B", 1, None),
ApiRoute("A", 2, None),
ApiRoute("A", 1, None),
ApiRoute("B", 2, None),
]

expected = [("A", 1), ("A", 2), ("B", 1), ("B", 2)]
sorted_routes = list(map(lambda x: (x.name, x.version), sorted(routes)))
self.assertEqual(sorted_routes, expected)

reversed_sorted_routes = list(map(lambda x: (x.name, x.version), sorted(reversed(routes))))
self.assertEqual(reversed_sorted_routes, expected)
Loading