Skip to content

Commit

Permalink
Fix JSON serializer issue where serialize_alerts but alerts module is…
Browse files Browse the repository at this point in the history
… not imported
  • Loading branch information
SanderMertens committed Aug 4, 2023
1 parent e996463 commit 88bcbc3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
6 changes: 5 additions & 1 deletion flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -35133,6 +35133,10 @@ int flecs_json_serialize_alerts(
(void)entity;

#ifdef FLECS_ALERTS
if (!ecs_id(EcsAlertsActive)) {
return 0; /* Alert module not imported */
}

flecs_json_memberl(buf, "alerts");
flecs_json_array_push(buf);
const EcsAlertsActive *alerts = ecs_get(world, entity, EcsAlertsActive);
Expand Down Expand Up @@ -51146,7 +51150,7 @@ int flecs_term_populate_from_id(

ecs_entity_t term_first = flecs_term_id_get_entity(&term->first);
if (term_first) {
if ((uint32_t)term_first != first) {
if ((uint32_t)term_first != (uint32_t)first) {
flecs_filter_error(ctx, "mismatch between term.id and term.first");
return -1;
}
Expand Down
4 changes: 4 additions & 0 deletions src/addons/json/serialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,10 @@ int flecs_json_serialize_alerts(
(void)entity;

#ifdef FLECS_ALERTS
if (!ecs_id(EcsAlertsActive)) {
return 0; /* Alert module not imported */
}

flecs_json_memberl(buf, "alerts");
flecs_json_array_push(buf);
const EcsAlertsActive *alerts = ecs_get(world, entity, EcsAlertsActive);
Expand Down
1 change: 1 addition & 0 deletions test/meta/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@
"serialize_entity_w_2_alerts",
"serialize_entity_w_child_alerts",
"serialize_entity_w_severity_filter_alert",
"serialize_entity_w_alerts_not_imported",
"serialize_entity_refs_childof",
"serialize_entity_refs_custom",
"serialize_entity_refs_wildcard",
Expand Down
24 changes: 24 additions & 0 deletions test/meta/src/SerializeToJson.c
Original file line number Diff line number Diff line change
Expand Up @@ -2504,6 +2504,29 @@ void SerializeToJson_serialize_entity_w_severity_filter_alert() {
ecs_fini(world);
}

void SerializeToJson_serialize_entity_w_alerts_not_imported() {
ecs_world_t *world = ecs_init();

ECS_COMPONENT(world, Position);

ecs_entity_t e1 = ecs_new_entity(world, "e1");
ecs_set(world, e1, Position, {10, 20});
ecs_new_w_pair(world, EcsChildOf, e1);

ecs_entity_to_json_desc_t desc = ECS_ENTITY_TO_JSON_INIT;
desc.serialize_alerts = true;
char *json = ecs_entity_to_json(world, e1, &desc);
test_assert(json != NULL);

test_str(json, "{"
"\"path\":\"e1\", "
"\"ids\":[[\"Position\"]]"
"}");
ecs_os_free(json);

ecs_fini(world);
}

void SerializeToJson_serialize_entity_refs_childof() {
ecs_world_t *world = ecs_init();

Expand Down Expand Up @@ -4958,3 +4981,4 @@ void SerializeToJson_serialize_anonymous_entities_w_offset() {

ecs_fini(world);
}

7 changes: 6 additions & 1 deletion test/meta/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ void SerializeToJson_serialize_entity_w_1_alert(void);
void SerializeToJson_serialize_entity_w_2_alerts(void);
void SerializeToJson_serialize_entity_w_child_alerts(void);
void SerializeToJson_serialize_entity_w_severity_filter_alert(void);
void SerializeToJson_serialize_entity_w_alerts_not_imported(void);
void SerializeToJson_serialize_entity_refs_childof(void);
void SerializeToJson_serialize_entity_refs_custom(void);
void SerializeToJson_serialize_entity_refs_wildcard(void);
Expand Down Expand Up @@ -3581,6 +3582,10 @@ bake_test_case SerializeToJson_testcases[] = {
"serialize_entity_w_severity_filter_alert",
SerializeToJson_serialize_entity_w_severity_filter_alert
},
{
"serialize_entity_w_alerts_not_imported",
SerializeToJson_serialize_entity_w_alerts_not_imported
},
{
"serialize_entity_refs_childof",
SerializeToJson_serialize_entity_refs_childof
Expand Down Expand Up @@ -4754,7 +4759,7 @@ static bake_test_suite suites[] = {
"SerializeToJson",
NULL,
NULL,
139,
140,
SerializeToJson_testcases
},
{
Expand Down

0 comments on commit 88bcbc3

Please sign in to comment.