From 0493d6660dcf46da8215467b425637ac73fe4ca9 Mon Sep 17 00:00:00 2001 From: Sander Mertens Date: Thu, 20 Jun 2024 18:35:18 -0700 Subject: [PATCH] Don't throw REST errors when 'try' is set to true --- flecs.c | 16 ++++++++++------ src/addons/json/serialize_iter_result_query.c | 4 +++- src/addons/rest.c | 12 +++++++----- test/addons/src/Rest.c | 10 +++++----- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/flecs.c b/flecs.c index 94d38c878..d153d9edb 100644 --- a/flecs.c +++ b/flecs.c @@ -25400,7 +25400,8 @@ bool flecs_rest_script( flecs_rest_bool_param(req, "try", &try); bool prev_color = ecs_log_enable_colors(false); - rest_prev_log = ecs_os_api.log_; + ecs_os_api_log_t prev_log = ecs_os_api.log_; + rest_prev_log = try ? NULL : prev_log; ecs_os_api.log_ = flecs_rest_capture_log; script = ecs_script(world, { @@ -25423,7 +25424,7 @@ bool flecs_rest_script( ecs_os_free(err); } - ecs_os_api.log_ = rest_prev_log; + ecs_os_api.log_ = prev_log; ecs_log_enable_colors(prev_color); return true; @@ -25556,7 +25557,7 @@ bool flecs_rest_get_query( const char *expr = ecs_http_get_param(req, "expr"); if (!expr) { - ecs_strbuf_appendlit(&reply->body, "Missing parameter 'q'"); + ecs_strbuf_appendlit(&reply->body, "Missing parameter 'expr'"); reply->code = 400; /* bad request */ return true; } @@ -25566,7 +25567,8 @@ bool flecs_rest_get_query( ecs_dbg_2("rest: request query '%s'", expr); bool prev_color = ecs_log_enable_colors(false); - rest_prev_log = ecs_os_api.log_; + ecs_os_api_log_t prev_log = ecs_os_api.log_; + rest_prev_log = try ? NULL : prev_log; ecs_os_api.log_ = flecs_rest_capture_log; ecs_query_t *q = ecs_query(world, { .expr = expr }); @@ -25582,7 +25584,7 @@ bool flecs_rest_get_query( ecs_query_fini(q); } - ecs_os_api.log_ = rest_prev_log; + ecs_os_api.log_ = prev_log; ecs_log_enable_colors(prev_color); return true; @@ -43480,7 +43482,7 @@ bool flecs_json_serialize_iter_result_ids( ecs_world_t *world = it->world; int16_t f, field_count = flecs_ito(int16_t, it->field_count); - int16_t field_mask = flecs_ito(int16_t, (1u << field_count) - 1); + int16_t field_mask = flecs_ito(int16_t, (1 << field_count) - 1); if (q->static_id_fields == field_mask) { /* All matched ids are static, nothing to serialize */ return false; @@ -43587,6 +43589,7 @@ bool flecs_json_serialize_common_for_table( return result; } +static int flecs_json_serialize_iter_result_field_values( const ecs_world_t *world, const ecs_iter_t *it, @@ -43696,6 +43699,7 @@ int flecs_json_serialize_iter_result_query( if (flecs_json_serialize_iter_result_field_values( world, it, i, buf, desc, ser_ctx)) { + ecs_os_free(common_data); return -1; } } diff --git a/src/addons/json/serialize_iter_result_query.c b/src/addons/json/serialize_iter_result_query.c index 3b64f739b..c032e0780 100644 --- a/src/addons/json/serialize_iter_result_query.c +++ b/src/addons/json/serialize_iter_result_query.c @@ -46,7 +46,7 @@ bool flecs_json_serialize_iter_result_ids( ecs_world_t *world = it->world; int16_t f, field_count = flecs_ito(int16_t, it->field_count); - int16_t field_mask = flecs_ito(int16_t, (1u << field_count) - 1); + int16_t field_mask = flecs_ito(int16_t, (1 << field_count) - 1); if (q->static_id_fields == field_mask) { /* All matched ids are static, nothing to serialize */ return false; @@ -153,6 +153,7 @@ bool flecs_json_serialize_common_for_table( return result; } +static int flecs_json_serialize_iter_result_field_values( const ecs_world_t *world, const ecs_iter_t *it, @@ -262,6 +263,7 @@ int flecs_json_serialize_iter_result_query( if (flecs_json_serialize_iter_result_field_values( world, it, i, buf, desc, ser_ctx)) { + ecs_os_free(common_data); return -1; } } diff --git a/src/addons/rest.c b/src/addons/rest.c index 96a262878..5f0f68c40 100644 --- a/src/addons/rest.c +++ b/src/addons/rest.c @@ -506,7 +506,8 @@ bool flecs_rest_script( flecs_rest_bool_param(req, "try", &try); bool prev_color = ecs_log_enable_colors(false); - rest_prev_log = ecs_os_api.log_; + ecs_os_api_log_t prev_log = ecs_os_api.log_; + rest_prev_log = try ? NULL : prev_log; ecs_os_api.log_ = flecs_rest_capture_log; script = ecs_script(world, { @@ -529,7 +530,7 @@ bool flecs_rest_script( ecs_os_free(err); } - ecs_os_api.log_ = rest_prev_log; + ecs_os_api.log_ = prev_log; ecs_log_enable_colors(prev_color); return true; @@ -662,7 +663,7 @@ bool flecs_rest_get_query( const char *expr = ecs_http_get_param(req, "expr"); if (!expr) { - ecs_strbuf_appendlit(&reply->body, "Missing parameter 'q'"); + ecs_strbuf_appendlit(&reply->body, "Missing parameter 'expr'"); reply->code = 400; /* bad request */ return true; } @@ -672,7 +673,8 @@ bool flecs_rest_get_query( ecs_dbg_2("rest: request query '%s'", expr); bool prev_color = ecs_log_enable_colors(false); - rest_prev_log = ecs_os_api.log_; + ecs_os_api_log_t prev_log = ecs_os_api.log_; + rest_prev_log = try ? NULL : prev_log; ecs_os_api.log_ = flecs_rest_capture_log; ecs_query_t *q = ecs_query(world, { .expr = expr }); @@ -688,7 +690,7 @@ bool flecs_rest_get_query( ecs_query_fini(q); } - ecs_os_api.log_ = rest_prev_log; + ecs_os_api.log_ = prev_log; ecs_log_enable_colors(prev_color); return true; diff --git a/test/addons/src/Rest.c b/test/addons/src/Rest.c index 5d056b8ec..55e10e152 100644 --- a/test/addons/src/Rest.c +++ b/test/addons/src/Rest.c @@ -129,7 +129,7 @@ void Rest_try_query(void) { { ecs_http_reply_t reply = ECS_HTTP_REPLY_INIT; test_int(-1, ecs_http_server_request(srv, "GET", - "/query?q=Foo", &reply)); + "/query?expr=Foo", &reply)); test_int(reply.code, 400); // No try, should error ecs_strbuf_reset(&reply.body); } @@ -137,7 +137,7 @@ void Rest_try_query(void) { { ecs_http_reply_t reply = ECS_HTTP_REPLY_INIT; test_int(0, ecs_http_server_request(srv, "GET", - "/query?q=Foo&try=true", &reply)); + "/query?expr=Foo&try=true", &reply)); test_int(reply.code, 200); // With try, should not error ecs_strbuf_reset(&reply.body); } @@ -160,13 +160,13 @@ void Rest_query(void) { ecs_http_reply_t reply = ECS_HTTP_REPLY_INIT; test_int(0, ecs_http_server_request(srv, "GET", - "/query?q=Position", &reply)); + "/query?expr=Position", &reply)); test_int(reply.code, 200); char *reply_str = ecs_strbuf_get(&reply.body); test_assert(reply_str != NULL); test_str(reply_str, - "{\"results\":[{\"name\":\"e\", \"fields\":[{}]}]}"); + "{\"results\":[{\"name\":\"e\", \"fields\":{\"values\":[0]}}]}"); ecs_os_free(reply_str); ecs_rest_server_fini(srv); @@ -200,7 +200,7 @@ void Rest_named_query(void) { char *reply_str = ecs_strbuf_get(&reply.body); test_assert(reply_str != NULL); test_str(reply_str, - "{\"results\":[{\"name\":\"e\", \"fields\":[{}]}]}"); + "{\"results\":[{\"name\":\"e\", \"fields\":{\"values\":[0]}}]}"); ecs_os_free(reply_str); ecs_rest_server_fini(srv);