-
Notifications
You must be signed in to change notification settings - Fork 90
[Swagger Linter Migration] UnSupportedPatchProperties #5384
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
Open
Yuchao Yan (msyyc)
wants to merge
3
commits into
main
Choose a base branch
from
promote-un-supported-patch-properties-to-arm
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.
Open
Changes from 1 commit
Commits
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
8 changes: 8 additions & 0 deletions
8
.chronus/changes/promote-un-supported-patch-properties-2026-09-03-13-20-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 the `no-unsupported-patch-properties` ARM linter rule, which reports writable resource identity, location, and provisioning state properties in PATCH request bodies. Register the rule as disabled by default in the ARM 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
46 changes: 46 additions & 0 deletions
46
...es/typespec-azure-resource-manager/src/rules/no-unsupported-patch-properties.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,46 @@ | ||
| ARM PATCH request bodies must not make resource identity, location, or provisioning state properties writable. A PATCH operation cannot change top-level `id`, `name`, `type`, or `location`, or `properties.provisioningState`. | ||
|
|
||
| Remove these properties from the PATCH body, mark identity and provisioning state properties with read-only visibility, and mark location as read-only and create-only. | ||
|
|
||
| ## Impact | ||
|
|
||
| - **Area:** API | ||
|
|
||
| Writable system-managed properties let generated SDKs offer updates that the service cannot safely apply and violate the ARM RPC contract. | ||
|
|
||
| ## ❌ Incorrect | ||
|
|
||
| ```tsp | ||
| model WidgetPatch { | ||
| id?: string; | ||
| location?: string; | ||
| properties?: WidgetPatchProperties; | ||
| } | ||
|
|
||
| model WidgetPatchProperties { | ||
| provisioningState?: string; | ||
| } | ||
| ``` | ||
|
|
||
| ## ✅ Correct | ||
|
|
||
| ```tsp | ||
| model WidgetPatch { | ||
| @visibility(Lifecycle.Read) | ||
| id?: string; | ||
|
|
||
| @visibility(Lifecycle.Read, Lifecycle.Create) | ||
| location?: string; | ||
|
|
||
| properties?: WidgetPatchProperties; | ||
| } | ||
|
|
||
| model WidgetPatchProperties { | ||
| @visibility(Lifecycle.Read) | ||
| provisioningState?: ResourceProvisioningState; | ||
| } | ||
| ``` | ||
|
|
||
| ## LintDiff Equivalent | ||
|
|
||
| This rule corresponds to the Swagger validator rule [UnSupportedPatchProperties](https://github.com/Azure/azure-openapi-validator/blob/main/docs/un-supported-patch-properties.md). |
227 changes: 227 additions & 0 deletions
227
packages/typespec-azure-resource-manager/src/rules/no-unsupported-patch-properties.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,227 @@ | ||
| import { | ||
| createRule, | ||
| fileRef, | ||
| getLifecycleVisibilityEnum, | ||
| getLocationContext, | ||
| getVisibilityForClass, | ||
| isNeverType, | ||
| isNullType, | ||
| paramMessage, | ||
| resolveEncodedName, | ||
| type DiagnosticTarget, | ||
| type Model, | ||
| type ModelProperty, | ||
| type Operation, | ||
| type Program, | ||
| type Type, | ||
| } from "@typespec/compiler"; | ||
| import { | ||
| createMetadataInfo, | ||
| getHttpOperation, | ||
| resolveRequestVisibility, | ||
| Visibility, | ||
| type MetadataInfo, | ||
| } from "@typespec/http"; | ||
|
|
||
| import { resolveProviderNamespace } from "../namespace.js"; | ||
|
|
||
| const unsupportedPatchProperties = new Set(["id", "name", "type", "location"]); | ||
|
|
||
| export const noUnsupportedPatchPropertiesRule = createRule({ | ||
| name: "no-unsupported-patch-properties", | ||
| docs: fileRef.fromPackageRoot("src/rules/no-unsupported-patch-properties.md"), | ||
| description: | ||
| "ARM PATCH request bodies must not contain writable resource identity, location, or provisioning state properties.", | ||
| severity: "warning", | ||
| url: "https://azure.github.io/typespec-azure/docs/libraries/azure-resource-manager/rules/no-unsupported-patch-properties", | ||
| messages: { | ||
| default: paramMessage`PATCH request body property '${"propertyName"}' is not patchable and should be removed or made read-only or immutable.`, | ||
| }, | ||
| create(context) { | ||
| return { | ||
| operation: (operation) => { | ||
| const namespace = operation.interface?.namespace ?? operation.namespace; | ||
| if (resolveProviderNamespace(context.program, namespace) === undefined) { | ||
| return; | ||
| } | ||
|
|
||
| const [httpOperation] = getHttpOperation(context.program, operation); | ||
| if (httpOperation.verb !== "patch" || httpOperation.parameters.body === undefined) { | ||
| return; | ||
| } | ||
|
|
||
| for (const violation of findViolations( | ||
| context.program, | ||
| httpOperation.parameters.body.type, | ||
| operation, | ||
| )) { | ||
| context.reportDiagnostic({ | ||
| target: violation.target, | ||
| format: { | ||
| propertyName: violation.propertyName, | ||
| }, | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| type Violation = { | ||
| target: DiagnosticTarget; | ||
| propertyName: string; | ||
| }; | ||
|
|
||
| function findViolations(program: Program, patchBody: Type, operation: Operation): Violation[] { | ||
| const patchModel = getModelType(patchBody); | ||
| if (patchModel === undefined) { | ||
| return []; | ||
| } | ||
|
|
||
| const metadataInfo = createMetadataInfo(program, { | ||
| canonicalVisibility: Visibility.Read, | ||
| canShareProperty: (property) => canSharePropertyUsingReadonlyOrXmsMutability(program, property), | ||
|
msyyc marked this conversation as resolved.
|
||
| }); | ||
| const visibility = resolveRequestVisibility(program, operation, "patch"); | ||
| const schemaVisibility = getSchemaVisibility(metadataInfo, patchModel, visibility); | ||
| const violations: Violation[] = []; | ||
|
|
||
| for (const { property, jsonName } of getModelProperties(program, patchModel)) { | ||
| if (!metadataInfo.isPayloadProperty(property, schemaVisibility) || isNeverType(property.type)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (unsupportedPatchProperties.has(jsonName) && isWritableProperty(program, property)) { | ||
| violations.push({ | ||
| target: getDiagnosticTarget(program, property, operation), | ||
| propertyName: jsonName, | ||
| }); | ||
| } | ||
|
|
||
| if (jsonName === "properties") { | ||
| collectProvisioningStateViolation( | ||
| program, | ||
| property.type, | ||
| operation, | ||
| metadataInfo, | ||
| schemaVisibility, | ||
| violations, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return violations; | ||
| } | ||
|
|
||
| function collectProvisioningStateViolation( | ||
| program: Program, | ||
| type: Type, | ||
| operation: Operation, | ||
| metadataInfo: MetadataInfo, | ||
| visibility: Visibility, | ||
| violations: Violation[], | ||
| ) { | ||
| const propertiesModel = getModelType(type); | ||
| if (propertiesModel === undefined) { | ||
| return; | ||
| } | ||
|
|
||
| const schemaVisibility = getSchemaVisibility(metadataInfo, propertiesModel, visibility); | ||
| for (const { property, jsonName } of getModelProperties(program, propertiesModel)) { | ||
| if ( | ||
| jsonName === "provisioningState" && | ||
| metadataInfo.isPayloadProperty(property, schemaVisibility) && | ||
| !isNeverType(property.type) && | ||
| isWritableProperty(program, property) | ||
| ) { | ||
| violations.push({ | ||
| target: getDiagnosticTarget(program, property, operation), | ||
| propertyName: `properties.${jsonName}`, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function getModelType(type: Type): Model | undefined { | ||
| if (type.kind === "Model") { | ||
| return type; | ||
| } | ||
| if (type.kind !== "Union") { | ||
| return undefined; | ||
| } | ||
|
|
||
| const nonNullVariants = [...type.variants.values()] | ||
| .map((variant) => variant.type) | ||
| .filter((variant) => !isNullType(variant)); | ||
| return nonNullVariants.length === 1 && nonNullVariants[0].kind === "Model" | ||
| ? nonNullVariants[0] | ||
| : undefined; | ||
| } | ||
|
|
||
| function getSchemaVisibility( | ||
| metadataInfo: MetadataInfo, | ||
| model: Model, | ||
| visibility: Visibility, | ||
| ): Visibility { | ||
| return metadataInfo.isTransformed(model, visibility) ? visibility : Visibility.Read; | ||
| } | ||
|
|
||
| function isWritableProperty(program: Program, property: ModelProperty): boolean { | ||
| const lifecycle = getLifecycleVisibilityEnum(program); | ||
| const visibility = getVisibilityForClass(program, property, lifecycle); | ||
| const read = lifecycle.members.get("Read"); | ||
| const update = lifecycle.members.get("Update"); | ||
| if (read !== undefined && visibility.size === 1 && visibility.has(read)) { | ||
| return false; | ||
| } | ||
|
|
||
| const emittedMutability = [...visibility].filter((member) => | ||
| ["Read", "Create", "Update"].includes(member.name), | ||
| ); | ||
| return ( | ||
| visibility.size === lifecycle.members.size || | ||
| emittedMutability.length === 0 || | ||
| (update !== undefined && visibility.has(update)) | ||
| ); | ||
| } | ||
|
|
||
| function canSharePropertyUsingReadonlyOrXmsMutability( | ||
| program: Program, | ||
| property: ModelProperty, | ||
| ): boolean { | ||
| const lifecycle = getLifecycleVisibilityEnum(program); | ||
| const visibility = getVisibilityForClass(program, property, lifecycle); | ||
| if (visibility.size === lifecycle.members.size) { | ||
| return true; | ||
| } | ||
| return ( | ||
| visibility.size > 0 && | ||
| [...visibility].every((member) => ["Read", "Create", "Update"].includes(member.name)) | ||
| ); | ||
| } | ||
|
|
||
| function getDiagnosticTarget( | ||
| program: Program, | ||
| property: ModelProperty, | ||
| operation: Operation, | ||
| ): DiagnosticTarget { | ||
| return getLocationContext(program, property).type === "project" ? property : operation; | ||
| } | ||
|
|
||
| function getModelProperties( | ||
| program: Program, | ||
| model: Model, | ||
| ): { property: ModelProperty; jsonName: string }[] { | ||
| const properties = new Map<string, ModelProperty>(); | ||
|
|
||
| for (let current: Model | undefined = model; current !== undefined; current = current.baseModel) { | ||
| for (const property of current.properties.values()) { | ||
| const jsonName = resolveEncodedName(program, property, "application/json"); | ||
| if (!properties.has(jsonName)) { | ||
| properties.set(jsonName, property); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return [...properties].map(([jsonName, property]) => ({ property, jsonName })); | ||
| } | ||
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.