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

Add operation status #305

Merged
merged 3 commits into from
Feb 23, 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/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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should generateConvenient impact only usage or the model detection? In other logics, it only impact usage.

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
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",
]
);
});
});
});
});
Loading