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

[tcgc] add UsageFlags.JsonMergePatch #455

Merged
merged 9 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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/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
Original file line number Diff line number Diff line change
Expand Up @@ -529,4 +529,5 @@ export enum UsageFlags {
Input = 1 << 1,
Output = 1 << 2,
Versioning = 1 << 3,
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved
JsonMergePatch = 1 << 4,
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved
}
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 @@ -1264,14 +1264,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/json-merge-patch")) {
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved
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/json-merge-patch"): 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
Loading