Handling null
values and omissions in API requests
#2644
igboyes
started this conversation in
Developers
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The Virtool API still inconsistently handles
null
values and omissions.What does
null
mean in a request body?The client wants to set the value to
null
.If a user send
null
for a field that is not nullable, a validation error (400
) will be returned. The response will include a description of the problem.A
null
value in a request does not mean the value is being omitted from the request.What does omitting a field mean in a request body?
When a client omits a field from the request body, it means they don't want to modify it.
Make sure than an omitted value doesn't get converted to
None
in API code. This is a potential footgun because of how Pydantic request validation works.Fields
Make sure omission results in no change to the omitted field.
What does an empty string mean in a request body?
Use empty strings when a user-defined string value has not been filled.
For example, a
description
field that a user types out and adds to a resource should be""
when not populated.Use
null
if a string value is one of several options the user can select and the field is optional.For example, the
group
field on a sample can be one of the user groups defined for the instance. Users cannot enter their own arbitrary string value. Thegroup
field is optional, so it can be set tonull
when no group is selected (not""
).Pattern
Use the request model (eg.
UpdateUserSchema
) to pass data from the request handler to the data layer. Your data layer method signature should look like:This avoids:
dict
, which loses typing information.dict.get()
and accidentally setting a value toNone
.In the data layer method, use
exclude_unset
to get adict
that doesn't include fields omitted in the request:You can than use the
in
membership test to decide whether to update the field in database:There is probably opportunity to reduce repetion by implementing functions to unpack model objects and create MongoDB update dictionaries.
Beta Was this translation helpful? Give feedback.
All reactions