Skip to content

Commit

Permalink
Fix json and yaml formatters to respect num lines (#929)
Browse files Browse the repository at this point in the history
The json and yaml formatters were not outputting the lines of
content according to the user's selection.

This is a result of issue:as_dict() function that does not pass
through the max_lines argument to get_code. As a result, it
would always return the default of 3.

This change now optionally passes through desired lines of content.

Fixes #604

Signed-off-by: Eric Brown <eric_wade_brown@yahoo.com>
  • Loading branch information
ericwb committed Jul 14, 2022
1 parent f352b20 commit 6a00317
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions bandit/core/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def get_code(self, max_lines=3, tabbed=False):
lines.append(tmplt % (line, text))
return "".join(lines)

def as_dict(self, with_code=True):
def as_dict(self, with_code=True, max_lines=3):
"""Convert the issue to a dict of values for outputting."""
out = {
"filename": self.fname,
Expand All @@ -213,7 +213,7 @@ def as_dict(self, with_code=True):
}

if with_code:
out["code"] = self.get_code()
out["code"] = self.get_code(max_lines=max_lines)
return out

def from_dict(self, data, with_code=True):
Expand Down
8 changes: 5 additions & 3 deletions bandit/formatters/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,16 @@ def report(manager, fileobj, sev_level, conf_level, lines=-1):
if baseline:
collector = []
for r in results:
d = r.as_dict()
d = r.as_dict(max_lines=lines)
d["more_info"] = docs_utils.get_url(d["test_id"])
if len(results[r]) > 1:
d["candidates"] = [c.as_dict() for c in results[r]]
d["candidates"] = [
c.as_dict(max_lines=lines) for c in results[r]
]
collector.append(d)

else:
collector = [r.as_dict() for r in results]
collector = [r.as_dict(max_lines=lines) for r in results]
for elem in collector:
elem["more_info"] = docs_utils.get_url(elem["test_id"])

Expand Down
2 changes: 1 addition & 1 deletion bandit/formatters/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def report(manager, fileobj, sev_level, conf_level, lines=-1):
sev_level=sev_level, conf_level=conf_level
)

collector = [r.as_dict() for r in results]
collector = [r.as_dict(max_lines=lines) for r in results]
for elem in collector:
elem["more_info"] = docs_utils.get_url(elem["test_id"])

Expand Down

0 comments on commit 6a00317

Please sign in to comment.