Skip to content

Commit

Permalink
Fix type sources conversion for actually empty sources
Browse files Browse the repository at this point in the history
Signed-off-by: Emerson Knapp <emerson.b.knapp@gmail.com>
  • Loading branch information
emersonknapp committed Jun 26, 2023
1 parent 7fc9f12 commit a9e72c5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion rcl/src/rcl/node_type_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ rcl_ret_t rcl_node_type_cache_register_type(
type_info_with_registrations.type_info.type_sources =
rcl_convert_type_source_sequence_runtime_to_msg(type_description_sources);
RCL_CHECK_FOR_NULL_WITH_MSG(
type_info_with_registrations.type_info.type_description,
type_info_with_registrations.type_info.type_sources,
"converting type sources struct failed",
type_description_interfaces__msg__TypeDescription__destroy(
type_info_with_registrations.type_info.type_description);
Expand Down
10 changes: 6 additions & 4 deletions rcl/src/rcl/type_description_conversions.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,12 @@ rcl_convert_type_source_sequence_runtime_to_msg(
goto fail;
}
// raw_file_contents
if (!rosidl_runtime_c__String__copy(
&(runtime_type_sources->data[i].raw_file_contents), &(out->data[i].raw_file_contents)))
{
goto fail;
if (runtime_type_sources->data[i].raw_file_contents.size > 0) {
if (!rosidl_runtime_c__String__copy(
&(runtime_type_sources->data[i].raw_file_contents), &(out->data[i].raw_file_contents)))
{
goto fail;
}
}
}

Expand Down
38 changes: 34 additions & 4 deletions rcl/test/rcl/test_type_description_conversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
#include "rosidl_runtime_c/message_type_support_struct.h"
#include "rosidl_runtime_c/type_description/type_description__functions.h"
#include "rosidl_runtime_c/type_description/type_source__functions.h"
#include "test_msgs/msg/basic_types.h"
#include "test_msgs/msg/constants.h"
#include "test_msgs/srv/basic_types.h"

TEST(TestTypeDescriptionConversions, type_description_conversion_round_trip) {
const rosidl_message_type_support_t * ts =
ROSIDL_GET_MSG_TYPE_SUPPORT(test_msgs, msg, BasicTypes);
ROSIDL_GET_MSG_TYPE_SUPPORT(test_msgs, msg, Constants);

type_description_interfaces__msg__TypeDescription * type_description_msg =
rcl_convert_type_description_runtime_to_msg(ts->get_type_description_func(ts));
Expand All @@ -49,16 +50,35 @@ TEST(TestTypeDescriptionConversions, type_description_invalid_input) {

TEST(TestTypeDescriptionConversions, type_source_sequence_conversion_round_trip) {
const rosidl_message_type_support_t * ts =
ROSIDL_GET_MSG_TYPE_SUPPORT(test_msgs, msg, BasicTypes);
ROSIDL_GET_MSG_TYPE_SUPPORT(test_msgs, msg, Constants);

const rosidl_runtime_c__type_description__TypeSource__Sequence * original_sources =
ts->get_type_description_sources_func(ts);
type_description_interfaces__msg__TypeSource__Sequence * type_sources_msg =
rcl_convert_type_source_sequence_runtime_to_msg(ts->get_type_description_sources_func(ts));
rcl_convert_type_source_sequence_runtime_to_msg(original_sources);
EXPECT_TRUE(NULL != type_sources_msg);
ASSERT_EQ(1, type_sources_msg->size);
{
auto source = type_sources_msg->data[0];
std::string type_name = source.type_name.data;
EXPECT_GT(source.raw_file_contents.size, 0);
std::string encoding = source.encoding.data;
EXPECT_EQ(encoding, "msg");
}

rosidl_runtime_c__type_description__TypeSource__Sequence * type_sources_rt =
rcl_convert_type_source_sequence_msg_to_runtime(type_sources_msg);
EXPECT_TRUE(NULL != type_sources_rt);

ASSERT_EQ(1, type_sources_rt->size);
{
auto source = type_sources_rt->data[0];
std::string type_name = source.type_name.data;
EXPECT_GT(source.raw_file_contents.size, 0);
std::string encoding = source.encoding.data;
EXPECT_EQ(encoding, "msg");
}

EXPECT_TRUE(
rosidl_runtime_c__type_description__TypeSource__Sequence__are_equal(
type_sources_rt, ts->get_type_description_sources_func(ts)));
Expand All @@ -69,6 +89,16 @@ TEST(TestTypeDescriptionConversions, type_source_sequence_conversion_round_trip)
type_sources_rt);
}

TEST(TestTypeDescriptionConversions, actually_empty_sources_ok) {
// Implicit definition will always be empty
const rosidl_message_type_support_t * ts =
ROSIDL_GET_MSG_TYPE_SUPPORT(test_msgs, srv, BasicTypes_Request);

const auto * sources = ts->get_type_description_sources_func(ts);
auto * msg = rcl_convert_type_source_sequence_runtime_to_msg(sources);
ASSERT_NE(nullptr, msg);
}

TEST(TestTypeDescriptionConversions, type_source_sequence_invalid_input) {
EXPECT_TRUE(NULL == rcl_convert_type_source_sequence_msg_to_runtime(NULL));
EXPECT_TRUE(NULL == rcl_convert_type_source_sequence_runtime_to_msg(NULL));
Expand Down

0 comments on commit a9e72c5

Please sign in to comment.