diff --git a/packages/typespec-client-generator-core/CHANGELOG.md b/packages/typespec-client-generator-core/CHANGELOG.md index 0b3b33e13c..99f0d1dabd 100644 --- a/packages/typespec-client-generator-core/CHANGELOG.md +++ b/packages/typespec-client-generator-core/CHANGELOG.md @@ -1,5 +1,12 @@ # Change Log - @azure-tools/typespec-client-generator-core +## 0.48.4 + +### Bug Fixes + +- [#1935](https://github.com/Azure/typespec-azure/pull/1935) fix wrong `unexpected-pageable-operation-return-type` because of nullable type + + ## 0.48.3 ### Bug Fixes diff --git a/packages/typespec-client-generator-core/package.json b/packages/typespec-client-generator-core/package.json index ee4de77310..973a260e6d 100644 --- a/packages/typespec-client-generator-core/package.json +++ b/packages/typespec-client-generator-core/package.json @@ -1,6 +1,6 @@ { "name": "@azure-tools/typespec-client-generator-core", - "version": "0.48.3", + "version": "0.48.4", "author": "Microsoft Corporation", "description": "TypeSpec Data Plane Generation library", "homepage": "https://azure.github.io/typespec-azure", 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