Skip to content

Commit

Permalink
Showing 4 changed files with 45 additions and 7 deletions.
7 changes: 7 additions & 0 deletions packages/typespec-client-generator-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion packages/typespec-client-generator-core/package.json
Original file line number Diff line number Diff line change
@@ -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",
18 changes: 12 additions & 6 deletions packages/typespec-client-generator-core/src/package.ts
Original file line number Diff line number Diff line change
@@ -140,11 +140,17 @@ function getSdkPagingServiceMethod<TServiceOperation extends SdkServiceOperation
getSdkBasicServiceMethod<TServiceOperation>(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<TServiceOperation extends SdkServiceOperation

basic.response.resultPath = getPropertyPathFromModel(
context,
basic.response.type?.__raw,
responseType?.__raw,
(p) => 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<TServiceOperation extends SdkServiceOperation
// azure core paging
const pagedMetadata = getPagedResult(context.program, operation)!;

if (basic.response.type?.__raw?.kind !== "Model" || !pagedMetadata.itemsProperty) {
if (responseType?.__raw?.kind !== "Model" || !pagedMetadata.itemsProperty) {
diagnostics.add(
createDiagnostic({
code: "unexpected-pageable-operation-return-type",
@@ -207,7 +213,7 @@ function getSdkPagingServiceMethod<TServiceOperation extends SdkServiceOperation
});
}

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(
Original file line number Diff line number Diff line change
@@ -69,6 +69,31 @@ describe("typespec-client-generator-core: paged operation", () => {
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

0 comments on commit a6e4689

Please sign in to comment.