From 2f4470edca40a16d408573209a6802088b2777c8 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Thu, 18 Jun 2020 15:23:43 +0100 Subject: [PATCH] Serialise UUID to string not UUID object MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We need to serialise the template to JSON to store it in Redis. Python’s built in JSON serialiser doesn’t know what to do with a UUID object, so we need to manually cast it to a string instead. --- app/schemas.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/schemas.py b/app/schemas.py index 1dafdba14..dded0b09e 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -13,6 +13,7 @@ from marshmallow import ( post_dump ) from marshmallow_sqlalchemy import field_for +from uuid import UUID from notifications_utils.recipients import ( validate_email_address, @@ -58,6 +59,14 @@ def _validate_datetime_not_in_past(dte, msg="Date cannot be in the past"): raise ValidationError(msg) +class UUIDsAsStringsMixin: + @post_dump() + def __post_dump(self, data): + for key, value in data.items(): + if isinstance(value, UUID): + data[key] = str(value) + + class BaseSchema(ma.ModelSchema): def __init__(self, load_json=False, *args, **kwargs): @@ -341,7 +350,7 @@ class BaseTemplateSchema(BaseSchema): strict = True -class TemplateSchema(BaseTemplateSchema): +class TemplateSchema(BaseTemplateSchema, UUIDsAsStringsMixin): created_by_id = field_for( models.Template, 'created_by_id', dump_to='created_by', dump_only=True