Skip to content

Commit 9c749ed

Browse files
authored
Fix/schema traversal mismatch - CT-541 (#1352)
* Fix bug in schema traversal where in some cases we were not returning the value. * Added test
1 parent 56ed450 commit 9c749ed

File tree

2 files changed

+109
-4
lines changed

2 files changed

+109
-4
lines changed

packages/memory/test/space-test.ts

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assert, assertEquals, assertExists, assertMatch } from "@std/assert";
22
import { refer } from "merkle-reference";
3-
import { SchemaContext } from "@commontools/runner";
3+
import { JSONSchema, SchemaContext } from "@commontools/runner";
44
import * as Changes from "../changes.ts";
55
import * as Commit from "../commit.ts";
66
import * as Fact from "../fact.ts";
@@ -2013,3 +2013,105 @@ test(
20132013
);
20142014
},
20152015
);
2016+
2017+
test(
2018+
"returns all entries from charm list",
2019+
DB,
2020+
async (session) => {
2021+
const charmSchema = {
2022+
type: "object",
2023+
properties: {
2024+
["$NAME"]: { type: "string" },
2025+
["$UI"]: { type: "object" },
2026+
},
2027+
required: ["$UI", "$NAME"],
2028+
} as const satisfies JSONSchema;
2029+
2030+
const charmListSchema = {
2031+
type: "array",
2032+
items: { ...charmSchema, asCell: true },
2033+
} as const satisfies JSONSchema;
2034+
2035+
const c1 = Fact.assert({
2036+
the,
2037+
of: doc1,
2038+
is: {
2039+
"value": { "$NAME": "test 1", "$UI": { "type": "vnode" } },
2040+
},
2041+
});
2042+
2043+
const c2 = Fact.assert({
2044+
the,
2045+
of: doc2,
2046+
is: {
2047+
"value": { "$NAME": "test 2", "$UI": { "type": "vnode" } },
2048+
},
2049+
});
2050+
2051+
const c_list = Fact.assert({
2052+
the,
2053+
of: doc3,
2054+
is: {
2055+
"value": [
2056+
{
2057+
"space": space.did(),
2058+
"cell": { "/": doc1.slice(3) },
2059+
"path": [],
2060+
"schema": charmSchema,
2061+
},
2062+
{
2063+
"space": space.did(),
2064+
"cell": { "/": doc2.slice(3) },
2065+
"path": [],
2066+
"schema": charmSchema,
2067+
},
2068+
],
2069+
},
2070+
});
2071+
2072+
const tr1 = Transaction.create({
2073+
issuer: alice.did(),
2074+
subject: space.did(),
2075+
changes: Changes.from([c1, c2, c_list]),
2076+
});
2077+
const write1 = await session.transact(tr1);
2078+
assert(write1.ok);
2079+
2080+
const schemaSelector: SchemaSelector = {
2081+
[doc3]: {
2082+
[the]: {
2083+
_: {
2084+
path: [],
2085+
schemaContext: {
2086+
schema: charmListSchema,
2087+
rootSchema: charmListSchema,
2088+
},
2089+
},
2090+
},
2091+
},
2092+
};
2093+
2094+
const result = session.querySchema({
2095+
cmd: "/memory/graph/query",
2096+
iss: alice.did(),
2097+
sub: space.did(),
2098+
args: {
2099+
selectSchema: schemaSelector,
2100+
},
2101+
prf: [],
2102+
});
2103+
2104+
assertExists(
2105+
getResultForDoc(result, space.did(), doc1),
2106+
"doc1 should be in the result",
2107+
);
2108+
assertExists(
2109+
getResultForDoc(result, space.did(), doc2),
2110+
"doc2 should be in the result",
2111+
);
2112+
assertExists(
2113+
getResultForDoc(result, space.did(), doc3),
2114+
"doc3 should be in the result",
2115+
);
2116+
},
2117+
);

packages/runner/src/traverse.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,10 @@ export class SchemaObjectTraverser<K, S> extends BaseObjectTraverser<K, S> {
614614
if (!deepEqual(nextDoc.path, nextSelector!.path)) {
615615
throw new Error("New doc path doesn't match selector path");
616616
}
617-
this.traverseWithSchemaContext(nextDoc, nextSelector!.schemaContext!);
617+
return this.traverseWithSchemaContext(
618+
nextDoc,
619+
nextSelector!.schemaContext!,
620+
);
618621
}
619622
}
620623

@@ -664,7 +667,7 @@ export class SchemaObjectTraverser<K, S> extends BaseObjectTraverser<K, S> {
664667
if (this.isValidType(schemaObj, "array")) {
665668
if (this.tracker.enter(doc.value)) {
666669
try {
667-
this.traverseArrayWithSchema(doc, {
670+
return this.traverseArrayWithSchema(doc, {
668671
schema: schemaObj,
669672
rootSchema: schemaContext.rootSchema,
670673
});
@@ -688,7 +691,7 @@ export class SchemaObjectTraverser<K, S> extends BaseObjectTraverser<K, S> {
688691
} else if (this.isValidType(schemaObj, "object")) {
689692
if (this.tracker.enter(doc.value)) {
690693
try {
691-
this.traverseObjectWithSchema(doc, {
694+
return this.traverseObjectWithSchema(doc, {
692695
schema: schemaObj,
693696
rootSchema: schemaContext.rootSchema,
694697
});

0 commit comments

Comments
 (0)