diff --git a/rcl_yaml_param_parser/src/parser.c b/rcl_yaml_param_parser/src/parser.c index 5313edf..7851605 100644 --- a/rcl_yaml_param_parser/src/parser.c +++ b/rcl_yaml_param_parser/src/parser.c @@ -187,7 +187,14 @@ static rcl_ret_t add_name_to_ns( ns_len = strlen(cur_ns); name_len = strlen(name); sep_len = strlen(sep_str); - tot_len = ns_len + name_len + sep_len + 1U; + // Check the last sep_len characters of the current NS against the separator string. + if (strcmp(cur_ns + ns_len - sep_len, sep_str) == 0) { + // Current NS already ends with the separator: don't put another separator in. + sep_len = 0; + sep_str = ""; + } + + tot_len = ns_len + sep_len + name_len + 1U; if (tot_len > MAX_STRING_SIZE) { RCL_SET_ERROR_MSG("New namespace string is exceeding max string size", diff --git a/rcl_yaml_param_parser/test/root_ns.yaml b/rcl_yaml_param_parser/test/root_ns.yaml new file mode 100644 index 0000000..84ce013 --- /dev/null +++ b/rcl_yaml_param_parser/test/root_ns.yaml @@ -0,0 +1,7 @@ +# config/test_yaml +--- + +/: + my_node: + ros__parameters: + param_name: 'value' diff --git a/rcl_yaml_param_parser/test/test_parse_yaml.cpp b/rcl_yaml_param_parser/test/test_parse_yaml.cpp index 7d6815b..738cdd5 100644 --- a/rcl_yaml_param_parser/test/test_parse_yaml.cpp +++ b/rcl_yaml_param_parser/test/test_parse_yaml.cpp @@ -62,6 +62,31 @@ TEST(test_file_parser, multi_ns_correct_syntax) { allocator.deallocate(path, allocator.state); } +TEST(test_file_parser, root_ns) { + rcutils_reset_error(); + EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024)); + rcutils_allocator_t allocator = rcutils_get_default_allocator(); + char * test_path = rcutils_join_path(cur_dir, "test", allocator); + char * path = rcutils_join_path(test_path, "root_ns.yaml", allocator); + fprintf(stderr, "cur_path: %s\n", path); + EXPECT_TRUE(rcutils_exists(path)); + rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator); + EXPECT_FALSE(NULL == params_hdl); + bool res = rcl_parse_yaml_file(path, params_hdl); + fprintf(stderr, "%s\n", rcutils_get_error_string_safe()); + EXPECT_TRUE(res); + rcl_yaml_node_struct_print(params_hdl); + + // Check that there is only one forward slash in the node's FQN. + // (Regression test for https://github.com/ros2/rcl/pull/299). + ASSERT_EQ(1u, params_hdl->num_nodes); + ASSERT_STREQ("/my_node", params_hdl->node_names[0]); + + rcl_yaml_node_struct_fini(params_hdl); + allocator.deallocate(test_path, allocator.state); + allocator.deallocate(path, allocator.state); +} + TEST(test_file_parser, seq_map1) { rcutils_reset_error(); EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024));