loopbackio / loopbackio/loopback-connector-postgresql
eq object (JSON columns) doesn't work
未关闭
还没有人认领这个 Issue。
bug
needs-priority
stale
- 主要语言
- JavaScript
- 星标
- 118
- 派生
- 184
- 平均合并
- 1 天 22 小时
- 30 天内合并 PR
- 5
描述
Steps to reproduce
- Create a model that has an object column, mapped as a JSON type in PostgreSQL
- Try to do a
findsearching for a full value in that property/column, e.g.const objectValue = {a: 1, b: 2};and thenrepo.find({where: {objectProperty: objectValue}})orrepo.find({where: {objectProperty: {eq: objectValue}}})
Current Behavior
- The
{objectProperty: {eq: value}}is translated down into{objectProperty: value} - This line assumes that, if the value is an object, it must contain exactly one field that must be an operator: https://github.com/strongloop/loopback-connector-postgresql/blob/master/lib/postgresql.js#L654
- It tries to map e.g.
aas an operator name - The
buildExpressionoperator switch hits itsdefaultclause which delegates to the base class inloopback-connector: https://github.com/strongloop/loopback-connector-postgresql/blob/master/lib/postgresql.js#L540-L543 - That base class method has a
switchwith nodefaultclause, so it doesn't throw any errors and just concatenates the column name with the placeholder for the value: https://github.com/strongloop/loopback-connector/blob/master/lib/sql.js#L969 - And so it generates invalid SQL that looks like
"columName"$1
Expected Behavior
- I should be able to use object values in where clauses if the property contains object values
Link to reproduction sandbox
WIP -- NB: encountering this in an LB4 app
Additional information
- Running on
linux x64 12.22.1 npm lsdoesn't work withrush, but usingloopback-connector-postgresqlv5.0.1, withloopback-connectorv4.11.1, and the following LB4 components:"@loopback/boot": "2.2.0""@loopback/context": "3.9.3""@loopback/core": "2.5.0""@loopback/metadata": "2.2.6""@loopback/openapi-v3": "3.3.1""@loopback/openapi-v3-types": "1.2.1""@loopback/repository": "2.4.0""@loopback/rest": "4.0.0""@loopback/rest-explorer": "2.2.0"
Related Issues
Haven't found any yet
Workaround
Create a custom class to represent the value, and then have the equality comparison value use that, e.g. something like this, but without the prototype pollution vulnerabilities:
class JSONWrapper {
[k: string]: any
constructor(value: any) {
Object.assign(this, value)
}
}
// elsewhere:
repo.find({where: {objectProperty: new JSONWrapper(objectValue)}});
This causes the expression.constructor === Object check to fail, and so it doesn't try to unwrap the value
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从报告中指出的 operator parsing 和 buildExpression 位置开始检查 lib/postgresql.js,然后检查 loopback-connector/lib/sql.js 中相关的基础逻辑。针对 PostgreSQL 重现 JSON 列等值查询,并跟踪对象值如何变成 SQL。完成的标准是 where 子句中的对象值能够生成有效的 SQL 并返回匹配的记录,同时在相关的 connector 测试中加入回归覆盖。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript, postgresql
- 领域
- databases
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 48/100