Skip to content

Conversation

@grahamalama
Copy link
Contributor

When we included the callable in a public model field, we ran into a bug (#136 and #137) where jsonable_encoder couldn't serialize an action with an initialized requests.Session object.

Here's how I recreated the error we saw:

diff --git a/tests/unit/jbi/bugzilla_action.py b/tests/unit/jbi/bugzilla_action.py
new file mode 100644
index 0000000..d91e324
--- /dev/null
+++ b/tests/unit/jbi/bugzilla_action.py
@@ -0,0 +1,16 @@
+from bugzilla import Bugzilla
+from requests import Session
+
+session = Session()
+
+
+class TestBugzillaAction:
+    def __init__(self):
+        self.bz = Bugzilla(url=None, requests_session=session)
+
+    def __call__(self):
+        return lambda: "test"
+
+
+def init():
+    return TestBugzillaAction()
diff --git a/tests/unit/test_models.py b/tests/unit/test_models.py
new file mode 100644
index 0000000..6dc37ca
--- /dev/null
+++ b/tests/unit/test_models.py
@@ -0,0 +1,15 @@
+from src.jbi.models import Action
+from fastapi.encoders import jsonable_encoder
+
+
+def test_model_serializes():
+    action = Action.parse_obj(
+        {
+            "whiteboard_tag": "devtest",
+            "contact": "[email protected]",
+            "description": "test config",
+            "module": "tests.unit.jbi.bugzilla_action",
+        }
+    )
+    assert callable(action.callable)
+    jsonable_encoder(action)

For some reason, excluding the callable field with Config didn't seem to fix the bug. So instead, in this PR we move the callable to a private field with Pydantic's PrivateAttr.

We also add a test case as a regression test for the bug we observed.

This seems like more of a bug with jsonable_encoder, but it's probably good to remove the callable from the dict representation of the model anyway.

When we included the initialized callable in a public model field, we ran
into a bug where `jsonable_encoder` couldn't serialzie an action with an
initalized `requests.Session` object.

In this commit, we move the initialized caller to a private field so that it's
excluded during serialization.
@grahamalama grahamalama requested a review from a team as a code owner July 24, 2022 19:24
- change name
- caller takes and returns a payload
Pydantic does not include properties in the serialized object, which is
exactly what we need in this case.

This commit also changes the callable name to `caller` so we avoid
mirroring the built-in `callable` function
@grahamalama grahamalama requested a review from leplatrem July 25, 2022 22:05
@leplatrem leplatrem merged commit b681755 into main Jul 26, 2022
@leplatrem leplatrem deleted the private-callable-field branch July 26, 2022 12:22
leplatrem added a commit that referenced this pull request Aug 1, 2022
**Bug Fixes**

- Retrieve comments from the API if they are private (fixes #154) (#163)

**New Features**

- Show 'Bug XXXX' instead of Bugzilla Ticket in Jira link (#159)
- Add Jira details to log context (#157)
- Retry Jira and Bugzilla on error (fixes #33)  (#152)
- Add counter and timer for action execution (fixes #23, #62, #160, #164)
- Tweak logging (log `500` requests), other logging-adjacent tweaks (#161)

**Configuration**

- Use JB instead of OSS in config.prod.yaml and config.nonprod.yaml (#151, #153)
- Add local config for tests (fixes #121) (#138)

**Documentation**

- Start (naive) troubleshooting section in README (#156)

**Internal Changes**

- Run app as a single uvicorn process (fixes #133) (#162)
- Move initialized action callable to private field (#145)
- Do not sleep in retry tests (#158)

- Bump sentry-sdk from 1.8.0 to 1.9.0 (#167)
- Bump yamllint from 1.26.3 to 1.27.1 (#166)
- Bump atlassian-python-api from 3.20.1 to 3.25.0 (#149)
- Bump dockerflow from 2022.1.0 to 2022.7.0 (#147)
- Bump sentry-sdk from 1.7.2 to 1.8.0 (#146)
- Bump detect-secrets from 1.2.0 to 1.3.0 (#148)
- Bump mypy from 0.910 to 0.971 (#150)
@leplatrem leplatrem mentioned this pull request Aug 1, 2022
@leplatrem leplatrem added the enhancement New feature or request label Aug 1, 2022
leplatrem added a commit that referenced this pull request Aug 1, 2022
**Bug Fixes**

- Retrieve comments from the API if they are private (fixes #154) (#163)

**New Features**

- Show 'Bug XXXX' instead of Bugzilla Ticket in Jira link (#159)
- Add Jira details to log context (#157)
- Retry Jira and Bugzilla on error (fixes #33)  (#152)
- Add counter and timer for action execution (fixes #23, #62, #160, #164)
- Tweak logging (log `500` requests), other logging-adjacent tweaks (#161)

**Configuration**

- Use JB instead of OSS in config.prod.yaml and config.nonprod.yaml (#151, #153)
- Add local config for tests (fixes #121) (#138)

**Documentation**

- Start (naive) troubleshooting section in README (#156)

**Internal Changes**

- Run app as a single uvicorn process (fixes #133) (#162)
- Move initialized action callable to private field (#145)
- Do not sleep in retry tests (#158)

- Bump sentry-sdk from 1.8.0 to 1.9.0 (#167)
- Bump yamllint from 1.26.3 to 1.27.1 (#166)
- Bump atlassian-python-api from 3.20.1 to 3.25.0 (#149)
- Bump dockerflow from 2022.1.0 to 2022.7.0 (#147)
- Bump sentry-sdk from 1.7.2 to 1.8.0 (#146)
- Bump detect-secrets from 1.2.0 to 1.3.0 (#148)
- Bump mypy from 0.910 to 0.971 (#150)
- Bump fastapi from 0.73.0 to 0.79.0 (#168)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants