Skip to content

Commit

Permalink
fix: support for string escaping in ZModel (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Sep 4, 2023
1 parent 6e9a3b3 commit f034839
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/language/src/generated/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3429,7 +3429,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
"name": "STRING",
"definition": {
"$type": "RegexToken",
"regex": "\\"[^\\"]*\\"|'[^']*'"
"regex": "\\"(\\\\\\\\.|[^\\"\\\\\\\\])*\\"|'(\\\\\\\\.|[^'\\\\\\\\])*'"
},
"fragment": false,
"hidden": false
Expand Down
2 changes: 1 addition & 1 deletion packages/language/src/zmodel.langium
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ hidden terminal WS: /\s+/;
terminal NULL: 'null';
terminal THIS: 'this';
terminal ID: /[_a-zA-Z][\w_]*/;
terminal STRING: /"[^"]*"|'[^']*'/;
terminal STRING: /"(\\.|[^"\\])*"|'(\\.|[^'\\])*'/;
terminal NUMBER: /[+-]?[0-9]+(\.[0-9]+)?/;
terminal TRIPLE_SLASH_COMMENT: /\/\/\/[^\n\r]*/;
hidden terminal ML_COMMENT: /\/\*[\s\S]*?\*\//;
Expand Down
3 changes: 2 additions & 1 deletion packages/schema/src/plugins/prisma/prisma-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ export class AttributeArgValue {
toString(): string {
switch (this.type) {
case 'String':
return `"${this.value}"`;
// use JSON.stringify to escape quotes
return JSON.stringify(this.value);
case 'Number':
return this.value.toString();
case 'FieldReference': {
Expand Down
18 changes: 18 additions & 0 deletions packages/schema/tests/schema/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ describe('Parsing Tests', () => {
expect(m.fields[2].attributes[0].args[0].value.$resolvedType?.decl).toBe(firstEnum);
});

it('string escape', async () => {
const content = `
model Example {
id Int @id
doubleQuote String @default("s\\"1")
singleQuote String @default('s\\'1')
json Json @default("{\\"theme\\": \\"light\\", \\"consoleDrawer\\": false}")
}
`;
const doc = await loadModel(content, false);
const model = doc.declarations[0] as DataModel;
expect((model.fields[1].attributes[0].args[0].value as StringLiteral).value).toBe('s"1');
expect((model.fields[2].attributes[0].args[0].value as StringLiteral).value).toBe("s'1");
expect((model.fields[3].attributes[0].args[0].value as StringLiteral).value).toBe(
'{"theme": "light", "consoleDrawer": false}'
);
});

it('model field types', async () => {
const content = `
model User {
Expand Down
9 changes: 7 additions & 2 deletions packages/testtools/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,16 @@ export function getWorkspaceNpmCacheFolder(start: string) {
}

function makePrelude(options: SchemaLoadOptions) {
let dbUrl = options.dbUrl ?? (options.provider === 'postgresql' ? 'env("DATABASE_URL")' : 'file:./dev.db');

if (!dbUrl.includes('env(') && !dbUrl.startsWith("'") && !dbUrl.startsWith('"')) {
dbUrl = `'${dbUrl}'`;
}

return `
datasource db {
provider = '${options.provider}'
url = '${options.dbUrl}'
url = ${dbUrl}
}
generator js {
Expand Down Expand Up @@ -106,7 +112,6 @@ const defaultOptions: SchemaLoadOptions = {
compile: false,
logPrismaQuery: false,
provider: 'sqlite',
dbUrl: 'file:./test.db',
};

export async function loadSchemaFromFile(schemaFile: string, options?: SchemaLoadOptions) {
Expand Down
17 changes: 17 additions & 0 deletions tests/integration/tests/regression/issue-416.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { loadSchema } from '@zenstackhq/testtools';

describe('Regression: issue 416', () => {
it('regression', async () => {
await loadSchema(
`
model Example {
id Int @id
doubleQuote String @default("s\\"1")
singleQuote String @default('s\\'1')
json Json @default("{\\"theme\\": \\"light\\", \\"consoleDrawer\\": false}")
}
`,
{ provider: 'postgresql', dbUrl: 'env("DATABASE_URL")', pushDb: false }
);
});
});

0 comments on commit f034839

Please sign in to comment.