Skip to content

Commit

Permalink
[tcgc] add UsageFlags.JsonMergePatch (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft authored Mar 22, 2024
1 parent 1bd2081 commit 76cd217
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/move_patch_to_usage-2024-2-20-15-40-32.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

We've added Usage.JsonMergePatch. Usage.Input continues to refer to all inputs, Usage.JsonMergePatch is set if a model is explicitly set as JSON merge patch input body
1 change: 1 addition & 0 deletions packages/typespec-client-generator-core/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,5 @@ export enum UsageFlags {
Input = 1 << 1,
Output = 1 << 2,
ApiVersionEnum = 1 << 3,
JsonMergePatch = 1 << 4,
}
12 changes: 8 additions & 4 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1338,14 +1338,18 @@ function updateTypesFromOperation(
});
}
}
if (httpOperation.parameters.body) {
const bodies = diagnostics.pipe(
checkAndGetClientType(context, httpOperation.parameters.body.type, operation)
);
const httpBody = httpOperation.parameters.body;
if (httpBody) {
const bodies = diagnostics.pipe(checkAndGetClientType(context, httpBody.type, operation));
if (generateConvenient) {
bodies.forEach((body) => {
updateUsageOfModel(context, UsageFlags.Input, body);
});
if (httpBody.contentTypes.includes("application/merge-patch+json")) {
bodies.forEach((body) => {
updateUsageOfModel(context, UsageFlags.JsonMergePatch, body);
});
}
}
}
for (const response of httpOperation.responses) {
Expand Down
34 changes: 32 additions & 2 deletions packages/typespec-client-generator-core/test/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Model,
Namespace,
Operation,
UsageFlags,
ignoreDiagnostics,
} from "@typespec/compiler";
import { expectDiagnostics } from "@typespec/compiler/testing";
Expand All @@ -22,7 +21,7 @@ import {
shouldGenerateConvenient,
shouldGenerateProtocol,
} from "../src/decorators.js";
import { SdkOperationGroup } from "../src/interfaces.js";
import { SdkOperationGroup, UsageFlags } from "../src/interfaces.js";
import { getCrossLanguageDefinitionId, getCrossLanguagePackageId } from "../src/public-utils.js";
import { getAllModels } from "../src/types.js";
import { SdkTestRunner, createSdkContextTestHelper, createSdkTestRunner } from "./test-host.js";
Expand Down Expand Up @@ -2248,6 +2247,37 @@ describe("typespec-client-generator-core: decorators", () => {

strictEqual(getUsage(runner.context, Dog), UsageFlags.Output);
});

it("patch usage", async () => {
const { PatchModel, JsonMergePatchModel } = (await runner.compile(`
@service({})
@test namespace MyService {
@test
model PatchModel {
age: int32;
}
@test
model JsonMergePatchModel {
prop: string
}
@patch
@route("/patch")
op patchModel(@body body: PatchModel): void;
@patch
@route("/jsonMergePatch")
op jsonMergePatchModel(@body body: JsonMergePatchModel, @header contentType: "application/merge-patch+json"): void;
}
`)) as { PatchModel: Model; JsonMergePatchModel: Model };

strictEqual(getUsage(runner.context, PatchModel), UsageFlags.Input);
strictEqual(
getUsage(runner.context, JsonMergePatchModel),
UsageFlags.JsonMergePatch | UsageFlags.Input
);
});
});

describe("@flattenProperty", () => {
Expand Down

0 comments on commit 76cd217

Please sign in to comment.