Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/schema/src/plugins/zod/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,21 @@ export default class Transformer {
// "Input" or "NestedInput" suffix
mappedInputTypeName += match[4];

processedInputType = { ...inputType, type: mappedInputTypeName };
// Prisma's naming is inconsistent for update input types, so we need
// to check for a few other candidates and use the one that matches
// a DMMF input type name
const candidates = [mappedInputTypeName];
if (mappedInputTypeName.includes('UpdateOne')) {
candidates.push(...candidates.map((name) => name.replace('UpdateOne', 'Update')));
}
if (mappedInputTypeName.includes('NestedInput')) {
candidates.push(...candidates.map((name) => name.replace('NestedInput', 'Input')));
}

const finalMappedName =
candidates.find((name) => this.inputObjectTypes.some((it) => it.name === name)) ?? mappedInputTypeName;

processedInputType = { ...inputType, type: finalMappedName };
}
return processedInputType;
}
Expand Down
10 changes: 9 additions & 1 deletion packages/sdk/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,8 +659,16 @@ export function getRelationBackLink(field: DataModelField) {

const targetModel = field.type.reference.ref as DataModel;

const sameField = (f1: DataModelField, f2: DataModelField) => {
// for fields inherited from a delegate model, always use
// the base to compare
const parent1 = f1.$inheritedFrom ?? f1.$container;
const parent2 = f2.$inheritedFrom ?? f2.$container;
return f1.name === f2.name && parent1 === parent2;
};

for (const otherField of targetModel.fields) {
if (otherField === field) {
if (sameField(otherField, field)) {
// backlink field is never self
continue;
}
Expand Down
31 changes: 31 additions & 0 deletions tests/regression/tests/issue-2226.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { loadSchema } from '@zenstackhq/testtools';

describe('issue 2226', () => {
it('regression', async () => {
const { zodSchemas } = await loadSchema(
`
model Registration {
id String @id
regType String
@@delegate(regType)

replacedRegistrationId String?
replacedRegistration Registration? @relation("ReplacedBy", fields: [replacedRegistrationId], references: [id])
replacements Registration[] @relation("ReplacedBy")
}

// Delegated subtype
model RegistrationFramework extends Registration {
}
`,
{ fullZod: true }
);

const schema = zodSchemas.objects.RegistrationFrameworkUpdateInputObjectSchema;
expect(schema).toBeDefined();
const parsed = schema.safeParse({
replacedRegistrationId: '123',
});
expect(parsed.success).toBe(true);
});
});
Loading