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 diff --git a/packages/typespec-client-generator-core/src/interfaces.ts b/packages/typespec-client-generator-core/src/interfaces.ts index ee2c24d3c0..cc7d59af21 100644 --- a/packages/typespec-client-generator-core/src/interfaces.ts +++ b/packages/typespec-client-generator-core/src/interfaces.ts @@ -638,7 +638,7 @@ 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, } interface SdkExampleBase { 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..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,17 +1473,33 @@ describe("typespec-client-generator-core: model types", () => { await runner.compileAndDiagnose(` @service({}) namespace MyService { - model Test { + model RoundTrip { + prop: string; + } + + model Input { prop: string; } - op test(@header("content-type") contentType: "application/xml", @body body: Test): Test; + @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[0].usage, UsageFlags.Input | UsageFlags.Output | UsageFlags.Xml); + strictEqual(models.length, 2); + const roundTripModel = models.find((x) => x.name === "RoundTrip"); + const inputModel = models.find((x) => x.name === "Input"); + ok(roundTripModel); + strictEqual( + roundTripModel.usage, + UsageFlags.Input | UsageFlags.Output | UsageFlags.Json | UsageFlags.Xml + ); + + ok(inputModel); + strictEqual(inputModel.usage, UsageFlags.Input | 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..443cf5a85f --- /dev/null +++ b/packages/typespec-client-generator-core/test/types/usage-flags.test.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from "vitest"; +import { UsageFlags } from "../../src/interfaces.js"; + +describe("typespec-client-generator-core: usage flags", () => { + 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); + } + } + }); +});