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 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
changeKind: breaking
packages:
- "@typespec/compiler"
---

Disallows overriding a required inherited property with an optional property.

In previous versions of TypeSpec, it was possible to override a required property with an optional property. This is no longer allowed. This change may result in errors in your code if you were relying on this bug, but specifications that used this behavior are likely to have been exposed to errors resulting from incoherent type checking behavior.

The following example demonstrates the behavior that is no longer allowed:

```tsp
model Base {
example: string;
}

model Child extends Base {
example?: string;
}
```

In this example, the `Child` model overrides the `example` property from the `Base` model with an optional property. This is no longer allowed.
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;
witemple-msft marked this conversation as resolved.
Show resolved Hide resolved
}

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