Skip to content
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

[tcgc] deprecate Error usage and add Exception usage. for all models used in exception response, they will no longer have Output usage, but have Exception usage. #1854

Merged
merged 8 commits into from
Nov 14, 2024
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/exception_usage-2024-10-13-11-50-32.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: breaking
packages:
- "@azure-tools/typespec-client-generator-core"
---

deprecate `Error` usage and add `Exception` usage. for all models used in exception response, they will no longer have `Output` usage, but have `Exception` usage.
9 changes: 7 additions & 2 deletions packages/typespec-client-generator-core/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,12 +730,17 @@ export enum UsageFlags {
MultipartFormData = 1 << 5,
// Used in spread.
Spread = 1 << 6,
/**
* @deprecated Use `Exception` instead.
*/
// Output will also be set when Error is set.
Error = 1 << 7,
// Set when model is used in conjunction with an application/json content type.
// Set when type is used in conjunction with an application/json content type.
Json = 1 << 8,
// Set when model is used in conjunction with an application/xml content type.
// Set when type is used in conjunction with an application/xml content type.
Xml = 1 << 9,
// Set when type is used for exception output.
Exception = 1 << 10,
}

interface SdkExampleBase {
Expand Down
8 changes: 6 additions & 2 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ export function getSdkModelWithDiagnostics(

if (!sdkType) {
const name = getLibraryName(context, type) || getGeneratedName(context, type, operation);
const usage = isErrorModel(context.program, type) ? UsageFlags.Error : UsageFlags.None; // initial usage we can tell just by looking at the model
const usage = isErrorModel(context.program, type) ? UsageFlags.Error : UsageFlags.None; // eslint-disable-line @typescript-eslint/no-deprecated
sdkType = {
...diagnostics.pipe(getSdkTypeBaseHelper(context, type, "model")),
name: name,
Expand Down Expand Up @@ -1603,7 +1603,11 @@ function updateTypesFromOperation(
: innerResponse.body.type;
const sdkType = diagnostics.pipe(getClientTypeWithDiagnostics(context, body, operation));
if (generateConvenient) {
diagnostics.pipe(updateUsageOrAccess(context, UsageFlags.Output, sdkType));
if (response.statusCodes === "*" || isErrorModel(context.program, body)) {
diagnostics.pipe(updateUsageOrAccess(context, UsageFlags.Exception, sdkType));
} else {
diagnostics.pipe(updateUsageOrAccess(context, UsageFlags.Output, sdkType));
}

if (innerResponse.body.contentTypes.some((x) => isJsonContentType(x))) {
diagnostics.pipe(updateUsageOrAccess(context, UsageFlags.Json, sdkType));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AzureCoreTestLibrary } from "@azure-tools/typespec-azure-core/testing";
import { isErrorModel } from "@typespec/compiler";
import { deepStrictEqual, ok, strictEqual } from "assert";
import { beforeEach, describe, it } from "vitest";
import { SdkBodyModelPropertyType, UsageFlags } from "../../src/interfaces.js";
Expand Down Expand Up @@ -1420,16 +1419,9 @@ describe("typespec-client-generator-core: model types", () => {
`);
const models = getAllModels(runner.context);
strictEqual(models.length, 1);
strictEqual(models[0].kind, "model");
ok(models[0].usage & UsageFlags.Error);

const model = models[0];
const rawModel = model.__raw;
ok(rawModel);
strictEqual(rawModel.kind, "Model");
strictEqual(isErrorModel(runner.context.program, rawModel), true);
ok(model.usage & UsageFlags.Output);
ok(model.usage & UsageFlags.Error);
strictEqual(model.kind, "model");
ok(model.usage & UsageFlags.Exception);
});

it("error model inheritance", async () => {
Expand Down
Loading