Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with UUIDs, Marshmallow & 'missing' fields #85

Open
delewis13 opened this issue Jul 16, 2020 · 0 comments
Open

Working with UUIDs, Marshmallow & 'missing' fields #85

delewis13 opened this issue Jul 16, 2020 · 0 comments

Comments

@delewis13
Copy link

delewis13 commented Jul 16, 2020

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant