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

Fix the duplicate usage flag value #1231

Merged
merged 6 commits into from
Jul 25, 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/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
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);
}
}
});
});
Loading