Skip to content

Commit

Permalink
parser_json: do not skip time key when parse fails
Browse files Browse the repository at this point in the history
parser_json: do not skip time key when parse fails

The documentation appears to suggest that the time key is discarded from
the key after it is "found and parsed", however in the current setup the
key is discarded even when parsing fails. This commit resets the `skip`
value if the time key parsing fails.

Additionally, an internal test case has been added for parser_json to validate 
the correction.

Note: This PR is a backport of PR #7323

Signed-off-by: braydonk <[email protected]>
  • Loading branch information
braydonk authored May 5, 2023
1 parent fddc5b8 commit c7b583c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/flb_parser_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,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 @@ -202,7 +203,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
Original file line number Diff line number Diff line change
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 }
};

0 comments on commit c7b583c

Please sign in to comment.