Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Malformed array literal when handling single backslash in an array #4796

Closed
Strengthless opened this issue Oct 6, 2023 · 1 comment · Fixed by #4797
Closed

Malformed array literal when handling single backslash in an array #4796

Strengthless opened this issue Oct 6, 2023 · 1 comment · Fixed by #4797
Labels
bug Something isn't working

Comments

@Strengthless
Copy link
Contributor

Strengthless commented Oct 6, 2023

Describe the bug
When calling em.upsert('Book', { SCODE: 'AGSj', title: ['\\'] }), the command fails.

Stack trace

 ⨯ DriverException: insert into "book" ("scode", "title") values ('AGSj',  E'{\\}') on conflict ("scode") do update set "title" = excluded."title" returning "author", "call_number", "discipline", "isbn", "type", "image" - malformed array literal: "{\}"
    at PostgreSqlExceptionConverter.convertException ([redacted]/node_modules/@mikro-orm/core/platforms/ExceptionConverter.js:8:16)
    at PostgreSqlExceptionConverter.convertException ([redacted]/node_modules/@mikro-orm/postgresql/PostgreSqlExceptionConverter.js:42:22)
    at PostgreSqlDriver.convertException (/[redacted]/node_modules/@mikro-orm/core/drivers/DatabaseDriver.js:201:54)
    at [redacted]/node_modules/@mikro-orm/core/drivers/DatabaseDriver.js:205:24
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async PostgreSqlDriver.nativeUpdate ([redacted]/node_modules/@mikro-orm/knex/AbstractSqlDriver.js:379:19)
    at async SqlEntityManager.upsert ([redacted]/node_modules/@mikro-orm/core/EntityManager.js:496:21)
    at async GET (webpack-internal:///(rsc)/./src/app/api/test/route.ts:16:5)
    at async [redacted]/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:62361

previous error: insert into "book" ("scode", "title") values ('AGSj',  E'{\\}') on conflict ("scode") do update set "title" = excluded."title" returning "author", "call_number", "discipline", "isbn", "type", "image" - malformed array literal: "{\}"
    at Parser.parseErrorMessage ([redacted]/node_modules/pg-protocol/dist/parser.js:287:98)
    at Parser.handlePacket ([redacted]/node_modules/pg-protocol/dist/parser.js:126:29)
    at Parser.parse ([redacted]/node_modules/pg-protocol/dist/parser.js:39:38)
    at Socket.<anonymous> ([redacted]/node_modules/pg-protocol/dist/index.js:11:42)
    at Socket.emit (node:events:514:28)
    at addChunk (node:internal/streams/readable:343:12)
    at readableAddChunk (node:internal/streams/readable:316:9)
    at Readable.push (node:internal/streams/readable:253:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
    at TCP.callbackTrampoline (node:internal/async_hooks:130:17) {
  length: 119,
  severity: 'ERROR',
  code: '22P02',
  detail: 'Unexpected end of input.',
  hint: undefined,
  position: '56',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'arrayfuncs.c',
  line: '491',
  routine: 'ArrayCount'
}

To Reproduce
Steps to reproduce the behavior:

  1. Upsert data including an array with only a single backslash, e.g.:
em.upsert('Book', { SCODE: 'AGSj', title: ['\\'] })
  1. Observe the error.

Expected behavior
The upsert should perform without errors.

Additional context

  1. This is likely a PostgreSQL driver-specific issue, with the parser translating JS arrays into Postgres arrays failing.
  2. Related issue: type: string[] - malformed array literal when setting a value that includes the character "," #3810
  3. Working patch, can make a PR if this looks good:
diff --git a/node_modules/@mikro-orm/postgresql/PostgreSqlPlatform.js b/node_modules/@mikro-orm/postgresql/PostgreSqlPlatform.js
index 499637a..f8db08d 100644
--- a/node_modules/@mikro-orm/postgresql/PostgreSqlPlatform.js
+++ b/node_modules/@mikro-orm/postgresql/PostgreSqlPlatform.js
@@ -113,7 +113,7 @@ class PostgreSqlPlatform extends knex_1.AbstractSqlPlatform {
         return true;
     }
     marshallArray(values) {
-        const quote = (v) => v === '' || v.match(/["{},]/) ? JSON.stringify(v) : v;
+        const quote = (v) => v === '' || v.match(/["{},\\]/) ? JSON.stringify(v) : v;
         return `{${values.map(v => quote('' + v)).join(',')}}`;
     }
     unmarshallArray(value) {

Versions

Dependency Version
node 20.5.1
typescript 5.2.2
mikro-orm 5.8.4
postgres 15.4
@B4nan
Copy link
Member

B4nan commented Oct 7, 2023

Working patch, can make a PR if this looks good:

Sure, as long as it does not break any existing tests, PR welcome! The change is looking good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants