Skip to content

Commit

Permalink
Fix notification polyfill from incomplete GQL status (#1080)
Browse files Browse the repository at this point in the history
When connected to a server sending GQL statuses instead of notifications, the
driver polyfills `summary.summary_notifications` from the GQL statuses. This PR
fixes a but where the driver would ignore any status that was lacking any of the
possible fields. However, at least the position is optional and should not cause
the status to be swallowed.
  • Loading branch information
robsdedude authored Aug 2, 2024
1 parent 4acb10f commit 4fbf12b
Show file tree
Hide file tree
Showing 2 changed files with 541 additions and 236 deletions.
76 changes: 30 additions & 46 deletions src/neo4j/_work/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,33 @@ def __getattr__(self, key):
f"'{self.__class__.__name__}' object has no attribute '{key}'"
)

@staticmethod
def _notification_from_status(status: dict) -> dict:
notification = {}
for notification_key, status_key in (
("title", "title"),
("code", "neo4j_code"),
("description", "description"),
):
if status_key in status:
notification[notification_key] = status[status_key]

if "diagnostic_record" in status:
diagnostic_record = status["diagnostic_record"]
if not isinstance(diagnostic_record, dict):
diagnostic_record = {}

for notification_key, diag_record_key in (
("severity", "_severity"),
("category", "_classification"),
("position", "_position"),
):
if diag_record_key in diagnostic_record:
notification[notification_key] = \
diagnostic_record[diag_record_key]

return notification

def _set_notifications(self):
if "notifications" in self.metadata:
notifications = self.metadata["notifications"]
Expand All @@ -156,53 +183,10 @@ def _set_notifications(self):
return
notifications = []
for status in statuses:
if not isinstance(status, dict):
continue
notification = {}
failed = False
for notification_key, status_key in (
("title", "title"),
("code", "neo4j_code"),
("description", "description"),
):
value = status.get(status_key)
if not isinstance(value, str) or not value:
failed = True
break
notification[notification_key] = value
if failed:
continue
diagnostic_record = status.get("diagnostic_record")
if not isinstance(diagnostic_record, dict):
continue
for notification_key, diag_record_key in (
("severity", "_severity"),
("category", "_classification"),
):
value = diagnostic_record.get(diag_record_key)
if not isinstance(value, str) or not value:
failed = True
break
notification[notification_key] = value
if failed:
continue
raw_position = diagnostic_record.get("_position")
if not isinstance(raw_position, dict):
continue
position = {}
for pos_key, raw_pos_key in (
("line", "line"),
("column", "column"),
("offset", "offset"),
):
value = raw_position.get(raw_pos_key)
if not isinstance(value, int) or isinstance(value, bool):
failed = True
break
position[pos_key] = value
if failed:
if not (isinstance(status , dict) and "neo4j_code" in status):
# not a notification status
continue
notification["position"] = position
notification = self._notification_from_status(status)
notifications.append(notification)
self.notifications = notifications
return
Expand Down
Loading

0 comments on commit 4fbf12b

Please sign in to comment.