diff --git a/.chronus/changes/fix_unbranded_page_issue-2024-10-29-17-58-58.md b/.chronus/changes/fix_unbranded_page_issue-2024-10-29-17-58-58.md new file mode 100644 index 0000000000..c84433a208 --- /dev/null +++ b/.chronus/changes/fix_unbranded_page_issue-2024-10-29-17-58-58.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +fix wrong `unexpected-pageable-operation-return-type` because of nullable type \ No newline at end of file diff --git a/packages/typespec-client-generator-core/src/package.ts b/packages/typespec-client-generator-core/src/package.ts index 19d19a1ab4..f314793b09 100644 --- a/packages/typespec-client-generator-core/src/package.ts +++ b/packages/typespec-client-generator-core/src/package.ts @@ -140,11 +140,17 @@ function getSdkPagingServiceMethod(context, operation, client), ); + // nullable response type means the underlaying operation has multiple responses and only one of them is not empty, which is what we want + let responseType = basic.response.type; + if (responseType?.kind === "nullable") { + responseType = responseType.type; + } + // normal paging if (isList(context.program, operation)) { const pagingOperation = diagnostics.pipe(getPagingOperation(context.program, operation)); - if (basic.response.type?.__raw?.kind !== "Model" || !pagingOperation) { + if (responseType?.__raw?.kind !== "Model" || !pagingOperation) { diagnostics.add( createDiagnostic({ code: "unexpected-pageable-operation-return-type", @@ -163,18 +169,18 @@ function getSdkPagingServiceMethod p === pagingOperation.output.pageItems.property, ); const nextLinkPath = pagingOperation.output.nextLink ? getPropertyPathFromModel( context, - basic.response.type?.__raw, + responseType?.__raw, (p) => p === pagingOperation.output.nextLink!.property, ) : undefined; - context.__pagedResultSet.add(basic.response.type); + context.__pagedResultSet.add(responseType); // tcgc will let all paging method return a list of items basic.response.type = diagnostics.pipe( getClientTypeWithDiagnostics(context, pagingOperation?.output.pageItems.property.type), @@ -190,7 +196,7 @@ function getSdkPagingServiceMethod { strictEqual(response.resultPath, "tests"); }); + it("nullable paged result", async () => { + await runner.compileWithBuiltInService(` + @list + op test(): ListTestResult | NotFoundResponse; + model ListTestResult { + @pageItems + tests: Test[]; + @TypeSpec.nextLink + next: string; + } + model Test { + id: string; + } + `); + const sdkPackage = runner.context.sdkPackage; + const method = getServiceMethodOfClient(sdkPackage); + strictEqual(method.name, "test"); + strictEqual(method.kind, "paging"); + strictEqual(method.nextLinkPath, "next"); + + const response = method.response; + strictEqual(response.kind, "method"); + strictEqual(response.resultPath, "tests"); + }); + it("normal paged result with encoded name", async () => { await runner.compileWithBuiltInService(` @list