Skip to content

Commit

Permalink
[core] Disallow overriding a required property with an optional prope…
Browse files Browse the repository at this point in the history
…rty (#3659)

Closes #3539 

This change disallows overriding a required inherited property with an
optional property.

This error does not take precedence over regular property assignability
rules. In case the property _both_ is optional when the overridden
property is required _and_ has a non-assignable type, both errors will
be reported.

---------

Co-authored-by: Will Temple <will@wtemple.net>
  • Loading branch information
witemple-msft and willmtemple committed Jul 10, 2024
1 parent fe345f2 commit 859f792
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
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;
}

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

0 comments on commit 859f792

Please sign in to comment.