Skip to content

Fix circular-ref when navigating paging properties #7480

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

Merged
merged 3 commits into from
May 28, 2025
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: 7 additions & 0 deletions .chronus/changes/inifite-loop-2025-4-27-14-20-40.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/compiler"
---

Fix infinite recursion when navigating paging properties by detecting and handling circular model references.
9 changes: 6 additions & 3 deletions packages/compiler/src/lib/paging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,21 +298,24 @@ function navigateProperties(
type: Type,
callback: (prop: ModelProperty, path: ModelProperty[]) => void,
path: ModelProperty[] = [],
visited: Set<Type> = new Set(),
): void {
if (visited.has(type)) return;
visited.add(type);
switch (type.kind) {
case "Model":
for (const prop of type.properties.values()) {
callback(prop, [...path, prop]);
navigateProperties(prop.type, callback, [...path, prop]);
navigateProperties(prop.type, callback, [...path, prop], visited);
}
break;
case "Union":
for (const member of type.variants.values()) {
navigateProperties(member, callback, path);
navigateProperties(member, callback, path, visited);
}
break;
case "UnionVariant":
navigateProperties(type.type, callback, path);
navigateProperties(type.type, callback, path, visited);
break;
}
}
Expand Down
17 changes: 15 additions & 2 deletions packages/compiler/test/decorators/paging.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { beforeEach, describe, expect, it } from "vitest";
import { ignoreDiagnostics, ModelProperty, Operation } from "../../src/index.js";
import { getPagingOperation, PagingOperation } from "../../src/lib/paging.js";
import { expectDiagnostics } from "../../src/testing/expect.js";
import { expectDiagnosticEmpty, expectDiagnostics } from "../../src/testing/expect.js";
import { createTestRunner } from "../../src/testing/test-host.js";
import { BasicTestRunner } from "../../src/testing/types.js";

Expand Down Expand Up @@ -39,7 +39,20 @@ it("emit error if missing pageItems property", async () => {
});
});

describe("emit conflict diagnostic if multiple properties are annotated with teh same property marker", () => {
it("@list decorator handle recursive models without infinite loop", async () => {
const diagnostics = await runner.diagnose(`
model MyPage {
selfRef?: MyPage;
@pageItems items: string[];
@nextLink next: string;
}

@list op foo(): MyPage;
`);
expectDiagnosticEmpty(diagnostics);
});

describe("emit conflict diagnostic if multiple properties are annotated with the same property marker", () => {
it.each([
["offset", "int32"],
["pageSize", "int32"],
Expand Down
Loading