| 199 | } |
| 200 | |
| 201 | void cbm_extract_openapi(CBMExtractCtx *ctx) { |
| 202 | if (!ctx || !ctx->result || !ctx->source || ts_node_is_null(ctx->root) || |
| 203 | (ctx->language != CBM_LANG_OPENAPI && ctx->language != CBM_LANG_YAML && |
| 204 | ctx->language != CBM_LANG_JSON)) { |
| 205 | return; |
| 206 | } |
| 207 | TSNode root = oa_mapping(ctx->root); |
| 208 | if (ts_node_is_null(oa_get(ctx, root, "openapi")) && |
| 209 | ts_node_is_null(oa_get(ctx, root, "swagger"))) { |
| 210 | return; /* no API description */ |
| 211 | } |
| 212 | TSNode paths = oa_mapping(oa_get(ctx, root, "paths")); |
| 213 | char path[OA_SCALAR_MAX]; |
| 214 | char key[OA_SCALAR_MAX]; |
| 215 | uint32_t np = ts_node_is_null(paths) ? 0 : ts_node_named_child_count(paths); |
| 216 | for (uint32_t i = 0; i < np; i++) { |
| 217 | TSNode pp = ts_node_named_child(paths, i); |
| 218 | if (!oa_pair(pp) || |
| 219 | !oa_scalar(ctx, ts_node_child_by_field_name(pp, "key", CBM_SZ_3), path, sizeof(path)) || |
| 220 | path[0] != '/') { |
| 221 | continue; |
| 222 | } |
| 223 | TSNode item = oa_mapping(ts_node_child_by_field_name(pp, "value", CBM_SZ_5)); |
| 224 | uint32_t nm = ts_node_is_null(item) ? 0 : ts_node_named_child_count(item); |
| 225 | for (uint32_t j = 0; j < nm; j++) { |
| 226 | TSNode mp = ts_node_named_child(item, j); |
| 227 | if (!oa_pair(mp) || |
| 228 | !oa_scalar(ctx, ts_node_child_by_field_name(mp, "key", CBM_SZ_3), key, |
| 229 | sizeof(key)) || |
| 230 | !oa_method(key)) { |
| 231 | continue; |
| 232 | } |
| 233 | TSNode op = oa_mapping(ts_node_child_by_field_name(mp, "value", CBM_SZ_5)); |
| 234 | if (!ts_node_is_null(op)) { |
| 235 | oa_operation(ctx, path, key, op, item, mp); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
no test coverage detected