Repository navigation
[Swagger Linter Migration] EvenSegmentedPathForPutOperation -> use-resource-path-for-put #5550
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
Merged
Yuchao Yan (msyyc)
merged 14 commits into
main
from
promote-lintdiff-even-segmented-path-for-put-operation
Oct 20, 2026
+348
−0
Merged
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
27fd132
Add use-even-segmented-put-path ARM lint rule
msyyc c5eac47
Address PUT path promotion review feedback
msyyc 2be0008
Merge branch 'main' into promote-lintdiff-even-segmented-path-for-put…
msyyc 78b60f1
test(arm): focus lint coverage on rule-owned behavior
msyyc a379efe
docs(arm): use templates in even-segmented PUT path examples
msyyc e3ac502
Merge branch 'main' into promote-lintdiff-even-segmented-path-for-put…
msyyc 268b53f
refactor(arm): rename PUT resource-path rule and simplify fixtures
msyyc 01bce11
test(arm): remove redundant PUT template filtering case
msyyc 89389d6
Cover extension PUT paths and registered suppression guidance
msyyc f2a67a7
Merge branch 'main' into promote-lintdiff-even-segmented-path-for-put…
msyyc cf36469
test(arm): remove redundant PUT suppression coverage
msyyc 2166f6e
Merge branch 'main' into promote-lintdiff-even-segmented-path-for-put…
msyyc 329cf5b
chore(arm): remove redundant PUT path test change note
msyyc 1c92c72
Merge branch 'main' into promote-lintdiff-even-segmented-path-for-put…
msyyc 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
There are no files selected for viewing
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,7 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@azure-tools/typespec-azure-resource-manager" | ||
| --- | ||
|
|
||
| Add the `use-even-segmented-put-path` lint rule to check that ARM PUT paths end in resource type/name pairs. | ||
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,7 @@ | ||
| --- | ||
| changeKind: internal | ||
| packages: | ||
| - "@azure-tools/typespec-azure-rulesets" | ||
| --- | ||
|
|
||
| Register the ARM `use-even-segmented-put-path` lint rule as disabled in the resource manager ruleset. |
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
77 changes: 77 additions & 0 deletions
77
packages/typespec-azure-resource-manager/src/rules/use-even-segmented-put-path.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,77 @@ | ||
| ARM PUT operations create or replace a resource instance. Their HTTP paths must | ||
| end in resource type/name pairs after the provider namespace, such as | ||
| `/providers/Microsoft.Contoso/widgets/{widgetName}`. A singleton may use the | ||
| literal name `default`, and nested resources add further type/name pairs. | ||
|
|
||
| A provider root, collection path, or unmatched suffix such as | ||
| `/widgets/{widgetName}/{configurationName}` is not a resource instance path for PUT. | ||
| Prefer standard ARM resource operation templates, which construct resource paths | ||
| for you. This rule also checks custom PUT operations when explicitly enabled. | ||
|
|
||
| The rule checks resolved HTTP paths on project operations, including ordinary | ||
| and nested namespaces without a provider decorator. It excludes imported library | ||
| declarations, template instances, and operations in uninstantiated template | ||
| interfaces. It does not filter operations by SDK language scope. | ||
|
|
||
| ## Impact | ||
|
|
||
| - **Area:** API, SDK | ||
|
|
||
| Using PUT for a collection or action endpoint obscures the identity of the | ||
| resource being created or replaced and gives generated SDKs an inconsistent | ||
| resource-management contract. | ||
|
|
||
| ## ❌ Incorrect | ||
|
|
||
| ```tsp | ||
| @armProviderNamespace | ||
| namespace Microsoft.Contoso; | ||
|
|
||
| model Widget is TrackedResource<{}> { | ||
| ...ResourceNameParameter<Widget>; | ||
| } | ||
|
|
||
| @armResourceOperations | ||
| interface Widgets { | ||
| createOrUpdate is ArmResourceCreateOrReplaceSync< | ||
| Widget, | ||
| Parameters = { | ||
| @path configurationName: string; | ||
| } | ||
| >; | ||
| } | ||
| ``` | ||
|
|
||
| ## ✅ Correct | ||
|
|
||
| Use the standard ARM create-or-replace template without the additional path | ||
| parameter, so the PUT targets the resource instance: | ||
|
|
||
| ```tsp | ||
| @armProviderNamespace | ||
| namespace Microsoft.Contoso; | ||
|
|
||
| model Widget is TrackedResource<{}> { | ||
| ...ResourceNameParameter<Widget>; | ||
| } | ||
|
|
||
| @armResourceOperations | ||
| interface Widgets { | ||
| createOrUpdate is ArmResourceCreateOrReplaceSync<Widget>; | ||
| } | ||
| ``` | ||
|
|
||
| ## Suppression | ||
|
|
||
| Suppress this rule only for a reviewed legacy ARM contract whose PUT path cannot | ||
| be changed, or a client-only operation that is not part of the service's ARM | ||
| surface. Document the reason on the operation. For new APIs, use a resource | ||
| instance path rather than suppressing a collection or action-like PUT. | ||
|
|
||
| ## LintDiff Equivalent | ||
|
|
||
| This rule corresponds to | ||
| [EvenSegmentedPathForPutOperation](https://github.com/Azure/azure-openapi-validator/blob/main/docs/even-segmented-path-for-put-operation.md). | ||
| It preserves the validator's provider/resource suffix pattern rather than | ||
| counting every segment in the entire path. SDK-scoped TypeSpec operations that | ||
| are absent from retained Swagger can still receive a diagnostic. |
48 changes: 48 additions & 0 deletions
48
packages/typespec-azure-resource-manager/src/rules/use-even-segmented-put-path.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,48 @@ | ||
| import { | ||
| createRule, | ||
| fileRef, | ||
| getLocationContext, | ||
| isTemplateInstance, | ||
| paramMessage, | ||
| } from "@typespec/compiler"; | ||
| import { getHttpOperation } from "@typespec/http"; | ||
| import { isTemplatedInterfaceOperation } from "./utils.js"; | ||
|
|
||
| const evenSegmentedArmPutPathPattern = /.*\/providers\/\w+\.\w+(\/\w+\/(default|\{\w+\}))+$/; | ||
|
|
||
| export const useEvenSegmentedPutPathRule = createRule({ | ||
| name: "use-even-segmented-put-path", | ||
| description: "ARM PUT paths must end in resource type and resource name pairs.", | ||
| severity: "warning", | ||
| url: "https://azure.github.io/typespec-azure/docs/libraries/azure-resource-manager/rules/use-even-segmented-put-path", | ||
| docs: fileRef.fromPackageRoot("src/rules/use-even-segmented-put-path.md"), | ||
| messages: { | ||
| default: paramMessage`ARM PUT path '${"path"}' must end in repeated /{resourceType}/{resourceName} or /{resourceType}/default pairs after the provider namespace.`, | ||
| }, | ||
| create(context) { | ||
| return { | ||
| operation: (operation) => { | ||
| if ( | ||
| isTemplateInstance(operation) || | ||
| isTemplatedInterfaceOperation(operation) || | ||
| getLocationContext(context.program, operation).type !== "project" | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const [httpOperation] = getHttpOperation(context.program, operation); | ||
| if ( | ||
| httpOperation.verb !== "put" || | ||
| evenSegmentedArmPutPathPattern.test(httpOperation.path) | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| context.reportDiagnostic({ | ||
| target: operation, | ||
| format: { path: httpOperation.path }, | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
| }); |
167 changes: 167 additions & 0 deletions
167
packages/typespec-azure-resource-manager/test/rules/use-even-segmented-put-path.test.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,167 @@ | ||
| import { Tester } from "#test/tester.js"; | ||
| import { type LinterRuleTester, createLinterRuleTester } from "@typespec/compiler/testing"; | ||
| import { readFileSync } from "node:fs"; | ||
| import { beforeEach, it } from "vitest"; | ||
| import { useEvenSegmentedPutPathRule } from "../../src/rules/use-even-segmented-put-path.js"; | ||
|
|
||
| let tester: LinterRuleTester; | ||
|
|
||
| beforeEach(async () => { | ||
| const runner = await Tester.createInstance(); | ||
| tester = createLinterRuleTester( | ||
| runner, | ||
| useEvenSegmentedPutPathRule, | ||
| "@azure-tools/typespec-azure-resource-manager", | ||
| ); | ||
| }); | ||
|
|
||
| function diagnostic(path: string) { | ||
| return { | ||
| code: "@azure-tools/typespec-azure-resource-manager/use-even-segmented-put-path", | ||
| severity: "warning" as const, | ||
| message: `ARM PUT path '${path}' must end in repeated /{resourceType}/{resourceName} or /{resourceType}/default pairs after the provider namespace.`, | ||
| }; | ||
| } | ||
|
|
||
| it("rejects a provider-root PUT and targets the operation", async () => { | ||
| await tester | ||
| .expect( | ||
| ` | ||
| /*target*/@route("/providers/Microsoft.Contoso") | ||
| @put op create(): void; | ||
| `, | ||
| ) | ||
| .toEmitDiagnostics((x) => ({ | ||
| ...diagnostic("/providers/Microsoft.Contoso"), | ||
| pos: x.pos.target.pos, | ||
| })); | ||
| }); | ||
|
|
||
| it("rejects a collection-level PUT", async () => { | ||
| await tester | ||
| .expect( | ||
| ` | ||
| @route("/providers/Microsoft.Contoso/widgets") | ||
| @put op create(): void; | ||
| `, | ||
| ) | ||
| .toEmitDiagnostics(diagnostic("/providers/Microsoft.Contoso/widgets")); | ||
| }); | ||
|
|
||
| it("rejects an odd suffix after a resource instance", async () => { | ||
| await tester | ||
| .expect( | ||
| ` | ||
| @route("/providers/Microsoft.Contoso/widgets/{widgetName}/config") | ||
| @put op create(@path widgetName: string): void; | ||
| `, | ||
| ) | ||
| .toEmitDiagnostics(diagnostic("/providers/Microsoft.Contoso/widgets/{widgetName}/config")); | ||
| }); | ||
|
|
||
| it("accepts standard ARM create-or-update templates", async () => { | ||
| await tester | ||
| .expect( | ||
| ` | ||
| @armProviderNamespace | ||
| namespace Microsoft.Contoso; | ||
| model Widget is TrackedResource<{}> { | ||
| ...ResourceNameParameter<Widget>; | ||
| } | ||
| @armResourceOperations | ||
| interface Widgets { | ||
| createOrUpdate is ArmResourceCreateOrReplaceSync<Widget>; | ||
| } | ||
| `, | ||
| ) | ||
| .toBeValid(); | ||
| }); | ||
|
|
||
| it("validates the published ARM template examples", async () => { | ||
|
msyyc marked this conversation as resolved.
Outdated
|
||
| const doc = readFileSync( | ||
| new URL("../../src/rules/use-even-segmented-put-path.md", import.meta.url), | ||
| "utf8", | ||
| ); | ||
| const examples = [...doc.matchAll(/```tsp\r?\n([\s\S]*?)\r?\n```/g)].map((match) => match[1]); | ||
| if (examples.length !== 2) { | ||
| throw new Error(`Expected exactly two published TypeSpec examples, got ${examples.length}`); | ||
| } | ||
|
|
||
| await tester.expect(examples[0]).toEmitDiagnostics((result) => { | ||
| const main = [...result.program.sourceFiles.values()].find((file) => | ||
| file.file.text.includes("createOrUpdate is ArmResourceCreateOrReplaceSync"), | ||
| ); | ||
| const pos = main?.file.text.indexOf("createOrUpdate is ArmResourceCreateOrReplaceSync"); | ||
| if (!main || pos === undefined || pos < 0) { | ||
| throw new Error("The published createOrUpdate operation was not compiled"); | ||
| } | ||
| return { | ||
| ...diagnostic( | ||
| "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Contoso/widgets/{widgetName}/{configurationName}", | ||
| ), | ||
| file: main.file.path, | ||
| pos, | ||
| }; | ||
| }); | ||
| await tester.expect(examples[1]).toBeValid(); | ||
| }); | ||
|
|
||
| it("accepts a singleton default resource", async () => { | ||
| await tester | ||
| .expect( | ||
| ` | ||
| @route("/providers/Microsoft.Contoso/widgets/default") | ||
| @put op create(): void; | ||
| `, | ||
| ) | ||
| .toBeValid(); | ||
| }); | ||
|
|
||
| it("accepts nested resource type and name pairs", async () => { | ||
| await tester | ||
| .expect( | ||
| ` | ||
| @route("/providers/Microsoft.Contoso/widgets/{widgetName}/configs/{configName}") | ||
| @put op create(@path widgetName: string, @path configName: string): void; | ||
| `, | ||
| ) | ||
| .toBeValid(); | ||
| }); | ||
|
|
||
| it("accepts a scope-prefixed resource path", async () => { | ||
| await tester | ||
| .expect( | ||
| ` | ||
| @route("/{scope}/providers/Microsoft.Contoso/widgets/{widgetName}") | ||
| @put op create(@path scope: string, @path widgetName: string): void; | ||
| `, | ||
| ) | ||
| .toBeValid(); | ||
| }); | ||
|
|
||
| it("ignores non-PUT operations", async () => { | ||
| await tester | ||
| .expect( | ||
| ` | ||
| @route("/providers/Microsoft.Contoso/widgets") | ||
| @post op create(): void; | ||
| @route("/providers/Microsoft.Contoso") | ||
| @get op read(): void; | ||
| `, | ||
| ) | ||
| .toBeValid(); | ||
| }); | ||
|
|
||
| it("ignores operation template instances while checking project declarations", async () => { | ||
| await tester | ||
| .expect( | ||
| ` | ||
| @route("/providers/Microsoft.Template") | ||
| @put op Template<T>(@body body: T): void; | ||
| alias Instance = Template<string>; | ||
| @route("/providers/Microsoft.Contoso") | ||
| @put op create(): void; | ||
| `, | ||
| ) | ||
| .toEmitDiagnostics(diagnostic("/providers/Microsoft.Contoso")); | ||
| }); | ||
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
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.