Boolean normalization in the API

Hey,

in Foreman docs we generate for all boolean fields: Must be one of: true, false, 1, 0

I wonder why do we accept integers. While ActiveRecord might normalize integers when params are passed into it, API can still be used in business logic and we apparently don’t want to manually normalize booleans everywhere. Can someone enlighten me?

I would like to propose to remove documenting that we accept also integers. We should not enforce this however not to break current behavior, but if I get a bug “look number does not work as boolean” I can just close it asking to send JSON native boolean.

I don’t know why accepting 1 and 0 as a boolean was added to the docs. If there’s no particular reason for supporting integers I’m for being more restrictive and mentioning only true/false in the docs.

1 Like

@iNecas I am trying to find the place in the codebase (definition of :bool) but I fail - can you get me on the rails?

This commit is most likely what you’re looking for https://github.com/Apipie/apipie-rails/commit/5fbc97575341695080121b76a33a7a5c5bc2524a

As @aruzicka said.

The reason why this is marked this way in apipie-rails is that this is default Rails behavior for booleans (that we also have in Foreman), you can check:

curl -k -u admin:changeme -X PUT -H 'Content-Type: application/json' -d '{"user":{"admin":0}}' http://localhost:3000/api/v2/users/4

curl -k -u admin:changeme -X PUT -H 'Content-Type: application/json' -d '{"user":{"admin":1}}' http://localhost:3000/api/v2/users/4

If we want to have more restrictive, we should probably use our own :boolean validator, as I don’t think changing this globally for all apipie users would be the way to go.

If this was behavior of Rails controller/routing stack then I’d agree, but I believe this is a behavior of ActiveRecord. There is still the problem when I try to access the user-admin flag in an controller - I am getting integer out of it. Therefore we need to review all our code and put zero? calls everywhere.

Unless I am terribly wrong and Rails “magic” does not work in my case, this is a valid argument to stop advertising integers for boolean flags.