summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2016-10-27 18:21:09 +0200
committerRémy Coutable <remy@rymai.me>2016-10-27 18:21:09 +0200
commitcae27eae3f80a58bbf7eb49e9d077ea774f2b25a (patch)
tree77d6a4c5093280e0d2eacd6a82acc3cd827fcdbe
parentb3210df33e0e4af677666d2044f029c6aeac0d9e (diff)
downloadgitlab-ce-23890-api-should-accepts-boolean.tar.gz
API: Fix booleans not recognized as such when using the `to_boolean` helper23890-api-should-accepts-boolean
Signed-off-by: Rémy Coutable <remy@rymai.me>
-rw-r--r--CHANGELOG.md1
-rw-r--r--lib/api/helpers.rb1
-rw-r--r--spec/requests/api/api_helpers_spec.rb29
3 files changed, 20 insertions, 11 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 447a5614e75..5409fcc5752 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@ Please view this file on the master branch, on stable branches it's out of date.
- Fix HipChat notifications rendering (airatshigapov, eisnerd)
- Add hover to trash icon in notes !7008 (blackst0ne)
- Fix sidekiq stats in admin area (blackst0ne)
+ - API: Fix booleans not recognized as such when using the `to_boolean` helper
- Removed delete branch tooltip !6954
- Escape ref and path for relative links !6050 (winniehell)
- Fixed link typo on /help/ui to Alerts section. !6915 (Sam Rose)
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb
index 45120898b76..8025581d3ca 100644
--- a/lib/api/helpers.rb
+++ b/lib/api/helpers.rb
@@ -6,6 +6,7 @@ module API
SUDO_PARAM = :sudo
def to_boolean(value)
+ return value if [true, false].include?(value)
return true if value =~ /^(true|t|yes|y|1|on)$/i
return false if value =~ /^(false|f|no|n|0|off)$/i
diff --git a/spec/requests/api/api_helpers_spec.rb b/spec/requests/api/api_helpers_spec.rb
index 0f41f8dc7f1..f7fe4c10873 100644
--- a/spec/requests/api/api_helpers_spec.rb
+++ b/spec/requests/api/api_helpers_spec.rb
@@ -266,18 +266,25 @@ describe API::Helpers, api: true do
end
describe '.to_boolean' do
+ it 'accepts booleans' do
+ expect(to_boolean(true)).to be(true)
+ expect(to_boolean(false)).to be(false)
+ end
+
it 'converts a valid string to a boolean' do
- expect(to_boolean('true')).to be_truthy
- expect(to_boolean('YeS')).to be_truthy
- expect(to_boolean('t')).to be_truthy
- expect(to_boolean('1')).to be_truthy
- expect(to_boolean('ON')).to be_truthy
- expect(to_boolean('FaLse')).to be_falsy
- expect(to_boolean('F')).to be_falsy
- expect(to_boolean('NO')).to be_falsy
- expect(to_boolean('n')).to be_falsy
- expect(to_boolean('0')).to be_falsy
- expect(to_boolean('oFF')).to be_falsy
+ expect(to_boolean(true)).to be(true)
+ expect(to_boolean('true')).to be(true)
+ expect(to_boolean('YeS')).to be(true)
+ expect(to_boolean('t')).to be(true)
+ expect(to_boolean('1')).to be(true)
+ expect(to_boolean('ON')).to be(true)
+
+ expect(to_boolean('FaLse')).to be(false)
+ expect(to_boolean('F')).to be(false)
+ expect(to_boolean('NO')).to be(false)
+ expect(to_boolean('n')).to be(false)
+ expect(to_boolean('0')).to be(false)
+ expect(to_boolean('oFF')).to be(false)
end
it 'converts an invalid string to nil' do