Skip to content

Commit

Permalink
Revert "unescape quotes when inserting json (#95)" (#96)
Browse files Browse the repository at this point in the history
This reverts commit d5f9391.
  • Loading branch information
Scott Sandler authored Feb 17, 2023
1 parent d5f9391 commit 708178e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 14 deletions.
6 changes: 1 addition & 5 deletions src/DataIntegrity.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,13 @@ public static function ensureFieldsPresent(dict<string, mixed> $row, table_schem
if ($row[$field_name] is nonnull) {
if (!Str\is_empty((string)$row[$field_name])) {
// validate json string
// json_decode will return null if json string contains escaped quotes - mysql just unescapes the quotes and accepts the string
$json_string = \stripslashes((string)$row[$field_name]);
$json_obj = \json_decode($json_string);
$json_obj = \json_decode((string)$row[$field_name]);
if ($json_obj is null) {
// invalid json
throw new SQLFakeRuntimeException(
"Invalid value '{$field_value}' for column '{$field_name}' on '{$schema['name']}', expected json",
);
}
// mysql removes the \
$row[$field_name] = $json_string;
} else {
// empty strings are not valid for json columns
throw new SQLFakeRuntimeException(
Expand Down
10 changes: 1 addition & 9 deletions tests/InsertQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,19 +271,11 @@ final class InsertQueryTest extends HackTest {
public async function testValidJsonInsertIntoJsonColumn(): Awaitable<void> {
$conn = static::$conn as nonnull;
QueryContext::$strictSQLMode = true;
await $conn->query('INSERT INTO table_with_json (id, data) VALUES (1, \'{"test":123}\')');
await $conn->query("INSERT INTO table_with_json (id, data) VALUES (1, '{\"test\":123}')");
$result = await $conn->query('SELECT * FROM table_with_json');
expect($result->rows())->toBeSame(vec[dict['id' => 1, 'data' => '{"test":123}']]);
}

public async function testValidJsonWithQuotesEscapedInsertIntoJsonColumn(): Awaitable<void> {
$conn = static::$conn as nonnull;
QueryContext::$strictSQLMode = true;
await $conn->query('INSERT INTO table_with_json (id, data) VALUES (1, \'{\\\"domains\\\":[\\\"foo.com\\\"]}\')');
$result = await $conn->query('SELECT * FROM table_with_json');
expect($result->rows())->toBeSame(vec[dict['id' => 1, 'data' => '{"domains":["foo.com"]}']]);
}

public async function testDupeInsertNoConflicts(): Awaitable<void> {
$conn = static::$conn as nonnull;
await $conn->query(
Expand Down

0 comments on commit 708178e

Please sign in to comment.