Skip to content

Commit

Permalink
Core: Fix rpc-operation-request-body rule (Azure#760)
Browse files Browse the repository at this point in the history
Turns out this rule was never actually looking for a request body at
all. It only cared if you were using the RpcOperation tempalte with
`@get` or `@delete`!

Fixes Azure#684.
  • Loading branch information
tjprescott authored Apr 29, 2024
1 parent 4f8caae commit 40c05ff
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@azure-tools/typespec-azure-core"
---

Fix `rpc-operation-request-body` rule not actually checking for a body parameter.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ export const rpcOperationRequestBodyRule = createRule({
operation.namespace?.namespace?.name === "Azure"
) {
const httpOperation = getHttpOperation(context.program, originalOperation)[0];
const bodyParam = httpOperation.parameters.body;
const verb = httpOperation.verb.toLowerCase();
if (verb === "get" || verb === "delete") {
if ((verb === "get" || verb === "delete") && bodyParam !== undefined) {
context.reportDiagnostic({
target: originalOperation,
messageId: "noBodyAllowed",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,38 @@ describe("typespec-azure-core: rpc-operation-request-body", () => {
);
});

it("allow RPCOperation with `@get` and empty body", async () => {
await tester
.expect(
`
model Widget {
name: string;
}
@get
@route ("/")
op getWidget is RpcOperation<{}, Widget>;
`
)
.toBeValid();
});

it("allow RPCOperation with `@delete` and empty body", async () => {
await tester
.expect(
`
model Widget {
name: string;
}
@delete
@route ("/")
op deleteWidget is RpcOperation<{}, Widget>;
`
)
.toBeValid();
});

it("emits warning when RPCOperation is marked with `@get` and has a request body", async () => {
await tester
.expect(
Expand Down

0 comments on commit 40c05ff

Please sign in to comment.