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

[core] Disallow overriding a required property with an optional property #3659

Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 17 additions & 1 deletion packages/compiler/src/core/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4618,6 +4618,8 @@ export function createChecker(program: Program): Checker {
const parentType = getTypeName(overriddenProp.type);
const newPropType = getTypeName(newProp.type);

let invalid = false;

if (!isAssignable) {
reportCheckerDiagnostic(
createDiagnostic({
Expand All @@ -4626,8 +4628,22 @@ export function createChecker(program: Program): Checker {
target: diagnosticTarget ?? newProp,
})
);
return;
invalid = true;
}

if (!overriddenProp.optional && newProp.optional) {
reportCheckerDiagnostic(
createDiagnostic({
code: "override-property-mismatch",
messageId: "disallowedOptionalOverride",
format: { propName: overriddenProp.name },
target: diagnosticTarget ?? newProp,
})
);
invalid = true;
}

if (invalid) return;
}

properties.set(newProp.name, newProp);
Expand Down
1 change: 1 addition & 0 deletions packages/compiler/src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ const diagnostics = {
severity: "error",
messages: {
default: paramMessage`Model has an inherited property named ${"propName"} of type ${"propType"} which cannot override type ${"parentType"}`,
disallowedOptionalOverride: paramMessage`Model has a required inherited property named ${"propName"} which cannot be overridden as optional`,
},
},
"extend-scalar": {
Expand Down
67 changes: 67 additions & 0 deletions packages/compiler/test/checker/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,73 @@ describe("compiler: models", () => {
]);
});

it("disallows subtype overriding required parent property with optional property", async () => {
testHost.addTypeSpecFile(
"main.tsp",
`
model A { x: int32; }
model B extends A { x?: int32; }
`
);

const diagnostics = await testHost.diagnose("main.tsp");
expectDiagnostics(diagnostics, [
{
code: "override-property-mismatch",
severity: "error",
message:
"Model has a required inherited property named x which cannot be overridden as optional",
},
]);
});

it("disallows subtype overriding required parent property with optional through multiple levels of inheritance", async () => {
testHost.addTypeSpecFile(
"main.tsp",
`
model A { x: int32; }
model B extends A { }
model C extends B { x?: int16; }
`
);

const diagnostics = await testHost.diagnose("main.tsp");
expectDiagnostics(diagnostics, [
{
code: "override-property-mismatch",
severity: "error",
message:
"Model has a required inherited property named x which cannot be overridden as optional",
},
]);
});

it("shows both errors when an override is optional and not assignable", async () => {
testHost.addTypeSpecFile(
"main.tsp",
`
model A { x: int32; }
model B extends A { x?: string; }
`
);

const diagnostics = await testHost.diagnose("main.tsp");
expectDiagnostics(diagnostics, [
{
code: "override-property-mismatch",
severity: "error",
message:
"Model has an inherited property named x of type string which cannot override type int32",
},
{
code: "override-property-mismatch",
severity: "error",
message:
"Model has a required inherited property named x which cannot be overridden as optional",
},
]);
});

it("allow multiple overrides", async () => {
testHost.addTypeSpecFile(
"main.tsp",
Expand Down
Loading