Skip to content

Commit

Permalink
Fix the duplicate usage flag value (#1231)
Browse files Browse the repository at this point in the history
The `Xml` in `UsageFlag` accidentally to have a duplicate value with an
existing one, this PR fixes it.
  • Loading branch information
ArcturusZhang committed Jul 25, 2024
1 parent 374c373 commit 01e5e0d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/fix-usage-issue-2024-6-25-10-23-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

fix the duplicate usageflags values for json and xml
2 changes: 1 addition & 1 deletion packages/typespec-client-generator-core/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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);
}
}
});
});

0 comments on commit 01e5e0d

Please sign in to comment.