Skip to content

Commit

Permalink
Fixes Invariant in union serialization when the value is a LazyObject
Browse files Browse the repository at this point in the history
  • Loading branch information
crgwbr committed Feb 2, 2024
1 parent 2318f9d commit c0669c1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions reactivated/serialization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from django.db import models
from django.forms.models import ModelChoiceIteratorValue # type: ignore
from django.utils.module_loading import import_string
from django.utils.functional import LazyObject

from reactivated import fields, stubs, utils
from reactivated.models import ComputedRelation
Expand Down Expand Up @@ -1132,6 +1133,10 @@ def serialize(
if value is None:
return None

# If value is a lazy object, resolve it before continuing.
if isinstance(value, LazyObject):
value = value.__reduce__()[1][0]

serializer: Serializer
serializer_path = schema.schema.get("serializer", None)

Expand Down
2 changes: 2 additions & 0 deletions tests/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from django.db.models import IntegerField, Model, UUIDField
from django.forms.models import ModelChoiceIteratorValue
from django.utils.translation import gettext, gettext_lazy
from django.utils.functional import SimpleLazyObject
from jsonschema import validate

from reactivated import Pick
Expand Down Expand Up @@ -608,6 +609,7 @@ def test_simple_union(snapshot):
assert serialize(["1", "2", "3"], schema) == ["1", "2", "3"]
assert serialize((5, "hello"), schema) == [5, "hello"]
assert serialize(True, schema) is True
assert serialize(SimpleLazyObject(lambda: "hello"), schema) == "hello"
assert schema == snapshot


Expand Down

0 comments on commit c0669c1

Please sign in to comment.