Summary: null
param should be returned as nil
Let me explain. Suppose I have the following params:
param :code, Integer
param :name, String
And my app is an API that receives the following JSON:
{
"code": null,
"name": null
}
Now let´s see how Lotus coerces the values. Currently, the default behaviour is to delegate coercion to Ruby. Lotus/Validations, will call kernel/Integer and kernel/String respectively to coerce the params. That´s what happens:
Actual result
code => 0
name = ""
This can lead to unexpected behaviour because I would expect both empty values, in others words, to be “not present”.
Expected result
code => nil
name => nil
After chatting with @pascalbetz and @eduardodeoh, they agree that a null
param should be held as nil
.
Proposal
updated to reflect @pascalbetz comment.
Change how each coercer deals with null
params:
# Lotus::Utils::Kernel.Integer(nil) # => nil
# Lotus::Utils::Kernel.Integer("23.4") # => 23
def self.Integer(arg)
if arg.nil?
nil
else
super(arg)
# ...
end
And apply the same for the rest of the coercers.