Skip to content

Commit c225725

Browse files
authored
Fix invalid JSON output for 'show ip route <protocol> json' commands (sonic-net#4111)
* Fix invalid JSON output for 'show ip route <protocol> 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 <davidfeng@uchicago.edu> * 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 <davidfeng@uchicago.edu> * misc: fix new line at end of file Signed-off-by: Bojun Feng <davidfeng@uchicago.edu> --------- Signed-off-by: Bojun Feng <davidfeng@uchicago.edu>
1 parent 4e9080d commit c225725

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

show/bgp_common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ def show_routes(args, namespace, display, verbose, ipver):
403403
return
404404

405405
# Multi-asic show ip route with additional parms are handled by going to FRR directly and get those outputs from each namespace
406-
if found_other_parms:
406+
if found_other_parms and not found_json:
407407
print("{}:".format(ns))
408408
print(output)
409409
continue
@@ -429,7 +429,7 @@ def show_routes(args, namespace, display, verbose, ipver):
429429
else:
430430
print_ip_routes(combined_route, filter_by_ip)
431431
else:
432-
new_string = json.dumps(combined_route,sort_keys=True, indent=4)
432+
new_string = json.dumps(combined_route, sort_keys=True, indent=4)
433433
print(new_string)
434434

435435
'''

tests/ip_show_routes_test.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23
import pytest
34

@@ -141,3 +142,30 @@ def test_show_ipv6_route_err(
141142
assert result.exit_code == 0
142143
assert result.output == show_ip_route_common.show_ipv6_route_err_expected_output + "\n"
143144

145+
@pytest.mark.parametrize('setup_single_bgp_instance',
146+
['ip_route'], indirect=['setup_single_bgp_instance'])
147+
def test_show_ip_route_bgp_json(
148+
self,
149+
setup_ip_route_commands,
150+
setup_single_bgp_instance):
151+
show = setup_ip_route_commands
152+
runner = CliRunner()
153+
result = runner.invoke(
154+
show.cli.commands["ip"].commands["route"], ["bgp", "json"])
155+
print("{}".format(result.output))
156+
assert result.exit_code == 0
157+
158+
try:
159+
parsed = json.loads(result.output)
160+
assert isinstance(parsed, dict)
161+
except json.JSONDecodeError as e:
162+
pytest.fail(f"Output is not valid JSON: {e}")
163+
164+
bgp_routes_found = False
165+
for route_data in parsed.values():
166+
if isinstance(route_data, list):
167+
for entry in route_data:
168+
if entry.get("protocol") == "bgp":
169+
bgp_routes_found = True
170+
break
171+
assert bgp_routes_found, "BGP routes should be present in filtered output"

0 commit comments

Comments
 (0)