Skip to content

Commit

Permalink
Add operation status (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft authored Feb 23, 2024
1 parent 315ab0c commit 66b8ae2
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/add_operation_status-2024-1-23-16-6-53.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

getAllModels will return models only used as final envelope results
14 changes: 8 additions & 6 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1070,17 +1070,19 @@ function updateTypesFromOperation(
}
}
const lroMetaData = getLroMetadata(program, operation);
if (lroMetaData) {
if (lroMetaData && generateConvenient) {
const logicalResult = diagnostics.pipe(
checkAndGetClientType(context, lroMetaData.logicalResult, operation)
);
if (
logicalResult &&
["model", "enum", "array", "dict", "union"].includes(logicalResult.kind) &&
generateConvenient
) {
if (logicalResult) {
updateUsageOfModel(context, logicalResult, UsageFlags.Output);
}
const envelopeResult = diagnostics.pipe(
checkAndGetClientType(context, lroMetaData.envelopeResult, operation)
);
if (envelopeResult) {
updateUsageOfModel(context, envelopeResult, UsageFlags.Output);
}
}
return diagnostics.wrap(undefined);
}
Expand Down
85 changes: 85 additions & 0 deletions packages/typespec-client-generator-core/test/public-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AzureCoreTestLibrary } from "@azure-tools/typespec-azure-core/testing";
import {
Model,
ModelProperty,
Expand Down Expand Up @@ -1387,5 +1388,89 @@ describe("typespec-client-generator-core: public-utils", () => {
strictEqual(stringType.values[2].kind, "string");
});
});

describe("getLroMetadata", () => {
const lroCode = `
@versioned(Versions)
@service({title: "Test Service"})
namespace TestService;
alias ResourceOperations = Azure.Core.ResourceOperations<NoConditionalRequests &
NoRepeatableRequests &
NoClientRequestId>;
@doc("The API version.")
enum Versions {
@doc("The 2022-12-01-preview version.")
@useDependency(Azure.Core.Versions.v1_0_Preview_2)
v2022_12_01_preview: "2022-12-01-preview",
}
@resource("users")
@doc("Details about a user.")
model User {
@key
@visibility("read")
@doc("The name of user.")
name: string;
@doc("The role of user")
role: string;
}
@doc("The parameters for exporting a user.")
model UserExportParams {
@query
@doc("The format of the data.")
format: string;
}
@doc("The exported user data.")
model ExportedUser {
@doc("The name of user.")
name: string;
@doc("The exported URI.")
resourceUri: string;
}
op export is ResourceOperations.LongRunningResourceAction<User, UserExportParams, ExportedUser>;
`;
it("filter-out-core-models true", async () => {
const runnerWithCore = await createSdkTestRunner({
librariesToAdd: [AzureCoreTestLibrary],
autoUsings: ["Azure.Core", "Azure.Core.Traits"],
emitterName: "@azure-tools/typespec-java",
});
await runnerWithCore.compile(lroCode);
const models = getAllModelsAssertNoDiagnostics(runnerWithCore.context);
strictEqual(models.length, 1);
// there should only be one non-core model
strictEqual(models[0].name, "ExportedUser");
});
it("filter-out-core-models false", async () => {
const runnerWithCore = await createSdkTestRunner({
librariesToAdd: [AzureCoreTestLibrary],
autoUsings: ["Azure.Core", "Azure.Core.Traits"],
emitterName: "@azure-tools/typespec-java",
});
await runnerWithCore.compile(lroCode);
runnerWithCore.context.filterOutCoreModels = false;
const models = getAllModelsAssertNoDiagnostics(runnerWithCore.context);
strictEqual(models.length, 7);
// there should only be one non-core model
deepStrictEqual(
models.map((x) => x.name),
[
"ResourceOperationStatus",
"OperationState",
"Error",
"InnerError",
"ExportedUser",
"ErrorResponse",
"OperationStatus",
]
);
});
});
});
});

0 comments on commit 66b8ae2

Please sign in to comment.