Skip to content

Commit 68a2ff0

Browse files
authored
Use orjson for serialising responses. (#86)
When creating a bug we embed the response of the bugzilla update_bugs call into the repsonse. This contains a datetime instance which is not serialisable by the default JSONResponse and so currently JBI throws an internal server error back to Bugzilla which then repeats the request a second time which is effectively ignored because by then the bug has the see_also field set. This switches to ORJSONResponse which can serialise datetime and a few other types correctly.
1 parent 01b5b48 commit 68a2ff0

File tree

5 files changed

+73
-8
lines changed

5 files changed

+73
-8
lines changed

poetry.lock

Lines changed: 49 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dockerflow = "2022.1.0"
1818
Jinja2 = "^3.0.3"
1919
pydantic-yaml = {extras = ["pyyaml","ruamel"], version = "^0.6.1"}
2020
sentry-sdk = "^1.5.7"
21+
orjson = "^3.7.7"
2122

2223

2324
[tool.poetry.dev-dependencies]

src/app/api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import sentry_sdk
1111
import uvicorn # type: ignore
1212
from fastapi import Body, Depends, FastAPI, Request
13-
from fastapi.responses import HTMLResponse, JSONResponse
13+
from fastapi.responses import HTMLResponse, ORJSONResponse
1414
from fastapi.staticfiles import StaticFiles
1515
from fastapi.templating import Jinja2Templates
1616
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
@@ -97,9 +97,9 @@ def bugzilla_webhook(
9797
"""API endpoint that Bugzilla Webhook Events request"""
9898
try:
9999
result = execute_action(request, actions, settings)
100-
return JSONResponse(content=result, status_code=200)
100+
return ORJSONResponse(content=result, status_code=200)
101101
except IgnoreInvalidRequestError as exception:
102-
return JSONResponse(content={"error": str(exception)}, status_code=200)
102+
return ORJSONResponse(content={"error": str(exception)}, status_code=200)
103103

104104

105105
@app.get("/whiteboard_tags/")

src/app/monitor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Dict
55

66
from fastapi import APIRouter, Depends, Request
7-
from fastapi.responses import JSONResponse
7+
from fastapi.responses import ORJSONResponse
88

99
from src.app import environment
1010
from src.jbi.services import jbi_service_health_map
@@ -20,7 +20,7 @@ def heartbeat(request: Request, settings: environment.Settings):
2020
if not health.get("up"):
2121
status_code = 503
2222

23-
return JSONResponse(content=data, status_code=status_code)
23+
return ORJSONResponse(content=data, status_code=status_code)
2424

2525

2626
@api_router.get("/__heartbeat__")

tests/unit/app/test_api.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from unittest import mock
66

77
import pytest
8+
from datetime import datetime
89
from fastapi.testclient import TestClient
910

1011
from src.app.api import app
@@ -96,8 +97,23 @@ def test_webhook_is_200_if_action_succeeds(
9697
mocked_jira,
9798
mocked_bugzilla,
9899
):
99-
mocked_bugzilla().update_bugs.return_value = {}
100-
mocked_jira().create_or_update_issue_remote_links.return_value = {}
100+
mocked_bugzilla().update_bugs.return_value = {
101+
"bugs": [
102+
{
103+
"changes": {
104+
"see_also": {
105+
"added": "https://mozilla.atlassian.net/browse/JBI-1922",
106+
"removed": "",
107+
}
108+
},
109+
"last_change_time": datetime.now(),
110+
}
111+
]
112+
}
113+
mocked_jira().create_or_update_issue_remote_links.return_value = {
114+
"id": 18936,
115+
"self": "https://mozilla.atlassian.net/rest/api/2/issue/JBI-1922/remotelink/18936",
116+
}
101117

102118
with TestClient(app) as anon_client:
103119
response = anon_client.post(

0 commit comments

Comments
 (0)