From 5d691e5df482fdbb7842713cd9cf387c4985b7b4 Mon Sep 17 00:00:00 2001 From: Guy Royse Date: Wed, 26 Jul 2023 14:38:13 -0400 Subject: [PATCH 1/2] escaped additional characters --- lib/search/where-field.ts | 10 ++++++++-- lib/search/where-string-array.ts | 3 +-- lib/search/where-string.ts | 2 +- lib/search/where-text.ts | 3 +-- spec/unit/search/search-by-string-array.spec.ts | 8 ++++---- spec/unit/search/search-by-string.spec.ts | 6 +++--- spec/unit/search/search-by-text.spec.ts | 8 ++++---- 7 files changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/search/where-field.ts b/lib/search/where-field.ts index 0a2c6e2..7905298 100644 --- a/lib/search/where-field.ts +++ b/lib/search/where-field.ts @@ -310,13 +310,19 @@ export abstract class WhereField { /** @internal */ protected buildQuery(valuePortion: string): string { const negationPortion = this.negated ? '-' : '' - const fieldPortion = this.escapePunctuation(this.field.name) + const fieldPortion = this.escapePunctuationAndSpaces(this.field.name) return `(${negationPortion}@${fieldPortion}:${valuePortion})` } /** @internal */ protected escapePunctuation(value: string): string { - const matchPunctuation = /[,.<>{}[\]"':;!@#$%^&()\-+=~|/\\ ]/g + const matchPunctuation = /[,.?<>{}[\]"':;!@#$%^&()\-+=~|/\\]/g + return value.replace(matchPunctuation, '\\$&') + } + + /** @internal */ + protected escapePunctuationAndSpaces(value: string): string { + const matchPunctuation = /[,.?<>{}[\]"':;!@#$%^&()\-+=~|/\\ ]/g return value.replace(matchPunctuation, '\\$&') } } diff --git a/lib/search/where-string-array.ts b/lib/search/where-string-array.ts index f5d1804..93102e3 100644 --- a/lib/search/where-string-array.ts +++ b/lib/search/where-string-array.ts @@ -19,8 +19,7 @@ export class WhereStringArray extends WhereField { containOneOf(...value: Array): Search { return this.containsOneOf(...value) } toString(): string { - const matchPunctuation = /[,.<>{}[\]"':;!@#$%^&()\-+=~| ]/g - const escapedValue = this.value.map(s => s.replace(matchPunctuation, '\\$&')).join('|') + const escapedValue = this.value.map(s => this.escapePunctuationAndSpaces(s)).join('|') return this.buildQuery(`{${escapedValue}}`) } } diff --git a/lib/search/where-string.ts b/lib/search/where-string.ts index 0e397aa..40cd1d5 100644 --- a/lib/search/where-string.ts +++ b/lib/search/where-string.ts @@ -24,7 +24,7 @@ export class WhereString extends WhereField { get exactly() { return this.throwMatchExcpetionReturningThis() } toString(): string { - const escapedValue = this.escapePunctuation(this.value) + const escapedValue = this.escapePunctuationAndSpaces(this.value) return this.buildQuery(`{${escapedValue}}`) } diff --git a/lib/search/where-text.ts b/lib/search/where-text.ts index ac818d7..74e9fee 100644 --- a/lib/search/where-text.ts +++ b/lib/search/where-text.ts @@ -36,8 +36,7 @@ export class WhereText extends WhereField { equalTo(_: string | number | boolean): Search { return this.throwEqualsExcpetion() } toString(): string { - const matchPunctuation = /[,.<>{}[\]"':;!@#$%^&()\-+=~|]/g - const escapedValue = this.value.replace(matchPunctuation, '\\$&') + const escapedValue = this.escapePunctuation(this.value) if (this.exactValue) { return this.buildQuery(`"${escapedValue}"`) diff --git a/spec/unit/search/search-by-string-array.spec.ts b/spec/unit/search/search-by-string-array.spec.ts index 22fe6fe..fa0ed4f 100644 --- a/spec/unit/search/search-by-string-array.spec.ts +++ b/spec/unit/search/search-by-string-array.spec.ts @@ -43,13 +43,13 @@ describe("Search", () => { it("generates a query with .does.not.containOneOf", () => expectToBeNegatedContainsOneQuery(where.does.not.containOneOf(A_STRING, ANOTHER_STRING, A_THIRD_STRING))) it("generates a query with .contains that escapes all punctuation", () => { - let query = where.contains(",.<>{}[]\"':;|!@#$%^&()-+=~ ").query - expect(query).toBe("(@someStrings:{\\,\\.\\<\\>\\{\\}\\[\\]\\\"\\'\\:\\;\\|\\!\\@\\#\\$\\%\\^\\&\\(\\)\\-\\+\\=\\~\\ })") + let query = where.contains(",.?<>{}[]\"':;|!@#$%^&()-+=~/\\ ").query + expect(query).toBe("(@someStrings:{\\,\\.\\?\\<\\>\\{\\}\\[\\]\\\"\\'\\:\\;\\|\\!\\@\\#\\$\\%\\^\\&\\(\\)\\-\\+\\=\\~\\/\\\\\\ })") }) it("generates a query with .containsOneOf that escapes all punctuation", () => { - let query = where.containsOneOf(",.<>{}[]\"':;|", "!@#$%^&()-+=~ ").query - expect(query).toBe("(@someStrings:{\\,\\.\\<\\>\\{\\}\\[\\]\\\"\\'\\:\\;\\||\\!\\@\\#\\$\\%\\^\\&\\(\\)\\-\\+\\=\\~\\ })") + let query = where.containsOneOf(",.?<>{}[]\"':;|", "!@#$%^&()-+=~/\\ ").query + expect(query).toBe("(@someStrings:{\\,\\.\\?\\<\\>\\{\\}\\[\\]\\\"\\'\\:\\;\\||\\!\\@\\#\\$\\%\\^\\&\\(\\)\\-\\+\\=\\~\\/\\\\\\ })") }) it("generates a query with .contains with a prefix matching wildcard", () => { diff --git a/spec/unit/search/search-by-string.spec.ts b/spec/unit/search/search-by-string.spec.ts index 796bfea..8f90b5f 100644 --- a/spec/unit/search/search-by-string.spec.ts +++ b/spec/unit/search/search-by-string.spec.ts @@ -76,11 +76,11 @@ describe("Search", () => { }) it("generates a query that escapes all punctuation", () => { - let query = where.eq(",.<>{}[]\"':;!@#$%^&()-+=~|/\\ ").query - expect(query).toBe("(@aString:{\\,\\.\\<\\>\\{\\}\\[\\]\\\"\\'\\:\\;\\!\\@\\#\\$\\%\\^\\&\\(\\)\\-\\+\\=\\~\\|\\/\\\\\\ })") + let query = where.eq(",.?<>{}[]\"':;!@#$%^&()-+=~|/\\ ").query + expect(query).toBe("(@aString:{\\,\\.\\?\\<\\>\\{\\}\\[\\]\\\"\\'\\:\\;\\!\\@\\#\\$\\%\\^\\&\\(\\)\\-\\+\\=\\~\\|\\/\\\\\\ })") }) - it("generates a query with a prefix matching wildcard", () => { + it("generates a query with a prefix matching wildcards", () => { let query = where.eq("foo*").query expect(query).toBe("(@aString:{foo*})") }) diff --git a/spec/unit/search/search-by-text.spec.ts b/spec/unit/search/search-by-text.spec.ts index c168fe8..11d120a 100644 --- a/spec/unit/search/search-by-text.spec.ts +++ b/spec/unit/search/search-by-text.spec.ts @@ -117,13 +117,13 @@ describe("Search", () => { describe("when generating a query with special characters in the string", () => { it("generates a query that escapes all punctuation for a match", () => { - let query = where.match(",.<>{}[]\"':;!@#$%^&()-+=~|").query - expect(query).toBe("(@someText:'\\,\\.\\<\\>\\{\\}\\[\\]\\\"\\'\\:\\;\\!\\@\\#\\$\\%\\^\\&\\(\\)\\-\\+\\=\\~\\|')") + let query = where.match(",.?<>{}[]\"':;!@#$%^&()-+=~|\\").query + expect(query).toBe("(@someText:'\\,\\.\\?\\<\\>\\{\\}\\[\\]\\\"\\'\\:\\;\\!\\@\\#\\$\\%\\^\\&\\(\\)\\-\\+\\=\\~\\|\\\\')") }) it("generates a query that escapes all punctuation for an exact match", () => { - let query = where.exact.match(",.<>{}[]\"':;!@#$%^&()-+=~|").query - expect(query).toBe('(@someText:"\\,\\.\\<\\>\\{\\}\\[\\]\\"\\\'\\:\\;\\!\\@\\#\\$\\%\\^\\&\\(\\)\\-\\+\\=\\~\\|")') + let query = where.exact.match(",.?<>{}[]\"':;!@#$%^&()-+=~|\\").query + expect(query).toBe('(@someText:"\\,\\.\\?\\<\\>\\{\\}\\[\\]\\"\\\'\\:\\;\\!\\@\\#\\$\\%\\^\\&\\(\\)\\-\\+\\=\\~\\|\\\\")') }) }) From 8da813f4222d7c6563cb7ed0a0a62269eb15fd43 Mon Sep 17 00:00:00 2001 From: Guy Royse Date: Wed, 26 Jul 2023 15:34:27 -0400 Subject: [PATCH 2/2] updated changelog and bumped version --- CHANGELOG | 4 ++++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 5995ea1..de32773 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,6 +4,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and Redis OM adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 0.4.2 - 2023-07-26 +### Fixed +- Fixed issue with TAGs not properl;y escaping question marks + ## 0.4.1 - 2023-07-26 ### Added - Added new type of `number[]` which works just like `number` but against an array of them diff --git a/package-lock.json b/package-lock.json index 5e67db2..8a0b7f1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "redis-om", - "version": "0.4.1", + "version": "0.4.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "redis-om", - "version": "0.4.1", + "version": "0.4.2", "license": "MIT", "dependencies": { "jsonpath-plus": "^7.2.0", diff --git a/package.json b/package.json index 63f0088..1021787 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "redis-om", - "version": "0.4.1", + "version": "0.4.2", "description": "Object mapping, and more, for Redis and Node.js. Written in TypeScript.", "main": "dist/index.js", "types": "./dist/index.d.ts",