From 86d7fb67c5459ecc9a6388b23b507aabfdcda026 Mon Sep 17 00:00:00 2001 From: Bojun Feng Date: Fri, 31 Oct 2025 12:18:33 -0500 Subject: [PATCH 1/3] Fix invalid JSON output for 'show ip route json' commands Commands like 'show ip route bgp json' were producing invalid JSON with a namespace prefix (': '), breaking JSON parsers and automation. The issue occurred because protocol filters (bgp, static, etc.) triggered the namespace-prefix output path even when JSON was requested. On single-ASIC systems where namespace is empty, this resulted in ': ' being printed before the JSON output. Fixed by skipping the namespace-prefix path when JSON output is requested, allowing proper JSON processing and formatting. Signed-off-by: Bojun Feng --- show/bgp_common.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/show/bgp_common.py b/show/bgp_common.py index e9c0e12e8a..a7c34922e9 100644 --- a/show/bgp_common.py +++ b/show/bgp_common.py @@ -403,7 +403,7 @@ def show_routes(args, namespace, display, verbose, ipver): return # Multi-asic show ip route with additional parms are handled by going to FRR directly and get those outputs from each namespace - if found_other_parms: + if found_other_parms and not found_json: print("{}:".format(ns)) print(output) continue @@ -429,7 +429,7 @@ def show_routes(args, namespace, display, verbose, ipver): else: print_ip_routes(combined_route, filter_by_ip) else: - new_string = json.dumps(combined_route,sort_keys=True, indent=4) + new_string = json.dumps(combined_route, sort_keys=True, indent=4) print(new_string) ''' From d591e2f85b82ca77a1725a9f2ecade8065e2a1af Mon Sep 17 00:00:00 2001 From: Bojun Feng Date: Fri, 31 Oct 2025 14:00:23 -0500 Subject: [PATCH 2/3] Add test for 'show ip route bgp json' command Adds test_show_ip_route_bgp_json to verify protocol-filtered route commands with JSON output work correctly. Test validates: - Command executes successfully - Output is valid parseable JSON - No namespace prefix bug (': ' at start) - Output starts with '{' - BGP routes present in filtered output This test covers the bug fix in show/bgp_common.py where protocol filters (bgp, static, etc.) with JSON output were producing invalid JSON with namespace prefix on single-ASIC systems. Signed-off-by: Bojun Feng --- tests/ip_show_routes_test.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/ip_show_routes_test.py b/tests/ip_show_routes_test.py index f39ccc52d5..e86c28e355 100644 --- a/tests/ip_show_routes_test.py +++ b/tests/ip_show_routes_test.py @@ -1,3 +1,4 @@ +import json import os import pytest @@ -141,3 +142,31 @@ def test_show_ipv6_route_err( assert result.exit_code == 0 assert result.output == show_ip_route_common.show_ipv6_route_err_expected_output + "\n" + @pytest.mark.parametrize('setup_single_bgp_instance', + ['ip_route'], indirect=['setup_single_bgp_instance']) + def test_show_ip_route_bgp_json( + self, + setup_ip_route_commands, + setup_single_bgp_instance): + show = setup_ip_route_commands + runner = CliRunner() + result = runner.invoke( + show.cli.commands["ip"].commands["route"], ["bgp", "json"]) + print("{}".format(result.output)) + assert result.exit_code == 0 + + try: + parsed = json.loads(result.output) + assert isinstance(parsed, dict) + except json.JSONDecodeError as e: + pytest.fail(f"Output is not valid JSON: {e}") + + bgp_routes_found = False + for route_data in parsed.values(): + if isinstance(route_data, list): + for entry in route_data: + if entry.get("protocol") == "bgp": + bgp_routes_found = True + break + assert bgp_routes_found, "BGP routes should be present in filtered output" + From d483a64c8c1811fb934af2087d2c18cb579fcba2 Mon Sep 17 00:00:00 2001 From: Bojun Feng Date: Fri, 31 Oct 2025 14:30:36 -0500 Subject: [PATCH 3/3] misc: fix new line at end of file Signed-off-by: Bojun Feng --- tests/ip_show_routes_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/ip_show_routes_test.py b/tests/ip_show_routes_test.py index e86c28e355..2d2e1f825f 100644 --- a/tests/ip_show_routes_test.py +++ b/tests/ip_show_routes_test.py @@ -169,4 +169,3 @@ def test_show_ip_route_bgp_json( bgp_routes_found = True break assert bgp_routes_found, "BGP routes should be present in filtered output" -