Skip to content

Commit

Permalink
[TCGC] Fix error response when error model has status code (#808)
Browse files Browse the repository at this point in the history
fixes #800
  • Loading branch information
msyyc authored and tadelesh committed May 10, 2024
1 parent 87e2e0c commit a8ac318
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .chronus/changes/fix-response-body-type-2024-4-9-11-14-24.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

Fix error response when error model has statusCode
6 changes: 5 additions & 1 deletion packages/typespec-client-generator-core/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
isSubscriptionId,
} from "./internal-utils.js";
import { createDiagnostic } from "./lib.js";
import { getEffectivePayloadType } from "./public-utils.js";
import {
addEncodeInfo,
addFormatInfo,
Expand Down Expand Up @@ -420,7 +421,10 @@ function getSdkHttpResponseAndExceptions(
);
}
contentTypes = contentTypes.concat(innerResponse.body.contentTypes);
body = innerResponse.body.type;
body =
innerResponse.body.type.kind === "Model"
? getEffectivePayloadType(context, innerResponse.body.type)
: innerResponse.body.type;
}
}

Expand Down
35 changes: 35 additions & 0 deletions packages/typespec-client-generator-core/test/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,41 @@ describe("typespec-client-generator-core: package", () => {
strictEqual(method.response.type, undefined);
strictEqual(method.response.resultPath, undefined);
});
it("basic returning void and error model has status code", async () => {
await runner.compileWithBuiltInService(
`
@error
model Error {
@statusCode _: 403;
code: int32;
message: string;
}
@delete op delete(@path id: string): void | Error;
`
);
const sdkPackage = runner.context.experimental_sdkPackage;
const method = getServiceMethodOfClient(sdkPackage);
strictEqual(sdkPackage.models.length, 1);
strictEqual(method.name, "delete");
const serviceResponses = method.operation.responses;
strictEqual(serviceResponses.size, 1);

const voidResponse = serviceResponses.get(204);
ok(voidResponse);
strictEqual(voidResponse.kind, "http");
strictEqual(voidResponse.type, undefined);
strictEqual(voidResponse.headers.length, 0);

const errorResponse = method.operation.exceptions.get(403);
ok(errorResponse);
strictEqual(errorResponse.kind, "http");
ok(errorResponse.type);
strictEqual(errorResponse.type.kind, "model");
strictEqual(errorResponse.type, sdkPackage.models[0]);

strictEqual(method.response.type, undefined);
strictEqual(method.response.resultPath, undefined);
});
it("basic returning model", async () => {
await runner.compileWithBuiltInService(
`
Expand Down

0 comments on commit a8ac318

Please sign in to comment.