Skip to content

Commit fac631b

Browse files
authored
fix(delegate): discriminator fields should be removed from unchecked create/update input types (#1804)
1 parent 74dd0f7 commit fac631b

File tree

2 files changed

+60
-10
lines changed

2 files changed

+60
-10
lines changed

packages/schema/src/plugins/enhancer/enhance/index.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -515,15 +515,11 @@ export function enhance(prisma: any, context?: EnhancementContext<${authTypePara
515515
return source;
516516
}
517517

518-
private removeCreateFromDelegateInput(
519-
typeAlias: TypeAliasDeclaration,
520-
delegateModels: DelegateInfo,
521-
source: string
522-
) {
518+
private removeCreateFromDelegateInput(typeAlias: TypeAliasDeclaration, delegateInfo: DelegateInfo, source: string) {
523519
// remove create/connectOrCreate/upsert fields from delegate's input types because
524520
// delegate models cannot be created directly
525521
const typeName = typeAlias.getName();
526-
const delegateModelNames = delegateModels.map(([delegate]) => delegate.name);
522+
const delegateModelNames = delegateInfo.map(([delegate]) => delegate.name);
527523
const delegateCreateUpdateInputRegex = new RegExp(
528524
`^(${delegateModelNames.join('|')})(Unchecked)?(Create|Update).*Input$`
529525
);
@@ -538,17 +534,24 @@ export function enhance(prisma: any, context?: EnhancementContext<${authTypePara
538534
return source;
539535
}
540536

541-
private readonly ModelCreateUpdateInputRegex = /(\S+)(Unchecked)?(Create|Update).*Input/;
542-
543537
private removeDiscriminatorFromConcreteInput(
544538
typeAlias: TypeAliasDeclaration,
545-
_delegateInfo: DelegateInfo,
539+
delegateInfo: DelegateInfo,
546540
source: string
547541
) {
548542
// remove discriminator field from the create/update input because discriminator cannot be set directly
549543
const typeName = typeAlias.getName();
550544

551-
const match = typeName.match(this.ModelCreateUpdateInputRegex);
545+
const delegateModelNames = delegateInfo.map(([delegate]) => delegate.name);
546+
const concreteModelNames = delegateInfo
547+
.map(([_, concretes]) => concretes.flatMap((c) => c.name))
548+
.flatMap((name) => name);
549+
const allModelNames = [...new Set([...delegateModelNames, ...concreteModelNames])];
550+
const concreteCreateUpdateInputRegex = new RegExp(
551+
`^(${allModelNames.join('|')})(Unchecked)?(Create|Update).*Input$`
552+
);
553+
554+
const match = typeName.match(concreteCreateUpdateInputRegex);
552555
if (match) {
553556
const modelName = match[1];
554557
const dataModel = this.model.declarations.find(
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
3+
describe('issue 1763', () => {
4+
it('regression', async () => {
5+
await loadSchema(
6+
`
7+
model Post {
8+
id Int @id @default(autoincrement())
9+
name String
10+
11+
type String
12+
@@delegate(type)
13+
14+
// full access by author
15+
@@allow('all', true)
16+
}
17+
18+
model ConcretePost extends Post {
19+
age Int
20+
}
21+
`,
22+
{
23+
compile: true,
24+
extraSourceFiles: [
25+
{
26+
name: 'main.ts',
27+
content: `
28+
import { PrismaClient as Prisma } from '@prisma/client';
29+
import { enhance } from '@zenstackhq/runtime';
30+
31+
async function test() {
32+
const prisma = new Prisma();
33+
const db = enhance(prisma);
34+
await db.concretePost.create({
35+
data: {
36+
id: 5,
37+
name: 'a name',
38+
age: 20,
39+
},
40+
});
41+
} `,
42+
},
43+
],
44+
}
45+
);
46+
});
47+
});

0 commit comments

Comments
 (0)