Skip to content

Commit 2397d58

Browse files
authored
Merge pull request #1325 from Enferlain/fix/1284-list-valued-fields
fix(search): preserve compound requested fields
2 parents de4ec9b + 470c763 commit 2397d58

2 files changed

Lines changed: 50 additions & 33 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3241,6 +3241,27 @@ static int sg_parse_fields(const char *args, const char *out[], int max_out, yyj
32413241
return n;
32423242
}
32433243

3244+
/* Append a property as one compact-output cell. Compound values stay one
3245+
* column by using their compact JSON representation; the cell emitter quotes
3246+
* and escapes that representation as needed. */
3247+
static void sg_toon_property_cell(cbm_sb_t *sb, yyjson_val *v) {
3248+
if (v && yyjson_is_str(v)) {
3249+
cbm_tree_cell_str(sb, yyjson_get_str(v), false);
3250+
} else if (v && yyjson_is_bool(v)) {
3251+
cbm_tree_cell_bool(sb, yyjson_get_bool(v), false);
3252+
} else if (v && yyjson_is_int(v)) {
3253+
cbm_tree_cell_int(sb, yyjson_get_int(v), false);
3254+
} else if (v && yyjson_is_real(v)) {
3255+
cbm_tree_cell_real(sb, yyjson_get_real(v), false);
3256+
} else if (v && !yyjson_is_null(v)) {
3257+
char *json = yyjson_val_write(v, 0, NULL);
3258+
cbm_tree_cell_str(sb, json ? json : "", false);
3259+
free(json);
3260+
} else {
3261+
cbm_tree_cell_str(sb, "", false);
3262+
}
3263+
}
3264+
32443265
/* Append one row's extra-field cells, pulled from the node's properties. */
32453266
static void sg_toon_extra_cells(cbm_sb_t *sb, const char *props_json, const char *const *fields,
32463267
int nfields) {
@@ -3249,17 +3270,7 @@ static void sg_toon_extra_cells(cbm_sb_t *sb, const char *props_json, const char
32493270
yyjson_val *pr = pd ? yyjson_doc_get_root(pd) : NULL;
32503271
for (int f = 0; f < nfields; f++) {
32513272
yyjson_val *v = (pr && yyjson_is_obj(pr)) ? yyjson_obj_get(pr, fields[f]) : NULL;
3252-
if (v && yyjson_is_str(v)) {
3253-
cbm_tree_cell_str(sb, yyjson_get_str(v), false);
3254-
} else if (v && yyjson_is_bool(v)) {
3255-
cbm_tree_cell_bool(sb, yyjson_get_bool(v), false);
3256-
} else if (v && yyjson_is_int(v)) {
3257-
cbm_tree_cell_int(sb, yyjson_get_int(v), false);
3258-
} else if (v && yyjson_is_real(v)) {
3259-
cbm_tree_cell_real(sb, yyjson_get_real(v), false);
3260-
} else {
3261-
cbm_tree_cell_str(sb, "", false);
3262-
}
3273+
sg_toon_property_cell(sb, v);
32633274
}
32643275
if (pd) {
32653276
yyjson_doc_free(pd);
@@ -3391,17 +3402,7 @@ static void emit_search_results_tree(cbm_sb_t *sb, cbm_search_output_t *out, int
33913402
yyjson_val *pr = pd ? yyjson_doc_get_root(pd) : NULL;
33923403
for (int f = 0; f < nfields; f++) {
33933404
yyjson_val *v = (pr && yyjson_is_obj(pr)) ? yyjson_obj_get(pr, fields[f]) : NULL;
3394-
if (v && yyjson_is_str(v)) {
3395-
cbm_tree_cell_str(sb, yyjson_get_str(v), false);
3396-
} else if (v && yyjson_is_int(v)) {
3397-
cbm_tree_cell_int(sb, yyjson_get_int(v), false);
3398-
} else if (v && yyjson_is_real(v)) {
3399-
cbm_tree_cell_real(sb, yyjson_get_real(v), false);
3400-
} else if (v && yyjson_is_bool(v)) {
3401-
cbm_tree_cell_bool(sb, yyjson_get_bool(v), false);
3402-
} else {
3403-
cbm_tree_cell_str(sb, "", false); /* emits "-" */
3404-
}
3405+
sg_toon_property_cell(sb, v);
34053406
}
34063407
if (pd) {
34073408
yyjson_doc_free(pd);
@@ -3483,14 +3484,10 @@ static void emit_search_results_tree_json(yyjson_mut_doc *doc, yyjson_mut_val *r
34833484
yyjson_val *pr = pd ? yyjson_doc_get_root(pd) : NULL;
34843485
for (int f = 0; f < nfields; f++) {
34853486
yyjson_val *v = (pr && yyjson_is_obj(pr)) ? yyjson_obj_get(pr, fields[f]) : NULL;
3486-
if (v && yyjson_is_str(v)) {
3487-
yyjson_mut_arr_add_strcpy(doc, row, yyjson_get_str(v));
3488-
} else if (v && yyjson_is_int(v)) {
3489-
yyjson_mut_arr_add_int(doc, row, yyjson_get_int(v));
3490-
} else if (v && yyjson_is_real(v)) {
3491-
yyjson_mut_arr_add_real(doc, row, yyjson_get_real(v));
3492-
} else if (v && yyjson_is_bool(v)) {
3493-
yyjson_mut_arr_add_bool(doc, row, yyjson_get_bool(v));
3487+
yyjson_mut_val *copy =
3488+
(v && !yyjson_is_null(v)) ? yyjson_val_mut_copy(doc, v) : NULL;
3489+
if (copy) {
3490+
yyjson_mut_arr_add_val(row, copy);
34943491
} else {
34953492
yyjson_mut_arr_add_null(doc, row);
34963493
}

‎tests/test_mcp.c‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,15 +2084,32 @@ TEST(tool_search_graph_includes_node_properties) {
20842084
free(inner);
20852085
free(resp);
20862086

2087+
/* List-valued fields are compact JSON in text output, rather than an
2088+
* empty placeholder. */
2089+
resp = cbm_mcp_server_handle(
2090+
srv, "{\"jsonrpc\":\"2.0\",\"id\":431,\"method\":\"tools/call\","
2091+
"\"params\":{\"name\":\"search_graph\","
2092+
"\"arguments\":{\"project\":\"test-project\",\"label\":\"Function\","
2093+
"\"name_pattern\":\"HandleRequest\",\"fields\":[\"base_classes\"],"
2094+
"\"limit\":5}}}");
2095+
ASSERT_NOT_NULL(resp);
2096+
inner = extract_text_content(resp);
2097+
ASSERT_NOT_NULL(inner);
2098+
ASSERT_NOT_NULL(strstr(inner, "base_classes"));
2099+
ASSERT_NOT_NULL(strstr(inner, "HandlerBase"));
2100+
ASSERT_NOT_NULL(strstr(inner, "Audited"));
2101+
free(inner);
2102+
free(resp);
2103+
20872104
/* format:"json" = json-stringified tree: same grouped model, column-
20882105
* ordered row arrays — never per-row key envelopes or property blobs.
2089-
* fields adds columns there too. */
2106+
* fields adds columns there too and preserves compound JSON types. */
20902107
resp = cbm_mcp_server_handle(
20912108
srv, "{\"jsonrpc\":\"2.0\",\"id\":44,\"method\":\"tools/call\","
20922109
"\"params\":{\"name\":\"search_graph\","
20932110
"\"arguments\":{\"project\":\"test-project\",\"label\":\"Function\","
20942111
"\"name_pattern\":\"HandleRequest\",\"format\":\"json\","
2095-
"\"fields\":[\"signature\"],\"limit\":5}}}");
2112+
"\"fields\":[\"signature\",\"base_classes\"],\"limit\":5}}}");
20962113
ASSERT_NOT_NULL(resp);
20972114
inner = extract_text_content(resp);
20982115
ASSERT_NOT_NULL(inner);
@@ -2101,6 +2118,8 @@ TEST(tool_search_graph_includes_node_properties) {
21012118
ASSERT_NOT_NULL(strstr(inner, "\"rows\""));
21022119
ASSERT_NOT_NULL(strstr(inner, "\"signature\"")); /* requested column */
21032120
ASSERT_NOT_NULL(strstr(inner, "func HandleRequest")); /* its value */
2121+
ASSERT_NOT_NULL(strstr(inner, "\"base_classes\""));
2122+
ASSERT_NOT_NULL(strstr(inner, "[\"HandlerBase\",\"Audited\"]"));
21042123
ASSERT_NULL(strstr(inner, "is_exported")); /* blob never spills */
21052124
free(inner);
21062125
free(resp);
@@ -7411,7 +7430,8 @@ static cbm_mcp_server_t *setup_snippet_server(char *tmp_dir, size_t tmp_sz) {
74117430
n_hr.end_line = 5;
74127431
n_hr.properties_json = "{\"signature\":\"func HandleRequest() error\","
74137432
"\"return_type\":\"error\","
7414-
"\"is_exported\":true}";
7433+
"\"is_exported\":true,"
7434+
"\"base_classes\":[\"HandlerBase\",\"Audited\"]}";
74157435
int64_t id_hr = cbm_store_upsert_node(st, &n_hr);
74167436

74177437
cbm_node_t n_po = {0};

0 commit comments

Comments
 (0)