-
Notifications
You must be signed in to change notification settings - Fork 90
[Swagger Linter Migration] PatchBodyParametersSchema #5294
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
9
commits into
Azure:main
Choose a base branch
from
msyyc:promote-lintdiff-patch-body-parameters-schema-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 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2d22520
Promote PatchBodyParametersSchema rule
msyyc 07a3c9f
Update resource-manager.ts
msyyc 2db9f39
docs(arm): remove generated rule metadata
msyyc 7dece2e
Merge branch 'main' into promote-lintdiff-patch-body-parameters-schem…
msyyc 20ca081
test(arm): cover library PATCH properties
msyyc 526713f
docs(arm): catalog PATCH body rule
msyyc 64a91f6
refactor(arm): rename unsafe PATCH rule
msyyc 7f6971c
test(arm): use recognized PATCH test wording
msyyc 70527cb
Fix promotion changelog classification
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
8 changes: 8 additions & 0 deletions
8
.chronus/changes/promote-patch-body-parameters-schema-2026-08-25-05-25-55.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 an ARM linter rule that reports required, default-valued, and create-only properties emitted in PATCH request bodies. |
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
52 changes: 52 additions & 0 deletions
52
packages/typespec-azure-resource-manager/src/rules/patch-body-invalid-property.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,52 @@ | ||
| ARM PATCH request body properties must be update-safe. A property emitted in an ARM PATCH body must not be required, must not define a default value, and must not be create-only. | ||
|
|
||
| ## Impact | ||
|
|
||
| - **Area:** API | ||
|
|
||
| PATCH describes partial updates. Required PATCH body properties, default-valued properties, and create-only properties can make partial updates ambiguous for service authors and SDKs, and can produce ARM OpenAPI that violates PATCH request-body guidance. | ||
|
|
||
| The rule checks the effective emitted PATCH payload. Properties omitted from the PATCH payload, such as `never` properties or create-only properties removed by the PATCH visibility transform, are not reported. A top-level emitted property named `identity` is skipped to match ARM PATCH identity envelope behavior. | ||
|
|
||
| ## ❌ Incorrect | ||
|
|
||
| ```tsp | ||
| @armProviderNamespace | ||
| namespace Microsoft.Contoso; | ||
|
|
||
| model WidgetPatchBody { | ||
| displayName: string; | ||
| enabled?: boolean = false; | ||
|
|
||
| @visibility(Lifecycle.Create) | ||
| createdBy?: string; | ||
| } | ||
|
|
||
| @route("/widgets/{name}") | ||
| @patch | ||
| op update(@path name: string, @body body: WidgetPatchBody): void; | ||
| ``` | ||
|
|
||
| ## ✅ Correct | ||
|
|
||
| ```tsp | ||
| @armProviderNamespace | ||
| namespace Microsoft.Contoso; | ||
|
|
||
| model WidgetPatchBody { | ||
| displayName?: string; | ||
| enabled?: boolean; | ||
| } | ||
|
|
||
| @route("/widgets/{name}") | ||
| @patch | ||
| op update(@path name: string, @body body: WidgetPatchBody): void; | ||
| ``` | ||
|
|
||
| ## LintDiff Equivalent | ||
|
|
||
| This rule corresponds to the LintDiff rule [PatchBodyParametersSchema](https://github.com/Azure/azure-openapi-validator/blob/main/docs/patch-body-parameters-schema.md). | ||
|
|
||
| ## Suppression | ||
|
|
||
| Do not suppress this rule for ordinary ARM resource PATCH operations. Fix the PATCH model so updateable properties are optional, do not carry defaults, and exclude create-only properties from the emitted PATCH payload. |
296 changes: 296 additions & 0 deletions
296
packages/typespec-azure-resource-manager/src/rules/patch-body-invalid-property.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,296 @@ | ||
| import { | ||
| createRule, | ||
| fileRef, | ||
| getDiscriminator, | ||
| 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"; | ||
|
|
||
| export const patchBodyInvalidPropertyRule = createRule({ | ||
| name: "patch-body-invalid-property", | ||
| docs: fileRef.fromPackageRoot("src/rules/patch-body-invalid-property.md"), | ||
| severity: "warning", | ||
| description: "ARM PATCH body properties must not be required, have defaults, or be create-only.", | ||
| url: "https://azure.github.io/typespec-azure/docs/libraries/azure-resource-manager/rules/patch-body-invalid-property", | ||
| messages: { | ||
| required: paramMessage`Properties of a PATCH request body must not be required, property:${"propertyName"}.`, | ||
| default: paramMessage`Properties of a PATCH request body must not have default value, property:${"propertyName"}.`, | ||
| createOnly: paramMessage`Properties of a PATCH request body must not be x-ms-mutability: ["create"], property:${"propertyName"}.`, | ||
| }, | ||
| 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") { | ||
| return; | ||
| } | ||
|
|
||
| const patchBody = httpOperation.parameters.body?.type; | ||
| if (patchBody === undefined) { | ||
| return; | ||
| } | ||
|
|
||
| for (const violation of findViolations(context.program, patchBody, operation)) { | ||
| context.reportDiagnostic({ | ||
| target: violation.target, | ||
| messageId: violation.messageId, | ||
| format: { | ||
| propertyName: violation.propertyName, | ||
| }, | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| type Violation = { | ||
| target: DiagnosticTarget; | ||
| propertyName: string; | ||
| messageId: "required" | "default" | "createOnly"; | ||
| }; | ||
|
|
||
| function findViolations(program: Program, patchBody: Type, operation: Operation): Violation[] { | ||
| const violations: Violation[] = []; | ||
| const metadataInfo = createMetadataInfo(program, { | ||
| canonicalVisibility: Visibility.Read, | ||
| canShareProperty: (property) => canSharePropertyUsingReadonlyOrXmsMutability(program, property), | ||
| }); | ||
| const visibility = resolveRequestVisibility(program, operation, "patch"); | ||
| collectNestedViolations( | ||
| program, | ||
| patchBody, | ||
| violations, | ||
| [], | ||
| new Map(), | ||
| operation, | ||
| metadataInfo, | ||
| visibility, | ||
| ); | ||
| return violations; | ||
| } | ||
|
|
||
| function collectViolations( | ||
| program: Program, | ||
| model: Model, | ||
| violations: Violation[], | ||
| path: string[] = [], | ||
| visited: Map<Model, Set<Visibility>> = new Map(), | ||
| diagnosticTarget: DiagnosticTarget, | ||
| metadataInfo: MetadataInfo, | ||
| visibility: Visibility, | ||
| ) { | ||
| const schemaVisibility = metadataInfo.isTransformed(model, visibility) | ||
| ? visibility | ||
| : Visibility.Read; | ||
| const visitedVisibilities = visited.get(model); | ||
| if (visitedVisibilities?.has(schemaVisibility)) { | ||
| return; | ||
| } | ||
| if (visitedVisibilities === undefined) { | ||
| visited.set(model, new Set([schemaVisibility])); | ||
| } else { | ||
| visitedVisibilities.add(schemaVisibility); | ||
| } | ||
|
|
||
| const discriminator = getInheritedDiscriminator(program, model); | ||
| if ( | ||
| discriminator !== undefined && | ||
| getModelProperty(model, discriminator.propertyName) === undefined && | ||
| !isTopLevelIdentityProperty([...path, discriminator.propertyName], discriminator.propertyName) | ||
| ) { | ||
| violations.push({ | ||
| target: getLocationContext(program, model).type === "project" ? model : diagnosticTarget, | ||
| propertyName: [...path, discriminator.propertyName].join("."), | ||
| messageId: "required", | ||
| }); | ||
| } | ||
|
|
||
| for (const property of getModelProperties(model)) { | ||
| const jsonName = resolveEncodedName(program, property, "application/json"); | ||
| const propertyPath = [...path, jsonName]; | ||
| if (isTopLevelIdentityProperty(propertyPath, jsonName)) { | ||
| continue; | ||
| } | ||
| if (!metadataInfo.isPayloadProperty(property, schemaVisibility)) { | ||
| continue; | ||
| } | ||
| if (isNeverType(property.type)) { | ||
| continue; | ||
| } | ||
| const propertyTarget = | ||
| getLocationContext(program, property).type === "project" ? property : diagnosticTarget; | ||
|
|
||
| if ( | ||
| !metadataInfo.isOptional(property, schemaVisibility) || | ||
| property.name === discriminator?.propertyName | ||
| ) { | ||
| violations.push({ | ||
| target: propertyTarget, | ||
| propertyName: propertyPath.join("."), | ||
| messageId: "required", | ||
| }); | ||
| } | ||
|
|
||
| if (property.defaultValue !== undefined) { | ||
| violations.push({ | ||
| target: propertyTarget, | ||
| propertyName: propertyPath.join("."), | ||
| messageId: "default", | ||
| }); | ||
| } | ||
|
|
||
| if (isCreateOnlyMutability(program, property)) { | ||
| violations.push({ | ||
| target: propertyTarget, | ||
| propertyName: propertyPath.join("."), | ||
| messageId: "createOnly", | ||
| }); | ||
| } | ||
|
|
||
| collectNestedViolations( | ||
| program, | ||
| property.type, | ||
| violations, | ||
| propertyPath, | ||
| visited, | ||
| propertyTarget, | ||
| metadataInfo, | ||
| schemaVisibility, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| function collectNestedViolations( | ||
| program: Program, | ||
| type: Type, | ||
| violations: Violation[], | ||
| path: string[], | ||
| visited: Map<Model, Set<Visibility>>, | ||
| diagnosticTarget: DiagnosticTarget, | ||
| metadataInfo: MetadataInfo, | ||
| visibility: Visibility, | ||
| ) { | ||
| if (type.kind === "Model") { | ||
| collectViolations( | ||
| program, | ||
| type, | ||
| violations, | ||
| path, | ||
| visited, | ||
| diagnosticTarget, | ||
| metadataInfo, | ||
| visibility, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| if (type.kind === "Union") { | ||
| const nonNullVariants = [...type.variants.values()] | ||
| .map((variant) => variant.type) | ||
| .filter((variant) => !isNullType(variant)); | ||
| if (nonNullVariants.length === 1) { | ||
| collectNestedViolations( | ||
| program, | ||
| nonNullVariants[0], | ||
| violations, | ||
| path, | ||
| visited, | ||
| diagnosticTarget, | ||
| metadataInfo, | ||
| visibility, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function isTopLevelIdentityProperty(propertyPath: string[], jsonName: string): boolean { | ||
| return propertyPath.length === 1 && jsonName.toLowerCase() === "identity"; | ||
| } | ||
|
|
||
| function isCreateOnlyMutability(program: Program, property: ModelProperty): boolean { | ||
| const lifecycle = getLifecycleVisibilityEnum(program); | ||
| const create = lifecycle.members.get("Create"); | ||
| if (create === undefined) { | ||
| return false; | ||
| } | ||
|
|
||
| const visibility = getVisibilityForClass(program, property, lifecycle); | ||
| return visibility.size === 1 && visibility.has(create); | ||
| } | ||
|
|
||
| function getInheritedDiscriminator(program: Program, model: Model) { | ||
| for (let current: Model | undefined = model; current !== undefined; current = current.baseModel) { | ||
| const discriminator = getDiscriminator(program, current); | ||
| if (discriminator !== undefined) { | ||
| return discriminator; | ||
| } | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| 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 getModelProperty(model: Model, name: string): ModelProperty | undefined { | ||
| for (let current: Model | undefined = model; current !== undefined; current = current.baseModel) { | ||
| const property = current.properties.get(name); | ||
| if (property !== undefined) { | ||
| return property; | ||
| } | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| function getModelProperties(model: Model): ModelProperty[] { | ||
| const properties = new Map<string, ModelProperty>(); | ||
|
|
||
| for (let current: Model | undefined = model; current !== undefined; current = current.baseModel) { | ||
| for (const property of current.properties.values()) { | ||
| if (!properties.has(property.name)) { | ||
| properties.set(property.name, property); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return [...properties.values()]; | ||
| } | ||
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.