Skip to content

Commit

Permalink
Exclude template instances from the no-response-body rule. (#2085)
Browse files Browse the repository at this point in the history
issue: #1785
  • Loading branch information
AlitzelMendez authored Jan 24, 2025
1 parent 069d0e4 commit db67bee
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/fix-duplicate-warning-2025-0-15-11-39-29.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-azure-resource-manager"
---

Exclude template instances from the `no-response-body` rule.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createRule, Operation } from "@typespec/compiler";
import { createRule, isTemplateInstance, Operation } from "@typespec/compiler";
import { getHttpOperation, getResponsesForOperation, HttpOperationResponse } from "@typespec/http";
import { isTemplatedInterfaceOperation } from "./utils.js";
/**
Expand All @@ -20,7 +20,12 @@ export const noResponseBodyRule = createRule({
return {
operation: (op: Operation) => {
const [httpOperation] = getHttpOperation(context.program, op);
if (isTemplatedInterfaceOperation(op) || httpOperation.verb === "head") return;
if (
isTemplateInstance(op) ||
isTemplatedInterfaceOperation(op) ||
httpOperation.verb === "head"
)
return;

const responses = getResponsesForOperation(context.program, op)[0].find(
(v) => v.statusCodes !== 204 && v.statusCodes !== 202,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,42 @@ describe("typespec-azure-resource-manager: no response body rule", () => {
)
.toBeValid();
});

it("allows templates but emit on use of the template ", async () => {
await tester
.expect(
`
@armProviderNamespace
@useDependency(Azure.ResourceManager.Versions.v1_0_Preview_1)
namespace Microsoft.ContosoProviderHub;
op SampleTemplate<
Resource extends Foundations.Resource,
Response extends {} = ArmResponse<Resource>
> is ArmCustomPatchAsync<Resource, Response = Response>;
model Image is Azure.ResourceManager.TrackedResource<{name: string}> {
...ResourceNameParameter<
Resource = Image
>;
}
@armResourceOperations
interface Images {
@parameterVisibility
update is SampleTemplate<
Image,
Response = (ArmAcceptedLroResponse<LroHeaders = Azure.Core.Foundations.RetryAfterHeader &
ArmLroLocationHeader<FinalResult = Image>> & {
@body _: Image;
})
>;
}
`,
)
.toEmitDiagnostics({
code: "@azure-tools/typespec-azure-resource-manager/no-response-body",
message: `The body of 202 response should be empty.`,
});
});
});

0 comments on commit db67bee

Please sign in to comment.