-
Notifications
You must be signed in to change notification settings - Fork 72
Add linter rule arm-post-lro-response-mismatch for ARM POST LRO operations
#4145
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
Draft
Copilot
wants to merge
20
commits into
main
Choose a base branch
from
copilot/add-linter-for-arm-post-lro
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
4b6e875
Initial plan
Copilot 28bebb7
Add linter rule arm-post-lro-response-mismatch for ARM POST LRO opera…
Copilot e77b24d
Add codefix to suggest setting FinalResult in LroHeaders for arm-post…
Copilot 142de4b
Merge remote-tracking branch 'origin/main' into copilot/add-linter-fo…
Copilot a9c28e6
Address review: support ActionAsync, provider actions, fix test wording
Copilot 7e87831
Regenerate docs for typespec-azure-resource-manager
Copilot fcb4809
Add fallback for non-template operations: check 200 response body vs …
Copilot 47c4e27
Merge remote-tracking branch 'origin/main' into copilot/add-linter-fo…
Copilot 5c99138
Use resolveArmResources for both resource and provider actions, remov…
Copilot 765f709
Reset core submodule pointer to match main
Copilot db53844
Merge remote-tracking branch 'origin/main' into copilot/add-linter-fo…
Copilot 834bd4b
Address review: fix codefix text, support multiple responses, catch t…
Copilot f84d369
Merge remote-tracking branch 'origin/main' into copilot/add-linter-fo…
Copilot e8aef92
Fix void matching: use isVoidType for builtin void comparison, add te…
Copilot e5dbc09
Merge remote-tracking branch 'origin/main' into copilot/add-linter-fo…
Copilot 3785cc9
Merge remote-tracking branch 'origin/main' into copilot/add-linter-fo…
Copilot ebf307a
Handle Scalar and UnknownType in response template param, add tests f…
Copilot da8e547
Remove redundant Scalar type cast and unused import
Copilot e16023d
Merge remote-tracking branch 'origin/main' into copilot/add-linter-fo…
Copilot 1196313
Allow void types in Response template param, refactor tests to use it…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
.chronus/changes/arm-post-lro-response-mismatch-2026-03-28-00-40-00.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@azure-tools/typespec-azure-resource-manager" | ||
| - "@azure-tools/typespec-azure-rulesets" | ||
| --- | ||
|
|
||
| Add linter rule `arm-post-lro-response-mismatch` to warn when a long-running POST operation's final result type does not match the 200 response body |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
208 changes: 208 additions & 0 deletions
208
packages/typespec-azure-resource-manager/src/rules/arm-post-lro-response-mismatch.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| import { | ||
| CodeFix, | ||
| Operation, | ||
| Program, | ||
| Type, | ||
| createRule, | ||
| getSourceLocation, | ||
| isVoidType, | ||
| } from "@typespec/compiler"; | ||
| import { | ||
| type OperationSignatureReferenceNode, | ||
| type OperationStatementNode, | ||
| SyntaxKind, | ||
| type TemplateArgumentNode, | ||
| } from "@typespec/compiler/ast"; | ||
| import { $ } from "@typespec/compiler/typekit"; | ||
|
|
||
| import { getLroMetadata } from "@azure-tools/typespec-azure-core"; | ||
| import { HttpOperationResponse, HttpPayloadBody } from "@typespec/http"; | ||
| import { ArmResourceOperation } from "../operations.js"; | ||
| import { resolveArmResources } from "../resource.js"; | ||
|
|
||
| function createLroHeadersCodeFix(op: Operation, responseTypeName: string): CodeFix | undefined { | ||
| const node = op.node; | ||
| if (node === undefined || node.kind !== SyntaxKind.OperationStatement) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const opNode = node as OperationStatementNode; | ||
| const signature = opNode.signature; | ||
| if (signature.kind !== SyntaxKind.OperationSignatureReference) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const sigRef = signature as OperationSignatureReferenceNode; | ||
| const templateArgs: readonly TemplateArgumentNode[] = sigRef.baseOperation.arguments; | ||
| const lroHeadersArg = templateArgs.find((arg) => arg.name?.sv === "LroHeaders"); | ||
|
|
||
| if (lroHeadersArg === undefined) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return { | ||
| id: "arm-post-lro-set-final-result", | ||
| label: `Set FinalResult to ${responseTypeName} in LroHeaders`, | ||
| fix(context) { | ||
| const argValueLocation = getSourceLocation(lroHeadersArg.argument); | ||
| return context.replaceText( | ||
| argValueLocation, | ||
| `ArmLroLocationHeader<FinalResult = ${responseTypeName}>`, | ||
| ); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Get the Response type from an operation's template parameter. | ||
| * Looks at the operation's immediate sourceOperation to find a template parameter named "Response" | ||
| * and returns the resolved type. The Response type can be a Model, Scalar, UnknownType, or void | ||
| * (as per the constraint on ArmResourceActionAsync: `Response extends Model | unknown | void`). | ||
| * Only checks the first level of template indirection to avoid picking up internal | ||
| * template parameters (e.g., ArmResourceActionAsyncBase's Response parameter). | ||
| */ | ||
| function getResponseTemplateParam(op: Operation): Type | undefined { | ||
| // The operation itself is a resolved instance, so we need to look at the | ||
| // sourceOperation (the template). The templateMapper on sourceOperation | ||
| // contains the resolved template arguments. | ||
| const sourceOp: Operation | undefined = op.sourceOperation; | ||
| const mapper = sourceOp?.templateMapper; | ||
| if (sourceOp === undefined || mapper === undefined) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const templateParams = sourceOp.node?.templateParameters; | ||
| if (templateParams === undefined) { | ||
| return undefined; | ||
| } | ||
|
|
||
| for (let i = 0; i < templateParams.length; i++) { | ||
| if (templateParams[i].id.sv === "Response") { | ||
| const resolvedType = mapper.args[i]; | ||
| if (typeof resolvedType === "object" && "kind" in resolvedType) { | ||
| return resolvedType as Type; | ||
| } | ||
| return undefined; | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Get the body payload from an HTTP response's 200 status code, if present. | ||
| */ | ||
| function getResponseBody(responses: HttpOperationResponse[]): HttpPayloadBody | undefined { | ||
| const response200 = responses.find((r) => r.statusCodes === 200); | ||
| if (response200 === undefined) return undefined; | ||
| if (response200.responses.length === 0) return undefined; | ||
| if (response200.responses[0].body !== undefined) { | ||
| return response200.responses[0].body; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Get a printable name for a type, if available. | ||
| * Handles Model, Scalar, and Intrinsic types (including void, unknown, etc.). | ||
| */ | ||
| function getTypeName(type: Type): string | undefined { | ||
| switch (type.kind) { | ||
| case "Model": | ||
| return type.name; | ||
| case "Scalar": | ||
| return type.name; | ||
| case "Intrinsic": | ||
| return type.name; | ||
| default: | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Verify that the final result of an ARM LRO POST operation matches the Response parameter. | ||
| */ | ||
| export const armPostLroResponseMismatchRule = createRule({ | ||
| name: "arm-post-lro-response-mismatch", | ||
| severity: "warning", | ||
| url: "https://azure.github.io/typespec-azure/docs/libraries/azure-resource-manager/rules/post-lro-response-mismatch", | ||
| description: | ||
| "Ensure that the final result of a long-running POST operation matches the Response parameter.", | ||
| messages: { | ||
| default: `The final result type of a long-running POST operation does not match the Response parameter. Specify the FinalResult in the LroHeaders parameter to match the response type. For example: 'LroHeaders = ArmLroLocationHeader<FinalResult = ResponseType>'.`, | ||
| }, | ||
| create(context) { | ||
| /** | ||
| * Checks if the finalResult type matches the expected response type. | ||
| * Returns true if they match or if the comparison is not applicable. | ||
| * Returns false if there is a mismatch. | ||
| */ | ||
| function doesFinalResultMatch(finalResult: Type | "void", expectedResponseType: Type): boolean { | ||
| if (finalResult === "void") { | ||
markcowl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return isVoidType(expectedResponseType); | ||
| } | ||
| return $(context.program).type.isAssignableTo(finalResult, expectedResponseType, finalResult); | ||
| } | ||
|
|
||
| function validateOperation(op: ArmResourceOperation) { | ||
| if (op.httpOperation.verb !== "post") { | ||
| return; | ||
| } | ||
| const lroMetadata = getLroMetadata(context.program, op.operation); | ||
| if (lroMetadata === undefined) { | ||
| return; | ||
| } | ||
|
|
||
| const { finalResult } = lroMetadata; | ||
| if (finalResult === undefined) { | ||
| return; | ||
| } | ||
|
|
||
| // Get the Response type from the template parameter | ||
| const responseType = getResponseTemplateParam(op.operation); | ||
|
|
||
| if (responseType !== undefined) { | ||
| // Template-based detection: Response template param is non-void but finalResult doesn't match | ||
| if (!doesFinalResultMatch(finalResult, responseType)) { | ||
| const responseTypeName = getTypeName(responseType); | ||
| const codefixes: CodeFix[] = []; | ||
| if (responseTypeName !== undefined) { | ||
| const codeFix = createLroHeadersCodeFix(op.operation, responseTypeName); | ||
| if (codeFix !== undefined) { | ||
| codefixes.push(codeFix); | ||
| } | ||
| } | ||
| context.reportDiagnostic({ | ||
| target: op.operation, | ||
| codefixes, | ||
| }); | ||
| } | ||
| } else { | ||
| // Fallback for non-template operations: check the 200 response body | ||
| const body200 = getResponseBody(op.httpOperation.responses); | ||
| if (body200 !== undefined && !doesFinalResultMatch(finalResult, body200.type)) { | ||
| context.reportDiagnostic({ | ||
| target: op.operation, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| root: (program: Program) => { | ||
| const provider = resolveArmResources(program); | ||
|
|
||
| // Check resource-level actions | ||
| for (const resource of provider.resources ?? []) { | ||
| for (const op of resource.operations.actions) { | ||
| validateOperation(op); | ||
| } | ||
| } | ||
|
|
||
| // Check provider-level actions | ||
| for (const op of provider.providerOperations ?? []) { | ||
| validateOperation(op); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.