Unknown converter name is silently accepted and later fails with "undefined method 'arity' for nil"
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 57/100
Hướng nghiên cứu
Start with lib/csv/fields_converter.rb, especially FieldsConverter#add_converter, where named converters are looked up and added. Add coverage for unknown names passed through CSV#convert, converters:, and header_converters:, then update the documented behavior of CSV#convert and CSV#header_convert; done means invalid names raise a clear ArgumentError when added and the relevant tests pass.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
When an unknown converter name is passed to converters: / header_converters: (or to CSV#convert / CSV#header_convert), csv accepts it silently and stores nil as the converter. The mistake only shows up later, when the first field is converted, as:
NoMethodError: undefined method 'arity' for nil
The message does not mention converters at all, so it is hard to tell that the cause is a typo in a converter name, or a name that only exists in a newer csv. For example, header_converters: :symbol_raw (added in csv 3.2.3) fails this way on Ruby 3.1.0 (csv 3.2.2).
The current behavior is documented in the RDoc of CSV#convert and CSV#header_convert ("Raises a parse-time exception ... NoMethodError"), so this may be known. I'd still like to propose a clearer error.
Reproduction
require "csv"
data = "A,B\n1,2\n"
csv = CSV.new(data, headers: true, header_converters: :bogus) # no error here
p csv.header_converters # => [nil]
csv.read # => NoMethodError: undefined method 'arity' for nil
CSV.parse_line("1,2", converters: [:integer, :bogus])
# => NoMethodError: undefined method 'arity' for nil
csv = CSV.new(data)
p csv.convert(:bogus) # => [nil]
csv.read # => NoMethodError: undefined method 'arity' for nil
# String names are not accepted either (only Symbol keys match):
CSV.parse_line("1,2", converters: "integer")
# => NoMethodError: undefined method 'arity' for nil
# With no data rows, the bad name is never noticed:
CSV.parse("", headers: true, header_converters: :bogus) # => #<CSV::Table ...>
Versions
The same result on every version I tried:
| Ruby | csv | CSV.new(..., header_converters: :bogus) |
#header_converters |
read |
|---|---|---|---|---|
| 3.0.0 | 3.1.9 | no error | [nil] |
NoMethodError (arity for nil) |
| 3.1.0 | 3.2.2 | no error | [nil] |
NoMethodError |
| 3.2.0 | 3.2.6 | no error | [nil] |
NoMethodError |
| 3.4.0 | 3.3.2 | no error | [nil] |
NoMethodError |
| 4.0.0 | 3.3.5 | no error | [nil] |
NoMethodError |
| master (4.1.0dev) | 3.3.7 | no error | [nil] |
NoMethodError |
| master (4.1.0dev) | main (afbee5e) | no error | [nil] |
NoMethodError |
converters: :bogus, [:downcase, :bogus], "downcase", CSV#convert(:bogus), CSV#header_convert(:bogus), String#parse_csv(converters: :bogus) and CSV.parse_line all behave the same way.
Cause
CSV::FieldsConverter#add_converter looks the name up in CSV::Converters / CSV::HeaderConverters and appends the result without checking it, so an unknown name appends nil:
https://github.com/ruby/csv/blob/v3.3.7/lib/csv/fields_converter.rb#L34-L42
The nil is only used when a field is converted:
https://github.com/ruby/csv/blob/v3.3.7/lib/csv/fields_converter.rb#L65
Expected (proposal)
How about raising ArgumentError with the name when the converter is added? For example:
ArgumentError: unknown converter name: :bogus (not in CSV::HeaderConverters)
Since the converters given by the converters: / header_converters: options are already built in CSV.new, this would make CSV.new (and CSV#convert / CSV#header_convert) fail right away, and the message points at the problem.
One compatibility note: code that passes an unknown name but never converts a field (e.g. empty input) currently works and would start raising. I think that is acceptable because such code is already broken for any non-empty input, but I'd like to hear your opinion.
Possible fix
diff --git a/lib/csv/fields_converter.rb b/lib/csv/fields_converter.rb
--- a/lib/csv/fields_converter.rb
+++ b/lib/csv/fields_converter.rb
@@ -37,6 +37,10 @@ class CSV
combo.each do |sub_name|
add_converter(sub_name)
end
+ when nil
+ message = "unknown converter name: #{name.inspect} " +
+ "(not in CSV::#{@builtin_converters_name})"
+ raise ArgumentError, message
else # individual named converter
@converters << combo
end
plus tests for CSV#convert(:nosuch), converters: [:integer, :nosuch] and header_converters: :nosuch, and an update of the RDoc of CSV#convert / CSV#header_convert. With this change the whole test suite passes on Ruby master. I can open a pull request if this direction is OK.
🤖 Generated with Claude Code
- Ngôn ngữ chính
- Ruby
- Star
- 220
- Fork
- 139
- Merge trung bình
- 1 ngày 1 giờ
- Pull request đã merge (30 ngày)
- 2
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của ruby/csv
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
-
Timeout on WindowsCó thể làm lại được @kou đã nhận 406 ngày trước và không có pull request nào đang mở. Đang mở
-
FiberError/NoMemoryError error related to csv gem occurs when upgrading from ruby 3.1 to 3.3Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
-
Recipes not copied downstreamĐang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 15/100
Issue tương tự
-
🐛 bug 👩💻 équipe CE 🧑🏭 entreprises
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
betagouv/conseillers-entreprises#4808 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
yosiat/panko_serializer#250 ·
-
[Server Submission]: Aspera CLIĐang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 75/100
cline/mcp-marketplace#3886 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100