mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-11-16 21:51:13 +01:00
Fix JSON breadcrumb filters for literal keys (#689)
This commit is contained in:
1 parent
fccff5b7db
commit
1e3dfff01e
7 files changed
+211
-40
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import type { PluginDefinition } from "@yaakapp/api";
|
||||
import { jsonPathToSegments } from "@yaakapp-internal/lib/jsonPath";
|
||||
import { JSONPath } from "jsonpath-plus";
|
||||
|
||||
export const plugin: PluginDefinition = {
|
||||
@@ -8,7 +9,24 @@ export const plugin: PluginDefinition = {
|
||||
onFilter(_ctx, args) {
|
||||
const parsed = JSON.parse(args.payload);
|
||||
try {
|
||||
const filtered = JSONPath({ path: args.filter, json: parsed });
|
||||
// jsonpath-plus doesn't decode JSON-quoted keys reliably and can treat
|
||||
// their contents as operators. Only complete literal paths take this
|
||||
// route; all other expressions retain the library's existing behavior.
|
||||
const segments = args.filter.includes('["') ? jsonPathToSegments(args.filter) : null;
|
||||
let filtered: unknown;
|
||||
if (segments != null) {
|
||||
let value: unknown = parsed;
|
||||
for (const segment of segments) {
|
||||
const key = segment.kind === "key" ? segment.key : segment.index;
|
||||
if (value == null || !Object.prototype.hasOwnProperty.call(value, key)) {
|
||||
return { content: "[]" };
|
||||
}
|
||||
value = (value as Record<string, unknown>)[key];
|
||||
}
|
||||
filtered = [value];
|
||||
} else {
|
||||
filtered = JSONPath({ path: args.filter, json: parsed });
|
||||
}
|
||||
return { content: JSON.stringify(filtered, null, 2) };
|
||||
} catch (err) {
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user