From 2aacdc19c2428910538943a94f2e930c6fba1ceb Mon Sep 17 00:00:00 2001 From: Dapeng Zhang Date: Wed, 24 Jul 2024 16:44:09 +0800 Subject: [PATCH 1/5] fix xml usageflag issue --- packages/typespec-client-generator-core/src/interfaces.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/typespec-client-generator-core/src/interfaces.ts b/packages/typespec-client-generator-core/src/interfaces.ts index 7b5301a23f..e3ab0a28ac 100644 --- a/packages/typespec-client-generator-core/src/interfaces.ts +++ b/packages/typespec-client-generator-core/src/interfaces.ts @@ -636,5 +636,5 @@ export enum UsageFlags { // Set when model 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. - Xml = 1 << 8, + Xml = 1 << 9, } From f7873f4a94e89b35affdde699cb3d75e7b9d12e3 Mon Sep 17 00:00:00 2001 From: Dapeng Zhang Date: Wed, 24 Jul 2024 17:33:25 +0800 Subject: [PATCH 2/5] fix another issue and update test cases --- .../src/types.ts | 2 +- .../test/types/model-types.test.ts | 5 +++- .../test/types/usage-flags.test.ts | 23 +++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 packages/typespec-client-generator-core/test/types/usage-flags.test.ts diff --git a/packages/typespec-client-generator-core/src/types.ts b/packages/typespec-client-generator-core/src/types.ts index 90312768b4..6b2a9d8952 100644 --- a/packages/typespec-client-generator-core/src/types.ts +++ b/packages/typespec-client-generator-core/src/types.ts @@ -1568,7 +1568,7 @@ function updateTypesFromOperation( updateUsageOfModel(context, UsageFlags.Json, sdkType); } if (httpBody.contentTypes.some((x) => isXmlContentType(x))) { - updateUsageOfModel(context, UsageFlags.Json, sdkType); + updateUsageOfModel(context, UsageFlags.Xml, sdkType); } if (httpBody.contentTypes.includes("application/merge-patch+json")) { // will also have Json type diff --git a/packages/typespec-client-generator-core/test/types/model-types.test.ts b/packages/typespec-client-generator-core/test/types/model-types.test.ts index 9edf049157..3e2d362fb1 100644 --- a/packages/typespec-client-generator-core/test/types/model-types.test.ts +++ b/packages/typespec-client-generator-core/test/types/model-types.test.ts @@ -1484,6 +1484,9 @@ describe("typespec-client-generator-core: model types", () => { const models = runner.context.sdkPackage.models; strictEqual(models.length, 1); strictEqual(models[0].name, "Test"); - strictEqual(models[0].usage, UsageFlags.Input | UsageFlags.Output | UsageFlags.Xml); + strictEqual( + models[0].usage, + UsageFlags.Input | UsageFlags.Output | UsageFlags.Json | UsageFlags.Xml + ); }); }); diff --git a/packages/typespec-client-generator-core/test/types/usage-flags.test.ts b/packages/typespec-client-generator-core/test/types/usage-flags.test.ts new file mode 100644 index 0000000000..9a3129b6d9 --- /dev/null +++ b/packages/typespec-client-generator-core/test/types/usage-flags.test.ts @@ -0,0 +1,23 @@ +import { beforeEach, describe, expect, it } from "vitest"; +import { UsageFlags } from "../../src/interfaces.js"; +import { SdkTestRunner, createSdkTestRunner } from "../test-host.js"; + +describe("typespec-client-generator-core: usage flags", () => { + let runner: SdkTestRunner; + + beforeEach(async () => { + runner = await createSdkTestRunner({ emitterName: "@azure-tools/typespec-java" }); + }); + + it("all possible values in UsageFlags should be orthogonal", async () => { + const values = Object.values(UsageFlags).filter( + (value) => typeof value === "number" + ) as number[]; + + for (let i = 0; i < values.length; i++) { + for (let j = i + 1; j < values.length; j++) { + expect(values[i] & values[j]).toBe(0); + } + } + }); +}); From b453a9e530e0bb6d60a1b6f852dd732f30669111 Mon Sep 17 00:00:00 2001 From: Dapeng Zhang Date: Wed, 24 Jul 2024 17:38:47 +0800 Subject: [PATCH 3/5] add another input case --- .../test/types/model-types.test.ts | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/typespec-client-generator-core/test/types/model-types.test.ts b/packages/typespec-client-generator-core/test/types/model-types.test.ts index 3e2d362fb1..7574650c58 100644 --- a/packages/typespec-client-generator-core/test/types/model-types.test.ts +++ b/packages/typespec-client-generator-core/test/types/model-types.test.ts @@ -1473,20 +1473,33 @@ describe("typespec-client-generator-core: model types", () => { await runner.compileAndDiagnose(` @service({}) namespace MyService { - model Test { + model RoundTrip { prop: string; } - op test(@header("content-type") contentType: "application/xml", @body body: Test): Test; + model Input { + prop: string; + } + + @route("/test1") + op test1(@header("content-type") contentType: "application/xml", @body body: RoundTrip): RoundTrip; + + @route("/test2") + op test2(@header("content-type") contentType: "application/xml", @body body: Input): void; } `); const models = runner.context.sdkPackage.models; - strictEqual(models.length, 1); - strictEqual(models[0].name, "Test"); + strictEqual(models.length, 2); + const roundTripModel = models.find((x) => x.name === "RoundTrip"); + const inputModel = models.find((x) => x.name === "Input"); + ok(roundTripModel); strictEqual( - models[0].usage, + roundTripModel.usage, UsageFlags.Input | UsageFlags.Output | UsageFlags.Json | UsageFlags.Xml ); + + ok(inputModel); + strictEqual(inputModel.usage, UsageFlags.Input | UsageFlags.Xml); }); }); From a39024c7f221a9b1e8ed2b0d1608b68111963051 Mon Sep 17 00:00:00 2001 From: Dapeng Zhang Date: Thu, 25 Jul 2024 10:21:49 +0800 Subject: [PATCH 4/5] clean up unused vars --- .../test/types/usage-flags.test.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/typespec-client-generator-core/test/types/usage-flags.test.ts b/packages/typespec-client-generator-core/test/types/usage-flags.test.ts index 9a3129b6d9..443cf5a85f 100644 --- a/packages/typespec-client-generator-core/test/types/usage-flags.test.ts +++ b/packages/typespec-client-generator-core/test/types/usage-flags.test.ts @@ -1,14 +1,7 @@ -import { beforeEach, describe, expect, it } from "vitest"; +import { describe, expect, it } from "vitest"; import { UsageFlags } from "../../src/interfaces.js"; -import { SdkTestRunner, createSdkTestRunner } from "../test-host.js"; describe("typespec-client-generator-core: usage flags", () => { - let runner: SdkTestRunner; - - beforeEach(async () => { - runner = await createSdkTestRunner({ emitterName: "@azure-tools/typespec-java" }); - }); - it("all possible values in UsageFlags should be orthogonal", async () => { const values = Object.values(UsageFlags).filter( (value) => typeof value === "number" From 24816140cb14a9bd518f6adcb36aef9a4280397d Mon Sep 17 00:00:00 2001 From: Dapeng Zhang Date: Thu, 25 Jul 2024 10:23:10 +0800 Subject: [PATCH 5/5] add changeset --- .chronus/changes/fix-usage-issue-2024-6-25-10-23-1.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .chronus/changes/fix-usage-issue-2024-6-25-10-23-1.md diff --git a/.chronus/changes/fix-usage-issue-2024-6-25-10-23-1.md b/.chronus/changes/fix-usage-issue-2024-6-25-10-23-1.md new file mode 100644 index 0000000000..b9df7492f4 --- /dev/null +++ b/.chronus/changes/fix-usage-issue-2024-6-25-10-23-1.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +fix the duplicate usageflags values for json and xml \ No newline at end of file