From 81c4186a9ae2d03d2fe7044fc529c4c73d1aa514 Mon Sep 17 00:00:00 2001 From: Shizuo Fujita Date: Fri, 30 Oct 2026 18:26:04 +0900 Subject: [PATCH 1/2] parser_json: skip convert_values when no conversion is configured Since #4474, parse calls convert_values for every record through parse_one_record. convert_values returns a new [time, record] array even when types, null_value_pattern and null_empty_string are not set. Check @execute_convert_values in parse again, as before #4474, and remove parse_one_record. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Shizuo Fujita --- lib/fluent/plugin/parser_json.rb | 15 ++++++--------- test/plugin/test_parser_json.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/lib/fluent/plugin/parser_json.rb b/lib/fluent/plugin/parser_json.rb index 45fe8607c7..0537145800 100644 --- a/lib/fluent/plugin/parser_json.rb +++ b/lib/fluent/plugin/parser_json.rb @@ -67,16 +67,18 @@ def parse(text) parsed_json = @load_proc.call(text) if parsed_json.is_a?(Hash) - time, record = parse_one_record(parsed_json) - yield time, record + time = parse_time(parsed_json) + time, parsed_json = convert_values(time, parsed_json) if @execute_convert_values + yield time, parsed_json elsif parsed_json.is_a?(Array) parsed_json.each do |record| unless record.is_a?(Hash) yield nil, nil next end - time, parsed_record = parse_one_record(record) - yield time, parsed_record + time = parse_time(record) + time, record = convert_values(time, record) if @execute_convert_values + yield time, record end else yield nil, nil @@ -86,11 +88,6 @@ def parse(text) yield nil, nil end - def parse_one_record(record) - time = parse_time(record) - convert_values(time, record) - end - def parser_type :text end diff --git a/test/plugin/test_parser_json.rb b/test/plugin/test_parser_json.rb index 95516208e8..31e40492a4 100644 --- a/test/plugin/test_parser_json.rb +++ b/test/plugin/test_parser_json.rb @@ -197,6 +197,36 @@ def test_parse_with_keep_time_key_without_time_format(data) end end + sub_test_case "convert_values" do + def test_parse_applies_types + @parser.configure('types' => 'size:string') + @parser.instance.parse('{"time":1362020400,"size":777}') { |time, record| + assert_equal(event_time('2013-02-28 12:00:00 +0900').to_i, time) + assert_equal({'size' => '777'}, record) + } + end + + data('no conversion' => [{}, 0], + 'types' => [{'types' => 'k:string'}, 2], + 'null_empty_string' => [{'null_empty_string' => 'true'}, 2], + 'null_value_pattern' => [{'null_value_pattern' => '^-$'}, 2]) + def test_parse_calls_convert_values_only_with_conversion((conf, expected)) + klass = Class.new(Fluent::Plugin::JSONParser) do + attr_reader :convert_count + + def convert_values(time, record) + @convert_count = @convert_count.to_i + 1 + super + end + end + parser = Fluent::Test::Driver::Parser.new(klass) + parser.configure(conf) + parser.instance.parse('{"k":"v"}') { |time, record| } + parser.instance.parse('[{"k":"v"}]') { |time, record| } + assert_equal(expected, parser.instance.convert_count.to_i) + end + end + def test_yajl_parse_io_with_buffer_smaller_than_input parser = Fluent::Test::Driver::Parser.new(Fluent::Plugin::JSONParser) parser.configure( From 6c5990fbbc32468948679765e7fdfef5b87b5ed2 Mon Sep 17 00:00:00 2001 From: Shizuo Fujita Date: Fri, 30 Oct 2026 18:49:52 +0900 Subject: [PATCH 2/2] test: check that parse yields in test_parse_applies_types The assertions were only in the block, so the test passed even when parse yielded nothing. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Shizuo Fujita --- test/plugin/test_parser_json.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/plugin/test_parser_json.rb b/test/plugin/test_parser_json.rb index 31e40492a4..16f22e802e 100644 --- a/test/plugin/test_parser_json.rb +++ b/test/plugin/test_parser_json.rb @@ -200,10 +200,11 @@ def test_parse_with_keep_time_key_without_time_format(data) sub_test_case "convert_values" do def test_parse_applies_types @parser.configure('types' => 'size:string') + results = [] @parser.instance.parse('{"time":1362020400,"size":777}') { |time, record| - assert_equal(event_time('2013-02-28 12:00:00 +0900').to_i, time) - assert_equal({'size' => '777'}, record) + results << [time.to_i, record] } + assert_equal([[event_time('2013-02-28 12:00:00 +0900').to_i, {'size' => '777'}]], results) end data('no conversion' => [{}, 0],