Skip to content

Fixes JsonPathDetector path for empty objects #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2024
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
52 changes: 51 additions & 1 deletion src/__tests__/path-detector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,54 @@ test("PathDetector adds path", async () => {
{ ...objectEnd(), path: ["object"] },
{ ...objectEnd(), path: [] }
]);
});
});

test("PathDetector handles empty object", async () => {
const stream = serializeJsonValue({
outer: {
inner: {}
}
}).pipeThrough(new JsonPathDetector());

expect(await streamToArray(stream)).toEqual([
{ ...objectStart(), path: [] },
{ ...stringStart(StringRole.KEY), path: [] },
{ ...stringChunk("outer", StringRole.KEY), path: [] },
{ ...stringEnd(StringRole.KEY), path: [] },
{ ...colon(), path: [] },
{ ...objectStart(), path: ["outer"] },
{ ...stringStart(StringRole.KEY), path: ["outer"] },
{ ...stringChunk("inner", StringRole.KEY), path: ["outer"] },
{ ...stringEnd(StringRole.KEY), path: ["outer"] },
{ ...colon(), path: ["outer"] },
{ ...objectStart(), path: ["outer", "inner"] },
{ ...objectEnd(), path: ["outer", "inner"] },
{ ...objectEnd(), path: ["outer"] },
{ ...objectEnd(), path: [] }
]);
});

test("PathDetector handles empty array", async () => {
const stream = serializeJsonValue({
outer: {
inner: []
}
}).pipeThrough(new JsonPathDetector());

expect(await streamToArray(stream)).toEqual([
{ ...objectStart(), path: [] },
{ ...stringStart(StringRole.KEY), path: [] },
{ ...stringChunk("outer", StringRole.KEY), path: [] },
{ ...stringEnd(StringRole.KEY), path: [] },
{ ...colon(), path: [] },
{ ...objectStart(), path: ["outer"] },
{ ...stringStart(StringRole.KEY), path: ["outer"] },
{ ...stringChunk("inner", StringRole.KEY), path: ["outer"] },
{ ...stringEnd(StringRole.KEY), path: ["outer"] },
{ ...colon(), path: ["outer"] },
{ ...arrayStart(), path: ["outer", "inner"] },
{ ...arrayEnd(), path: ["outer", "inner"] },
{ ...objectEnd(), path: ["outer"] },
{ ...objectEnd(), path: [] }
]);
});
5 changes: 3 additions & 2 deletions src/json-path-detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ export class JsonPathDetector extends AbstractTransformStream<JsonChunk, JsonChu
} else if (chunk.type === JsonChunkType.ARRAY_START) {
this.stack.push({ type: "array", state: "next", key: 0 });
} else if (chunk.type === JsonChunkType.OBJECT_END || chunk.type === JsonChunkType.ARRAY_END) {
this.stack.pop();
this.path.pop();
if (this.stack.pop()?.state !== "pending") {
this.path.pop();
}
} else {
const current = this.stack[this.stack.length - 1];
if (current?.type === "object") {
Expand Down