Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser_json: keep time key when parsing fails #7323

Merged
merged 3 commits into from May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/flb_parser_json.c
Expand Up @@ -204,6 +204,7 @@ int flb_parser_json_do(struct flb_parser *parser,
flb_warn("[parser:%s] invalid time format %s for '%s'",
parser->name, parser->time_fmt_full, tmp);
time_lookup = 0;
skip = map_size;
}
else {
time_lookup = flb_parser_tm2time(&tm);
Expand All @@ -213,7 +214,7 @@ int flb_parser_json_do(struct flb_parser *parser,
msgpack_sbuffer_init(&mp_sbuf);
msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);

if (parser->time_keep == FLB_FALSE) {
if (parser->time_keep == FLB_FALSE && skip < map_size) {
msgpack_pack_map(&mp_pck, map_size - 1);
}
else {
Expand Down
57 changes: 57 additions & 0 deletions tests/internal/parser_json.c
Expand Up @@ -479,12 +479,69 @@ void test_decode_field_json()
flb_config_exit(config);
}

void test_time_key_kept_if_parse_fails()
{
struct flb_parser *parser = NULL;
struct flb_config *config = NULL;
int ret = 0;
char *input = "{\"str\":\"text\", \"time\":\"nonsense\"}";
char *time_format = "%Y-%m-%dT%H:%M:%S.%L";
void *out_buf = NULL;
size_t out_size = 0;
struct flb_time out_time;
char *expected_strs[] = {"str", "text", "time", "nonsense"};
struct str_list expected = {
.size = sizeof(expected_strs)/sizeof(char*),
.lists = &expected_strs[0],
};

out_time.tm.tv_sec = 0;
out_time.tm.tv_nsec = 0;

config = flb_config_init();
if(!TEST_CHECK(config != NULL)) {
TEST_MSG("flb_config_init failed");
exit(1);
}

parser = flb_parser_create("json", "json", NULL, FLB_FALSE, time_format, "time", NULL,
FLB_FALSE, FLB_TRUE, FLB_FALSE,
NULL, 0, NULL, config);
if (!TEST_CHECK(parser != NULL)) {
TEST_MSG("flb_parser_create failed");
flb_config_exit(config);
exit(1);
}

ret = flb_parser_do(parser, input, strlen(input), &out_buf, &out_size, &out_time);
if (!TEST_CHECK(ret != -1)) {
TEST_MSG("flb_parser_do failed");
flb_parser_destroy(parser);
flb_config_exit(config);
exit(1);
}

ret = compare_msgpack(out_buf, out_size, &expected);
if (!TEST_CHECK(ret == 0)) {
TEST_MSG("compare failed");
flb_free(out_buf);
flb_parser_destroy(parser);
flb_config_exit(config);
exit(1);
}

flb_free(out_buf);
flb_parser_destroy(parser);
flb_config_exit(config);
}


TEST_LIST = {
{ "basic", test_basic},
{ "time_key", test_time_key},
{ "time_keep", test_time_keep},
{ "types_is_not_supported", test_types_is_not_supported},
{ "decode_field_json", test_decode_field_json},
{ "time_key_kept_if_parse_fails", test_time_key_kept_if_parse_fails},
{ 0 }
};