Skip to content

Commit

Permalink
Make sure JSON parser returns Hash
Browse files Browse the repository at this point in the history
Record must be Hash.

Signed-off-by: Daijiro Fukuda <fukuda@clear-code.com>
  • Loading branch information
daipom committed Mar 21, 2023
1 parent d5df992 commit 9c0005e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/fluent/plugin/parser_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def configure_json_parser(name)

def parse(text)
record = @load_proc.call(text)
# Sometimes the parser returns String, not Hash.
# Ex. Oj.load('"hoge"') => "hoge"
raise @error_class unless record.is_a?(Hash)
time = parse_time(record)
if @execute_convert_values
time, record = convert_values(time, record)
Expand Down
26 changes: 26 additions & 0 deletions test/plugin/test_parser_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,30 @@ def test_yajl_parse_io_with_buffer_smaller_than_input
end
end
end

sub_test_case "various record pattern" do
data("Only string", { record: '"message"', exepected: nil }, keep: true)
data("Only string without quaot", { record: "message", exepected: nil }, keep: true)
data("Only number", { record: "0", exepected: nil }, keep: true)
def test_oj(data)
@parser.configure('json_parser' => "oj")
@parser.instance.parse(data[:record]) { |time, record|
assert_equal(data[:expected], record)
}
end

def test_yajl(data)
@parser.configure('json_parser' => "yajl")
@parser.instance.parse(data[:record]) { |time, record|
assert_equal(data[:expected], record)
}
end

def test_json(json)
@parser.configure('json_parser' => "json")
@parser.instance.parse(data[:record]) { |time, record|
assert_equal(data[:expected], record)
}
end
end
end

0 comments on commit 9c0005e

Please sign in to comment.