From 19cf6cf03a14294990fea0cd07d2295a98e5a39e Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sun, 8 Mar 2026 12:26:21 -0500 Subject: [PATCH] fix: serialize None to null, for ObjectRef schema type not sure how this didn't come up until now..anyway hopefully it's the right thing but we'll see --- src/wuttaweb/forms/schema.py | 10 ++++++---- tests/forms/test_schema.py | 2 ++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/wuttaweb/forms/schema.py b/src/wuttaweb/forms/schema.py index bca71ae..5e0364e 100644 --- a/src/wuttaweb/forms/schema.py +++ b/src/wuttaweb/forms/schema.py @@ -357,15 +357,17 @@ class ObjectRef(colander.SchemaType): def serialize(self, node, appstruct): # pylint: disable=empty-docstring """ """ - # nb. normalize to empty option if no object ref, so that - # works as expected + # normalize to empty option if no object ref, so that works as + # expected if self.empty_option and not appstruct: return self.empty_option[0] - if appstruct is colander.null: + # even if there is no empty option, still treat any false-ish + # value as null + if not appstruct: return colander.null - # nb. keep a ref to this for later use + # keep a ref to this for later use node.model_instance = appstruct # serialize to PK as string diff --git a/tests/forms/test_schema.py b/tests/forms/test_schema.py index f2290ef..844cb44 100644 --- a/tests/forms/test_schema.py +++ b/tests/forms/test_schema.py @@ -257,6 +257,8 @@ class TestObjectRef(DataTestCase): typ = mod.ObjectRef(self.request) value = typ.serialize(node, colander.null) self.assertIs(value, colander.null) + value = typ.serialize(node, None) + self.assertIs(value, colander.null) # model instance person = model.Person(full_name="Betty Boop")