Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/tough-cups-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@stackables/bridge": patch
"@stackables/bridge-parser": patch
---

Fix chained `||` literal fallback parsing so authored left-to-right short-circuiting is preserved after safe pulls (`?.`), and add regression coverage for mixed `||` + `??` chains.
5 changes: 5 additions & 0 deletions packages/bridge-parser/src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5237,13 +5237,18 @@ function buildBridgeBody(

let falsyFallback: string | undefined;
let falsyControl: ControlFlowInstruction | undefined;
let hasTruthyLiteralFallback = false;
for (const alt of subs(wireNode, "nullAlt")) {
if (hasTruthyLiteralFallback) break;
const altResult = extractCoalesceAlt(alt, lineNum);
if ("literal" in altResult) {
falsyFallback = altResult.literal;
hasTruthyLiteralFallback = Boolean(JSON.parse(altResult.literal));
} else if ("control" in altResult) {
falsyFallback = undefined;
falsyControl = altResult.control;
} else {
falsyFallback = undefined;
sourceParts.push({ ref: altResult.sourceRef, isPipeFork: false });
}
}
Expand Down
44 changes: 44 additions & 0 deletions packages/bridge/test/coalesce-cost.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,50 @@ bridge Query.lookup {
assert.equal(data.label, "fallback");
});

test("?. with chained || literals short-circuits at first truthy literal", async () => {
const doc = parseBridge(`version 1.5
const lorem = {
"ipsum":"dolor sit amet",
"consetetur":8.9
}

bridge Query.lookup {
with const
with output as o

o.label <- const.lorem.ipsums?.kala || "A" || "B"
}`);
const gateway = createGateway(typeDefs, doc, { tools: {} });
const executor = buildHTTPExecutor({ fetch: gateway.fetch as any });

const result: any = await executor({
document: parse(`{ lookup(q: "x") { label } }`),
});
assert.equal(result.data.lookup.label, "A");
});

test("mixed || and ?? remains left-to-right with first truthy || winner", async () => {
const doc = parseBridge(`version 1.5
const lorem = {
"ipsum": "dolor sit amet",
"consetetur": 8.9
}

bridge Query.lookup {
with const
with output as o

o.label <- const.lorem.kala || const.lorem.ipsums?.mees || "B" ?? "C"
}`);
const gateway = createGateway(typeDefs, doc, { tools: {} });
const executor = buildHTTPExecutor({ fetch: gateway.fetch as any });

const result: any = await executor({
document: parse(`{ lookup(q: "x") { label } }`),
});
assert.equal(result.data.lookup.label, "B");
});

test("?. passes through value when tool succeeds", async () => {
const { data } = await run(
`version 1.5
Expand Down
Loading