flask_accepts
flask_accepts copied to clipboard
Working with UUIDs, Marshmallow & 'missing' fields
Hi and thanks for this great package!
Wondering if there are any recommendations / thoughts on the following situation:
(1) I have a schema named PostSchema that has a UUID field (2) On post creation [i.e. post to "/posts"], this UUID does not yet exist (3) I want to use my PostSchema within my @accepts decorator
Going through the Marshmallow documentation I encountered the 'missing' parameter, that allows you to specify default values upon deserialisation [which differs from the 'default' parameter that performs a similar functionality but on serialisation]
As such I tried the following:
class PostSchema(Schema):
id = fields.UUID(missing=uuid.uuid4)
Now, while a similar approach worked great for the 'created_at' field with datetime, using the UUID as above causes Swagger to crash upon loading, as the UUID cannot be serialised by JSON encoder, which happens when Swagger tries to generate the default values.
I played around with id = fields.UUID(missing=lambda: str(uuid.uuid4))
, however while swagger documentation now works, on deserialisation of the object you have a string, rather than a UUID object.
I could attach the UUID in the post_load if it isn't present, but i'd really like to try get the default value working with swagger if possible.
Assistance would be much appreciated!