Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): bogus validation error when merging imported enums #1861

Merged
merged 2 commits into from
Nov 15, 2024
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
7 changes: 3 additions & 4 deletions packages/schema/src/cli/cli-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,10 @@ export async function loadDocument(fileName: string, validateOnly = false): Prom
const imported = mergeImportsDeclarations(langiumDocuments, model);

// remove imported documents
await services.shared.workspace.DocumentBuilder.update(
[],
imported.map((m) => m.$document!.uri)
);
imported.forEach((model) => langiumDocuments.deleteDocument(model.$document!.uri));
services.shared.workspace.IndexManager.remove(imported.map((model) => model.$document!.uri));

// extra validation after merging imported declarations
ymc9 marked this conversation as resolved.
Show resolved Hide resolved
validationAfterImportMerge(model);

// merge fields and attributes from base models
Expand Down
84 changes: 1 addition & 83 deletions packages/schema/src/language-server/validator/utils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import {
AttributeArg,
AttributeParam,
BuiltinType,
DataModelAttribute,
DataModelFieldAttribute,
Expression,
ExpressionType,
InternalAttribute,
isArrayExpr,
isDataModelField,
isEnum,
isMemberAccessExpr,
isReferenceExpr,
isStringLiteral,
} from '@zenstackhq/language/ast';
import { isAuthInvocation, resolved } from '@zenstackhq/sdk';
import { isAuthInvocation } from '@zenstackhq/sdk';
import { AstNode, ValidationAcceptor } from 'langium';

/**
Expand Down Expand Up @@ -109,80 +101,6 @@ export function mapBuiltinTypeToExpressionType(
}
}

/**
* Determines if the given attribute argument is assignable to the given attribute parameter
*/
export function assignableToAttributeParam(
arg: AttributeArg,
param: AttributeParam,
attr: DataModelAttribute | DataModelFieldAttribute | InternalAttribute
): boolean {
const argResolvedType = arg.$resolvedType;
if (!argResolvedType) {
return false;
}

let dstType = param.type.type;
let dstIsArray = param.type.array;
const dstRef = param.type.reference;

if (dstType === 'Any' && !dstIsArray) {
return true;
}

// destination is field reference or transitive field reference, check if
// argument is reference or array or reference
if (dstType === 'FieldReference' || dstType === 'TransitiveFieldReference') {
if (dstIsArray) {
return (
isArrayExpr(arg.value) &&
!arg.value.items.find((item) => !isReferenceExpr(item) || !isDataModelField(item.target.ref))
);
} else {
return isReferenceExpr(arg.value) && isDataModelField(arg.value.target.ref);
}
}

if (isEnum(argResolvedType.decl)) {
// enum type

let attrArgDeclType = dstRef?.ref;
if (dstType === 'ContextType' && isDataModelField(attr.$container) && attr.$container?.type?.reference) {
// attribute parameter type is ContextType, need to infer type from
// the attribute's container
attrArgDeclType = resolved(attr.$container.type.reference);
dstIsArray = attr.$container.type.array;
}
return attrArgDeclType === argResolvedType.decl && dstIsArray === argResolvedType.array;
} else if (dstType) {
// scalar type

if (typeof argResolvedType?.decl !== 'string') {
// destination type is not a reference, so argument type must be a plain expression
return false;
}

if (dstType === 'ContextType') {
// attribute parameter type is ContextType, need to infer type from
// the attribute's container
if (isDataModelField(attr.$container)) {
if (!attr.$container?.type?.type) {
return false;
}
dstType = mapBuiltinTypeToExpressionType(attr.$container.type.type);
dstIsArray = attr.$container.type.array;
} else {
dstType = 'Any';
}
}

return typeAssignable(dstType, argResolvedType.decl, arg.value) && dstIsArray === argResolvedType.array;
} else {
// reference type
return (dstRef?.ref === argResolvedType.decl || dstType === 'Any') && dstIsArray === argResolvedType.array;
}
}

export function isAuthOrAuthMemberAccess(expr: Expression): boolean {
return isAuthInvocation(expr) || (isMemberAccessExpr(expr) && isAuthOrAuthMemberAccess(expr.operand));
}
24 changes: 24 additions & 0 deletions tests/regression/tests/issue-1849.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { FILE_SPLITTER, loadSchema } from '@zenstackhq/testtools';

describe('issue 1849', () => {
it('regression', async () => {
await loadSchema(
`schema.zmodel
import './enum'

model Post {
id Int @id
status Status @default(PUBLISHED)
}

${FILE_SPLITTER}enum.zmodel

enum Status {
PENDING
PUBLISHED
}
`,
{ provider: 'postgresql', pushDb: false }
);
});
});
Loading