Skip to content

Commit 989923e

Browse files
committed
Merge pull request rails#13912 from mauricio/bug-13907
Fixes issue with parsing whitespace content back from database - fixes rails#13907
2 parents 82701cd + a34c10f commit 989923e

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

activerecord/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
* Parsing PostgreSQL arrays with empty strings now works correctly.
2+
3+
Previously, if you tried to parse `{"1","","2","","3"}` the result
4+
would be `["1","2","3"]`, removing the empty strings from the array,
5+
which would be incorrect. Now it will correctly produce `["1","","2","","3"]`
6+
as the result of parsing the above PostgreSQL array.
7+
8+
Fixes #13907.
9+
10+
*Maurício Linhares*
11+
112
* Associations now raise `ArgumentError` on name conflicts.
213

314
Dangerous association names conflicts include instance or class methods already

activerecord/lib/active_record/connection_adapters/postgresql/array_parser.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ def parse_array_contents(array, string, index)
9191
end
9292

9393
def add_item_to_array(array, current_item, quoted)
94-
if current_item.length == 0
95-
elsif !quoted && current_item == 'NULL'
94+
return if !quoted && current_item.length == 0
95+
96+
if !quoted && current_item == 'NULL'
9697
array.push nil
9798
else
9899
array.push current_item

activerecord/test/cases/adapters/postgresql/array_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ def test_multi_dimensional_with_strings
9393
assert_cycle(:tags, [[['1'], ['2']], [['2'], ['3']]])
9494
end
9595

96+
def test_with_empty_strings
97+
assert_cycle(:tags, [ '1', '2', '', '4', '', '5' ])
98+
end
99+
100+
def test_with_multi_dimensional_empty_strings
101+
assert_cycle(:tags, [[['1', '2'], ['', '4'], ['', '5']]])
102+
end
103+
104+
def test_with_arbitrary_whitespace
105+
assert_cycle(:tags, [[['1', '2'], [' ', '4'], [' ', '5']]])
106+
end
107+
96108
def test_multi_dimensional_with_integers
97109
assert_cycle(:ratings, [[[1], [7]], [[8], [10]]])
98110
end

0 commit comments

Comments
 (0)