Skip to content

Commit 8804d8a

Browse files
committed
fix: singleValueUnserialize is now working more stable
PR viur-framework#1425 introduced a `singleValueUnserialize` implementation that has no robustness against any other formats. We still have old StringBone values in the database (`val={'val': '0.5', 'idx': '0.5'}`). This was never noticed, as there was never an error before. Now the value can neither be loaded nor ignored, nor does the `getDefaultValue` in except work without parameters. So no rebuild search index can be made with `refresh()` if the value is not loaded in the first place.
1 parent fa3f977 commit 8804d8a

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/viur/core/bones/numeric.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,9 @@ def __setattr__(self, key, value):
8383
def singleValueUnserialize(self, val):
8484
if val is not None:
8585
try:
86-
if self.precision:
87-
return round(float(val), self.precision)
88-
89-
return int(val)
90-
except ValueError:
91-
return self.getDefaultValue()
86+
return self._convert_to_numeric(val)
87+
except (ValueError, TypeError):
88+
return self.getDefaultValue(None) # FIXME: callable needs the skeleton instance
9289

9390
return val
9491

@@ -220,10 +217,12 @@ def _convert_to_numeric(self, value: t.Any) -> int | float:
220217
"""Convert a value to an int or float considering the precision.
221218
222219
If the value is not convertable an exception will be raised."""
220+
if isinstance(value, db.Entity | dict) and "val" in value:
221+
value = value["val"] # was a StringBone before
223222
if isinstance(value, str):
224223
value = value.replace(",", ".", 1)
225224
if self.precision:
226-
return float(value)
225+
return round(float(value), self.precision)
227226
else:
228227
# First convert to float then to int to support "42.5" (str)
229228
return int(float(value))

0 commit comments

Comments
 (0)