Skip to content

Commit

Permalink
versioning - fixes crash when spreading incompatible versioned model …
Browse files Browse the repository at this point in the history
…as parameter to operation (microsoft#3951)

Fixes microsoft#3648

Co-authored-by: Christopher Radek <Christopher.Radek@microsoft.com>
  • Loading branch information
chrisradek and Christopher Radek authored Jul 23, 2024
1 parent 2914543 commit 755df74
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/versioning"
---

Fixes issue where spreading a versioned model as a parameter to an incompatible versioned operation would cause the compiler to crash.
5 changes: 5 additions & 0 deletions packages/versioning/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ export function $onValidate(program: Program) {
validateTargetVersionCompatible(program, op.interface, op, { isTargetADependent: true });
}
validateReference(program, op, op.returnType);

// Check that any spread/is/aliased models are valid for this operation
for (const sourceModel of op.parameters.sourceModels) {
validateReference(program, op, sourceModel.model);
}
},
interface: (iface) => {
for (const source of iface.sourceInterfaces) {
Expand Down
18 changes: 18 additions & 0 deletions packages/versioning/test/incompatible-versioning.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,24 @@ describe("versioning: validate incompatible references", () => {
);
});

it("emit diagnostic when versioned op has a newer versioned spread parameter", async () => {
const diagnostics = await runner.diagnose(`
@added(Versions.v2)
model MyOptions {
prop: string;
}
@added(Versions.v1)
op foo(...MyOptions,): void;
`);

expectDiagnostics(diagnostics, {
code: "@typespec/versioning/incompatible-versioned-reference",
message:
"'TestService.foo' was added in version 'v1' but referencing type 'TestService.MyOptions' added in version 'v2'.",
});
});

// TODO See: https://github.com/microsoft/typespec/issues/2695
it.skip("emit diagnostic when unversioned op based on a template has a versioned model as a parameter", async () => {
const diagnostics = await runner.diagnose(`
Expand Down

0 comments on commit 755df74

Please sign in to comment.