loopbackio / loopbackio/loopback-connector-postgresql

eq object (JSON columns) doesn't work

オープン
#477 コメント 6 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug needs-priority stale
主要言語
JavaScript
スター
118
フォーク
184
平均マージ
1日 22時間
マージ済み PR(30日)
5

説明

Steps to reproduce

  1. Create a model that has an object column, mapped as a JSON type in PostgreSQL
  2. Try to do a find searching for a full value in that property/column, e.g. const objectValue = {a: 1, b: 2}; and then repo.find({where: {objectProperty: objectValue}}) or repo.find({where: {objectProperty: {eq: objectValue}}})

Current Behavior

  1. The {objectProperty: {eq: value}} is translated down into {objectProperty: value}
  2. 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
  3. It tries to map e.g. a as an operator name
  4. The buildExpression operator switch hits its default clause which delegates to the base class in loopback-connector: https://github.com/strongloop/loopback-connector-postgresql/blob/master/lib/postgresql.js#L540-L543
  5. That base class method has a switch with no default clause, 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
  6. 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 ls doesn't work with rush, but using loopback-connector-postgresql v5.0.1, with loopback-connector v4.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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

レポートで示されている operator parsing と buildExpression の箇所を lib/postgresql.js から確認し、続いて loopback-connector/lib/sql.js にある関連する基盤ロジックを調べます。JSON カラムに対する等価比較クエリを PostgreSQL に対して再現し、オブジェクトの値がどのように SQL になるかを追跡します。where 句のオブジェクト値が有効な SQL を生成して一致するレコードを返し、関連する connector テストに回帰テストのカバレッジが追加されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, postgresql
領域
databases
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
48/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。