Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/gentle-moles-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@guardian/cql": patch
---

Fix `queryChange`'s serialised `queryStr` dropping quotes from a plain phrase containing a reserved character (`:`, `(`, `)`, or `"`) with no whitespace, e.g. `"hello:world"`. Previously only whitespace triggered re-quoting for plain phrases, while chip keys/values already correctly checked reserved characters too — the two now use the same predicate (`shouldQuoteFieldValue`). Without this fix, a quoted phrase containing a colon is silently corrupted into an unquoted string, which then gets mis-parsed as a `key:value` chip the next time it's read.
27 changes: 27 additions & 0 deletions lib/cql/src/lang/interpreter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@ describe("interpreter", () => {
expect(str).toBe(queryStr);
});

it("should quote a plain string containing a reserved char (colon)", () => {
const queryStr = `"hello:world"`;
const query = parser(queryStr).queryAst!;

const str = cqlQueryStrFromQueryAst(query);

expect(str).toBe(queryStr);
});

it("should quote a plain string containing a reserved char (parens)", () => {
const queryStr = `"(parenthetical)"`;
const query = parser(queryStr).queryAst!;

const str = cqlQueryStrFromQueryAst(query);

expect(str).toBe(queryStr);
});

it("should not quote a plain string with no reserved chars or whitespace", () => {
const queryStr = `hello`;
const query = parser(queryStr).queryAst!;

const str = cqlQueryStrFromQueryAst(query);

expect(str).toBe(queryStr);
});

it("should escape reserved characters in chip keys and values", () => {
const queryStr = `key:"\\"value\\""`;
const query = parser(queryStr).queryAst!;
Expand Down
4 changes: 2 additions & 2 deletions lib/cql/src/lang/interpreter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CqlBinary, CqlExpr, CqlField, CqlQuery } from "./ast";
import { hasWhitespace, shouldQuoteFieldValue } from "./utils";
import { shouldQuoteFieldValue } from "./utils";

export const cqlQueryStrFromQueryAst = (query: CqlQuery): string => {
const { content } = query;
Expand All @@ -17,7 +17,7 @@ const strFromExpr = (queryExpr: CqlExpr): string | undefined => {
const renderedContent = (() => {
switch (content.type) {
case "CqlStr":
return hasWhitespace(content.searchExpr)
return shouldQuoteFieldValue(content.searchExpr)
? `"${content.searchExpr}"`
: content.searchExpr;
case "CqlGroup":
Expand Down
Loading