From 492dd36292c1b50b2f4dba5e3d43c5e47cccc29c Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Sun, 20 Oct 2024 15:40:45 -0700 Subject: [PATCH 1/6] fix(cli): regular relation fields are incorrectly recognized as self-relation --- .../validator/datamodel-validator.ts | 47 ++++--------------- .../validation/datamodel-validation.test.ts | 4 +- 2 files changed, 12 insertions(+), 39 deletions(-) diff --git a/packages/schema/src/language-server/validator/datamodel-validator.ts b/packages/schema/src/language-server/validator/datamodel-validator.ts index eb6d06400..ebb014dd1 100644 --- a/packages/schema/src/language-server/validator/datamodel-validator.ts +++ b/packages/schema/src/language-server/validator/datamodel-validator.ts @@ -7,13 +7,7 @@ import { isEnum, isStringLiteral, } from '@zenstackhq/language/ast'; -import { - getLiteral, - getModelFieldsWithBases, - getModelIdFields, - getModelUniqueFields, - isDelegateModel, -} from '@zenstackhq/sdk'; +import { getModelFieldsWithBases, getModelIdFields, getModelUniqueFields, isDelegateModel } from '@zenstackhq/sdk'; import { AstNode, DiagnosticInfo, ValidationAcceptor, getDocument } from 'langium'; import { findUpInheritance } from '../../utils/ast-utils'; import { IssueCodes, SCALAR_TYPES } from '../constants'; @@ -147,15 +141,15 @@ export default class DataModelValidator implements AstValidator { } } + if (!fields && !references) { + return { attr: relAttr, name, fields, references, valid: true }; + } + if (!fields || !references) { - if (this.isSelfRelation(field, name)) { - // self relations are partial - // https://www.prisma.io/docs/concepts/components/prisma-schema/relations/self-relations - } else { - if (accept) { - accept('error', `Both "fields" and "references" must be provided`, { node: relAttr }); - } + if (accept) { + accept('error', `"fields" and "references" must be provided together`, { node: relAttr }); } + // } } else { // validate "fields" and "references" typing consistency if (fields.length !== references.length) { @@ -203,33 +197,12 @@ export default class DataModelValidator implements AstValidator { return { attr: relAttr, name, fields, references, valid }; } - private isSelfRelation(field: DataModelField, relationName?: string) { + private isSelfRelation(field: DataModelField, _relationName?: string) { if (field.type.reference?.ref === field.$container) { // field directly references back to its type return true; } - if (relationName) { - // field's relation points to another type, and that type's opposite relation field - // points back - const oppositeModel = field.type.reference?.ref as DataModel; - if (oppositeModel) { - const oppositeModelFields = getModelFieldsWithBases(oppositeModel); - for (const oppositeField of oppositeModelFields) { - // find the opposite relation with the matching name - const relAttr = oppositeField.attributes.find((a) => a.decl.ref?.name === '@relation'); - if (relAttr) { - const relNameExpr = relAttr.args.find((a) => !a.name || a.name === 'name'); - const relName = getLiteral(relNameExpr?.value); - if (relName === relationName && oppositeField.type.reference?.ref === field.$container) { - // found an opposite relation field that points back to this field's type - return true; - } - } - } - } - } - return false; } @@ -333,7 +306,7 @@ export default class DataModelValidator implements AstValidator { if (!this.isSelfRelation(f, thisRelation.name)) { accept( 'error', - 'Field for one side of relation must carry @relation attribute with both "fields" and "references" fields', + 'Field for one side of relation must carry @relation attribute with both "fields" and "references"', { node: f } ); } diff --git a/packages/schema/tests/schema/validation/datamodel-validation.test.ts b/packages/schema/tests/schema/validation/datamodel-validation.test.ts index e7dd6bf84..d89e96a44 100644 --- a/packages/schema/tests/schema/validation/datamodel-validation.test.ts +++ b/packages/schema/tests/schema/validation/datamodel-validation.test.ts @@ -451,7 +451,7 @@ describe('Data Model Validation Tests', () => { aId String } `) - ).toMatchObject(errorLike(`Both "fields" and "references" must be provided`)); + ).toMatchObject(errorLike(`"fields" and "references" must be provided together`)); // one-to-one inconsistent attribute expect( @@ -541,7 +541,7 @@ describe('Data Model Validation Tests', () => { `) ).toMatchObject( errorLike( - `Field for one side of relation must carry @relation attribute with both "fields" and "references" fields` + `Field for one side of relation must carry @relation attribute with both "fields" and "references"` ) ); From 93e61501389e368ca15ef91d04fe7dcde8298648 Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Sun, 20 Oct 2024 15:50:06 -0700 Subject: [PATCH 2/6] update --- .../src/language-server/validator/datamodel-validator.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/schema/src/language-server/validator/datamodel-validator.ts b/packages/schema/src/language-server/validator/datamodel-validator.ts index ebb014dd1..e826e6254 100644 --- a/packages/schema/src/language-server/validator/datamodel-validator.ts +++ b/packages/schema/src/language-server/validator/datamodel-validator.ts @@ -197,13 +197,8 @@ export default class DataModelValidator implements AstValidator { return { attr: relAttr, name, fields, references, valid }; } - private isSelfRelation(field: DataModelField, _relationName?: string) { - if (field.type.reference?.ref === field.$container) { - // field directly references back to its type - return true; - } - - return false; + private isSelfRelation(field: DataModelField) { + return field.type.reference?.ref === field.$container; } private validateRelationField(contextModel: DataModel, field: DataModelField, accept: ValidationAcceptor) { From 10c2f7b8769e90bbc70fd89b627995c4d7223f7f Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Sun, 20 Oct 2024 16:29:47 -0700 Subject: [PATCH 3/6] fix build --- .../schema/src/language-server/validator/datamodel-validator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/schema/src/language-server/validator/datamodel-validator.ts b/packages/schema/src/language-server/validator/datamodel-validator.ts index e826e6254..6b27d2f9a 100644 --- a/packages/schema/src/language-server/validator/datamodel-validator.ts +++ b/packages/schema/src/language-server/validator/datamodel-validator.ts @@ -298,7 +298,7 @@ export default class DataModelValidator implements AstValidator { // if both the field is array, then it's an implicit many-to-many relation if (!(field.type.array && oppositeField.type.array)) { [field, oppositeField].forEach((f) => { - if (!this.isSelfRelation(f, thisRelation.name)) { + if (!this.isSelfRelation(f)) { accept( 'error', 'Field for one side of relation must carry @relation attribute with both "fields" and "references"', From 5c84e657aebbfd64ef9ed31998b8a9963c882473 Mon Sep 17 00:00:00 2001 From: Thomas Sunde Nielsen Date: Mon, 21 Oct 2024 17:54:54 +0200 Subject: [PATCH 4/6] URI decode resourceId (#1791) --- packages/server/src/api/rest/index.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/server/src/api/rest/index.ts b/packages/server/src/api/rest/index.ts index 4bf2dcfe3..42bf0ef5c 100644 --- a/packages/server/src/api/rest/index.ts +++ b/packages/server/src/api/rest/index.ts @@ -1231,15 +1231,16 @@ class RequestHandler extends APIHandlerBase { } private makePrismaIdFilter(idFields: FieldInfo[], resourceId: string) { + const decodedId = decodeURIComponent(resourceId); if (idFields.length === 1) { - return { [idFields[0].name]: this.coerce(idFields[0].type, resourceId) }; + return { [idFields[0].name]: this.coerce(idFields[0].type, decodedId) }; } else { return { // TODO: support `@@id` with custom name [idFields.map((idf) => idf.name).join(prismaIdDivider)]: idFields.reduce( (acc, curr, idx) => ({ ...acc, - [curr.name]: this.coerce(curr.type, resourceId.split(this.idDivider)[idx]), + [curr.name]: this.coerce(curr.type, decodedId.split(this.idDivider)[idx]), }), {} ), From f1afc15cf5c508490f2207e2ba636f5c0dd4885e Mon Sep 17 00:00:00 2001 From: Yiming Date: Mon, 21 Oct 2024 09:32:18 -0700 Subject: [PATCH 5/6] chore: bump version (#1792) --- package.json | 2 +- packages/ide/jetbrains/build.gradle.kts | 2 +- packages/ide/jetbrains/package.json | 2 +- packages/language/package.json | 2 +- packages/misc/redwood/package.json | 2 +- packages/plugins/openapi/package.json | 2 +- packages/plugins/swr/package.json | 2 +- packages/plugins/tanstack-query/package.json | 2 +- packages/plugins/trpc/package.json | 2 +- packages/runtime/package.json | 2 +- packages/schema/package.json | 2 +- packages/sdk/package.json | 2 +- packages/server/package.json | 2 +- packages/testtools/package.json | 2 +- 14 files changed, 14 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index aea23c122..058fe45f5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zenstack-monorepo", - "version": "2.7.1", + "version": "2.7.2", "description": "", "scripts": { "build": "pnpm -r build", diff --git a/packages/ide/jetbrains/build.gradle.kts b/packages/ide/jetbrains/build.gradle.kts index 43f35d2bb..ea44ec1b8 100644 --- a/packages/ide/jetbrains/build.gradle.kts +++ b/packages/ide/jetbrains/build.gradle.kts @@ -9,7 +9,7 @@ plugins { } group = "dev.zenstack" -version = "2.7.1" +version = "2.7.2" repositories { mavenCentral() diff --git a/packages/ide/jetbrains/package.json b/packages/ide/jetbrains/package.json index be16a57d4..14b13cada 100644 --- a/packages/ide/jetbrains/package.json +++ b/packages/ide/jetbrains/package.json @@ -1,6 +1,6 @@ { "name": "jetbrains", - "version": "2.7.1", + "version": "2.7.2", "displayName": "ZenStack JetBrains IDE Plugin", "description": "ZenStack JetBrains IDE plugin", "homepage": "https://zenstack.dev", diff --git a/packages/language/package.json b/packages/language/package.json index be5e0f5b9..1a0c52ed6 100644 --- a/packages/language/package.json +++ b/packages/language/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/language", - "version": "2.7.1", + "version": "2.7.2", "displayName": "ZenStack modeling language compiler", "description": "ZenStack modeling language compiler", "homepage": "https://zenstack.dev", diff --git a/packages/misc/redwood/package.json b/packages/misc/redwood/package.json index 822226666..75cfb9454 100644 --- a/packages/misc/redwood/package.json +++ b/packages/misc/redwood/package.json @@ -1,7 +1,7 @@ { "name": "@zenstackhq/redwood", "displayName": "ZenStack RedwoodJS Integration", - "version": "2.7.1", + "version": "2.7.2", "description": "CLI and runtime for integrating ZenStack with RedwoodJS projects.", "repository": { "type": "git", diff --git a/packages/plugins/openapi/package.json b/packages/plugins/openapi/package.json index 812e1fd68..66cf69e74 100644 --- a/packages/plugins/openapi/package.json +++ b/packages/plugins/openapi/package.json @@ -1,7 +1,7 @@ { "name": "@zenstackhq/openapi", "displayName": "ZenStack Plugin and Runtime for OpenAPI", - "version": "2.7.1", + "version": "2.7.2", "description": "ZenStack plugin and runtime supporting OpenAPI", "main": "index.js", "repository": { diff --git a/packages/plugins/swr/package.json b/packages/plugins/swr/package.json index 32d84c4e8..072346883 100644 --- a/packages/plugins/swr/package.json +++ b/packages/plugins/swr/package.json @@ -1,7 +1,7 @@ { "name": "@zenstackhq/swr", "displayName": "ZenStack plugin for generating SWR hooks", - "version": "2.7.1", + "version": "2.7.2", "description": "ZenStack plugin for generating SWR hooks", "main": "index.js", "repository": { diff --git a/packages/plugins/tanstack-query/package.json b/packages/plugins/tanstack-query/package.json index 473e7e003..f53c16a30 100644 --- a/packages/plugins/tanstack-query/package.json +++ b/packages/plugins/tanstack-query/package.json @@ -1,7 +1,7 @@ { "name": "@zenstackhq/tanstack-query", "displayName": "ZenStack plugin for generating tanstack-query hooks", - "version": "2.7.1", + "version": "2.7.2", "description": "ZenStack plugin for generating tanstack-query hooks", "main": "index.js", "exports": { diff --git a/packages/plugins/trpc/package.json b/packages/plugins/trpc/package.json index 20adfe0bf..832b3f980 100644 --- a/packages/plugins/trpc/package.json +++ b/packages/plugins/trpc/package.json @@ -1,7 +1,7 @@ { "name": "@zenstackhq/trpc", "displayName": "ZenStack plugin for tRPC", - "version": "2.7.1", + "version": "2.7.2", "description": "ZenStack plugin for tRPC", "main": "index.js", "repository": { diff --git a/packages/runtime/package.json b/packages/runtime/package.json index 4189d9d24..92fc4fe57 100644 --- a/packages/runtime/package.json +++ b/packages/runtime/package.json @@ -1,7 +1,7 @@ { "name": "@zenstackhq/runtime", "displayName": "ZenStack Runtime Library", - "version": "2.7.1", + "version": "2.7.2", "description": "Runtime of ZenStack for both client-side and server-side environments.", "repository": { "type": "git", diff --git a/packages/schema/package.json b/packages/schema/package.json index e248df113..e0ba75416 100644 --- a/packages/schema/package.json +++ b/packages/schema/package.json @@ -3,7 +3,7 @@ "publisher": "zenstack", "displayName": "ZenStack Language Tools", "description": "FullStack enhancement for Prisma ORM: seamless integration from database to UI", - "version": "2.7.1", + "version": "2.7.2", "author": { "name": "ZenStack Team" }, diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 26f8205d7..2ccd344d2 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/sdk", - "version": "2.7.1", + "version": "2.7.2", "description": "ZenStack plugin development SDK", "main": "index.js", "scripts": { diff --git a/packages/server/package.json b/packages/server/package.json index c34b3857a..964149285 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/server", - "version": "2.7.1", + "version": "2.7.2", "displayName": "ZenStack Server-side Adapters", "description": "ZenStack server-side adapters", "homepage": "https://zenstack.dev", diff --git a/packages/testtools/package.json b/packages/testtools/package.json index 2111ab1ab..d8dd3ad45 100644 --- a/packages/testtools/package.json +++ b/packages/testtools/package.json @@ -1,6 +1,6 @@ { "name": "@zenstackhq/testtools", - "version": "2.7.1", + "version": "2.7.2", "description": "ZenStack Test Tools", "main": "index.js", "private": true, From 84118b4b3dbee95a828a8ce4e0e7362753107238 Mon Sep 17 00:00:00 2001 From: Yiming Date: Mon, 21 Oct 2024 09:45:11 -0700 Subject: [PATCH 6/6] chore: remove commented code (#1794) --- .../schema/src/language-server/validator/datamodel-validator.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/schema/src/language-server/validator/datamodel-validator.ts b/packages/schema/src/language-server/validator/datamodel-validator.ts index 6b27d2f9a..e463f740f 100644 --- a/packages/schema/src/language-server/validator/datamodel-validator.ts +++ b/packages/schema/src/language-server/validator/datamodel-validator.ts @@ -149,7 +149,6 @@ export default class DataModelValidator implements AstValidator { if (accept) { accept('error', `"fields" and "references" must be provided together`, { node: relAttr }); } - // } } else { // validate "fields" and "references" typing consistency if (fields.length !== references.length) {