Skip to content

Commit

Permalink
[tcgc] recursively check error models (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft authored Feb 27, 2024
1 parent e37cfa2 commit ff7726f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/error_models-2024-1-27-16-15-52.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

adds function and property to recursively check if a model is an error model
1 change: 1 addition & 0 deletions packages/typespec-client-generator-core/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ export interface SdkModelType extends SdkTypeBase {
properties: SdkModelPropertyType[];
name: string;
isFormDataType: boolean;
isError: boolean;
generatedName?: string;
description?: string;
details?: string;
Expand Down
12 changes: 12 additions & 0 deletions packages/typespec-client-generator-core/src/public-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getSummary,
ignoreDiagnostics,
Interface,
isErrorModel,
listServices,
Model,
ModelProperty,
Expand Down Expand Up @@ -532,3 +533,14 @@ function buildNameFromContextPaths(context: TCGCContext, contextPath: ContextNod
}
return createName;
}

export function isErrorOrChildOfError(context: TCGCContext, model: Model): boolean {
const errorDecorator = isErrorModel(context.program, model);
if (errorDecorator) return true;
let baseModel = model.baseModel;
while (baseModel) {
if (isErrorModel(context.program, baseModel)) return true;
baseModel = baseModel.baseModel;
}
return false;
}
2 changes: 2 additions & 0 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import {
getSdkTypeBaseHelper,
intOrFloat,
isAzureCoreModel,
isErrorOrChildOfError,
} from "./public-utils.js";

import { TCGCContext } from "./internal-utils.js";
Expand Down Expand Up @@ -501,6 +502,7 @@ export function getSdkModel(
usage: UsageFlags.None, // dummy value since we need to update models map before we can set this
crossLanguageDefinitionId: getCrossLanguageDefinitionId(type),
isFormDataType,
isError: isErrorOrChildOfError(context, type),
};

updateModelsMap(context, type, sdkType, operation);
Expand Down
51 changes: 51 additions & 0 deletions packages/typespec-client-generator-core/test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
SdkType,
SdkUnionType,
} from "../src/interfaces.js";
import { isErrorOrChildOfError } from "../src/public-utils.js";
import { getAllModels, getAllModelsWithDiagnostics, getSdkEnum, isReadOnly } from "../src/types.js";
import { SdkTestRunner, createSdkTestRunner, createTcgcTestRunnerForEmitter } from "./test-host.js";

Expand Down Expand Up @@ -2051,6 +2052,56 @@ describe("typespec-client-generator-core: types", () => {
const models = getAllModels(runner.context);
strictEqual(models.length, 2);
});
it("error model", async () => {
await runner.compileWithBuiltInService(`
@error
model ApiError {
code: string;
}
op test(): ApiError;
`);
const models = getAllModels(runner.context);
strictEqual(models.length, 1);
strictEqual(models[0].kind, "model");
strictEqual(models[0].isError, true);
const rawModel = models[0].__raw!;
strictEqual(rawModel.kind, "Model");
strictEqual(isErrorOrChildOfError(runner.context, rawModel), true);
});

it("error model inheritance", async () => {
await runner.compileWithBuiltInService(`
model ValidResponse {
prop: string;
};
@error
model ApiError {
code: string
};
model FourHundredError extends ApiError {};
model FourZeroFourError extends FourHundredError {};
model FiveHundredError extends ApiError {};
op test(): ValidResponse | FourZeroFourError | FiveHundredError;
`);
const models = getAllModels(runner.context);
strictEqual(models.length, 5);
const errorModels = models.filter((x) => x.kind === "model" && x.isError);
deepStrictEqual(errorModels.map((x) => x.name).sort(), [
"ApiError",
"FiveHundredError",
"FourHundredError",
"FourZeroFourError",
]);
const validModel = models.filter((x) => x.kind === "model" && !x.isError);
deepStrictEqual(
validModel.map((x) => x.name),
["ValidResponse"]
);
});
});
describe("SdkMultipartFormType", () => {
it("multipart form basic", async function () {
Expand Down

0 comments on commit ff7726f

Please sign in to comment.