From bb655ca28f50f283b2b298023baa8c310521c430 Mon Sep 17 00:00:00 2001 From: Chenjie Shi Date: Tue, 14 May 2024 00:02:17 +0800 Subject: [PATCH] [tcgc] fix two issues found by go mpg (#826) resolve: https://github.com/Azure/autorest.go/issues/1304 --- .chronus/changes/main-2024-4-11-15-5-45.md | 7 +++ .chronus/changes/main-2024-4-11-15-7-31.md | 7 +++ .../src/public-utils.ts | 5 +- .../src/types.ts | 30 ++++++----- .../test/package.test.ts | 53 +++++++++++++++++++ .../test/public-utils.test.ts | 29 ++++++++++ 6 files changed, 116 insertions(+), 15 deletions(-) create mode 100644 .chronus/changes/main-2024-4-11-15-5-45.md create mode 100644 .chronus/changes/main-2024-4-11-15-7-31.md diff --git a/.chronus/changes/main-2024-4-11-15-5-45.md b/.chronus/changes/main-2024-4-11-15-5-45.md new file mode 100644 index 0000000000..3b1b757c77 --- /dev/null +++ b/.chronus/changes/main-2024-4-11-15-5-45.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +change from using logical result to final result \ No newline at end of file diff --git a/.chronus/changes/main-2024-4-11-15-7-31.md b/.chronus/changes/main-2024-4-11-15-7-31.md new file mode 100644 index 0000000000..03f0940df5 --- /dev/null +++ b/.chronus/changes/main-2024-4-11-15-7-31.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +add union support for templated model naming \ No newline at end of file diff --git a/packages/typespec-client-generator-core/src/public-utils.ts b/packages/typespec-client-generator-core/src/public-utils.ts index 31b22e5cbd..d9a45ebfa7 100644 --- a/packages/typespec-client-generator-core/src/public-utils.ts +++ b/packages/typespec-client-generator-core/src/public-utils.ts @@ -195,7 +195,10 @@ export function getLibraryName( type.templateMapper.args .filter( (arg): arg is Model | Enum => - "kind" in arg && (arg.kind === "Model" || arg.kind === "Enum") && arg.name.length > 0 + "kind" in arg && + (arg.kind === "Model" || arg.kind === "Enum" || arg.kind === "Union") && + arg.name !== undefined && + arg.name.length > 0 ) .map((arg) => pascalCase(arg.name)) .join("") diff --git a/packages/typespec-client-generator-core/src/types.ts b/packages/typespec-client-generator-core/src/types.ts index 3de24fdc41..d4f602588c 100644 --- a/packages/typespec-client-generator-core/src/types.ts +++ b/packages/typespec-client-generator-core/src/types.ts @@ -1302,22 +1302,24 @@ function updateTypesFromOperation( } const lroMetaData = getLroMetadata(program, operation); if (lroMetaData && generateConvenient) { - const logicalResults = diagnostics.pipe( - checkAndGetClientType(context, lroMetaData.logicalResult, operation) - ); - logicalResults.forEach((logicalResult) => { - updateUsageOfModel(context, UsageFlags.Output, logicalResult); - }); - - if (!context.arm) { - // TODO: currently skipping adding of envelopeResult due to arm error - // https://github.com/Azure/typespec-azure/issues/311 - const envelopeResults = diagnostics.pipe( - checkAndGetClientType(context, lroMetaData.envelopeResult, operation) + if (lroMetaData.finalResult !== undefined && lroMetaData.finalResult !== "void") { + const finalResults = diagnostics.pipe( + checkAndGetClientType(context, lroMetaData.finalResult, operation) ); - envelopeResults.forEach((envelopeResult) => { - updateUsageOfModel(context, UsageFlags.Output, envelopeResult); + finalResults.forEach((finalResult) => { + updateUsageOfModel(context, UsageFlags.Output, finalResult); }); + + if (!context.arm) { + // TODO: currently skipping adding of envelopeResult due to arm error + // https://github.com/Azure/typespec-azure/issues/311 + const envelopeResults = diagnostics.pipe( + checkAndGetClientType(context, lroMetaData.envelopeResult, operation) + ); + envelopeResults.forEach((envelopeResult) => { + updateUsageOfModel(context, UsageFlags.Output, envelopeResult); + }); + } } } return diagnostics.wrap(undefined); diff --git a/packages/typespec-client-generator-core/test/package.test.ts b/packages/typespec-client-generator-core/test/package.test.ts index 5240e34797..c94e29ad40 100644 --- a/packages/typespec-client-generator-core/test/package.test.ts +++ b/packages/typespec-client-generator-core/test/package.test.ts @@ -2736,6 +2736,8 @@ describe("typespec-client-generator-core: package", () => { strictEqual(method.name, "delete"); strictEqual(method.kind, "lro"); strictEqual(method.response.type, undefined); + strictEqual(runnerWithCore.context.experimental_sdkPackage.models.length, 0); + strictEqual(runnerWithCore.context.experimental_sdkPackage.enums.length, 1); }); it("paging", async () => { const runnerWithCore = await createSdkTestRunner({ @@ -3376,6 +3378,57 @@ describe("typespec-client-generator-core: package", () => { deepStrictEqual(headerParam.apiVersions, ["v2"]); }); }); + + describe("lro", () => { + it("customized lro delete", async () => { + const runnerWithCore = await createSdkTestRunner({ + librariesToAdd: [AzureCoreTestLibrary], + autoUsings: ["Azure.Core"], + emitterName: "@azure-tools/typespec-java", + }); + await runnerWithCore.compile(` + @versioned(MyVersions) + @server("http://localhost:3000", "endpoint") + @service({name: "Service"}) + namespace My.Service; + + enum MyVersions { + @useDependency(Versions.v1_0_Preview_2) + v1: "v1", + } + + op delete(): { + ...AcceptedResponse, + @header("Location") + @Azure.Core.pollingLocation(Azure.Core.StatusMonitorPollingOptions) + @Azure.Core.finalLocation(void) + location?: string; + }; + + @Azure.Core.lroStatus + union ResourceProvisioningState { + Succeeded: "Succeeded", + Failed: "Failed", + Canceled: "Canceled", + string, + } + + model ArmOperationStatus { + @Azure.Core.lroStatus + status: ResourceProvisioningState; + @key + @path + @segment("operationStatuses") + id: Azure.Core.uuid; + @visibility("read") + name?: string; + } + `); + const sdkPackage = runnerWithCore.context.experimental_sdkPackage; + strictEqual(sdkPackage.models.length, 0); + strictEqual(sdkPackage.enums.length, 1); + }); + }); }); function getServiceMethodOfClient( diff --git a/packages/typespec-client-generator-core/test/public-utils.test.ts b/packages/typespec-client-generator-core/test/public-utils.test.ts index b26677aa44..0c65ff1afa 100644 --- a/packages/typespec-client-generator-core/test/public-utils.test.ts +++ b/packages/typespec-client-generator-core/test/public-utils.test.ts @@ -880,6 +880,35 @@ describe("typespec-client-generator-core: public-utils", () => { const model = models.filter((x) => x.name === "ResourceOperationStatusUser")[0]; ok(model); }); + + it("template without @friendlyName renaming for union as enum", async () => { + await runner.compileWithBuiltInService(` + union DependencyOfOrigins { + serviceExplicitlyCreated: "ServiceExplicitlyCreated", + userExplicitlyCreated: "UserExplicitlyCreated", + string, + } + + model DependencyOfRelationshipProperties + is BaseRelationshipProperties; + + model BaseRelationshipProperties { + originInformation: RelationshipOriginInformation; + } + + model RelationshipOriginInformation { + relationshipOriginType: TOrigin; + } + + op test(): DependencyOfRelationshipProperties; + `); + const models = runner.context.experimental_sdkPackage.models; + strictEqual(models.length, 2); + const model = models.filter( + (x) => x.name === "RelationshipOriginInformationDependencyOfOrigins" + )[0]; + ok(model); + }); }); describe("getGeneratedName", () => {