Skip to content

Commit ca19cc0

Browse files
DutchGermanwolfgangwalther
authored andcommitted
fix: Parse alias correctly with quoted field names including : in select
Resolves #587
1 parent 00855c0 commit ca19cc0

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/Query.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,12 @@ class Query extends URL {
8383
}
8484
// regular select
8585
let alias = ''; let field; let cast = ''; let subfields = []
86-
if (k.includes(':')) {
87-
[alias, field] = k.split(':')
86+
if (/^[^"]*:/.test(k)) {
87+
// first `:` is outside quotes
88+
[alias, field] = k.split(/:(.+)/)
89+
} else if (/^".*[^\\]":/.test(k)) {
90+
// quoted alias
91+
[alias, field] = k.split(/(?<=[^\\]"):(.+)/)
8892
} else {
8993
field = k
9094
}

tests/unit/Query.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,35 @@ describe('Query', () => {
228228
}
229229
}
230230
}, 'select=id,jd:json_data::text,bt:json_data->blood_type::integer')
231+
232+
itt('json field with quoted key', {
233+
select: {
234+
'some->nested->"https://json-that-needs-quotes"': true
235+
}
236+
}, 'select=some->nested->"https://json-that-needs-quotes"')
237+
238+
itt('json field with quoted key and aliased', {
239+
select: {
240+
'alias:some->nested->"https://json-that-needs-quotes"': true
241+
}
242+
}, 'select=alias:some->nested->"https://json-that-needs-quotes"')
243+
244+
itt('json field with quoted nested key', {
245+
select: {
246+
some: {
247+
nested: {
248+
'"https://json-that-needs-quotes"': true
249+
}
250+
}
251+
}
252+
}, 'select=some->nested->"https://json-that-needs-quotes"'
253+
)
254+
255+
itt('should parse quoted aliases with escaped quotes properly', {
256+
select: {
257+
'"a:\\":b":"x:y:z"': true
258+
}
259+
}, 'select="a:\\":b":"x:y:z"')
231260
})
232261

233262
describe('ordering', () => {

0 commit comments

Comments
 (0)