-
Notifications
You must be signed in to change notification settings - Fork 54
[XmsClientName] Optimize rule to prevent stack overflow on large specs #846
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
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ab0ae2f
[XmsClientName] Optimize rule to prevent excessive memory usage on la…
Copilot 29d51a4
[XmsClientName] Remove redundant null/object check from function (gua…
Copilot 847fb66
Fix formatting of xms-client-name.test.ts (add trailing commas per pr…
Copilot 04698a3
format
mikeharder 258677f
format
mikeharder 479173b
format
mikeharder 9d12449
Apply suggestion from @mikeharder
mikeharder 5efc835
format
mikeharder 086f0c7
Merge branch 'copilot/fix-xms-client-name-stack-overflow' of https://…
mikeharder eb90c01
Merge branch 'main' into copilot/fix-xms-client-name-stack-overflow
mikeharder 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
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
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
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
246 changes: 128 additions & 118 deletions
246
packages/rulesets/src/spectral/test/xms-client-name.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 |
|---|---|---|
| @@ -1,134 +1,144 @@ | ||
| import { Spectral } from "@stoplight/spectral-core"; | ||
| import linterForRule from "./utils"; | ||
| import { Spectral } from "@stoplight/spectral-core" | ||
| import linterForRule from "./utils" | ||
|
|
||
| let linter: Spectral; | ||
| let linter: Spectral | ||
|
|
||
| beforeAll(async () => { | ||
| linter = await linterForRule("XmsClientName"); | ||
| return linter; | ||
| }); | ||
| linter = await linterForRule("XmsClientName") | ||
| return linter | ||
| }) | ||
|
|
||
| test("XmsClientName should find errors", () => { | ||
| const myOpenApiDocument = { | ||
| swagger: "2.0", | ||
| paths: { | ||
| "/api/Paths": { | ||
| put: { | ||
| operationId: "Path_Create", | ||
| parameters: [ | ||
| { | ||
| name: "resourceGroupName", | ||
| in: "path", | ||
| required: true, | ||
| type: "string", | ||
| description: "The name of the resource group.", | ||
| "x-ms-client-name": "resource-group-name", | ||
| }, | ||
| { | ||
| name: "name", | ||
| in: "path", | ||
| required: true, | ||
| type: "string", | ||
| description: "The name of the Redis cache.", | ||
| "x-ms-client-name": "name", | ||
| }, | ||
| ], | ||
| responses: { | ||
| 200: { | ||
| description: "Success", | ||
| schema: { | ||
| $ref: "#/definitions/LroStatusCodeSchema", | ||
| }, | ||
| // Helper function to create OpenAPI document with parameters and properties | ||
| const createOpenApiDoc = (parameters: unknown[], properties: unknown) => ({ | ||
| swagger: "2.0", | ||
| paths: { | ||
| "/api/Paths": { | ||
| put: { | ||
| operationId: "Path_Create", | ||
| parameters, | ||
| responses: { | ||
| 200: { | ||
| description: "Success", | ||
| schema: { | ||
| $ref: "#/definitions/Schema", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| definitions: { | ||
| LroStatusCodeSchema: { | ||
| type: "object", | ||
| properties: { | ||
| name: { | ||
| type: "string", | ||
| readOnly: true, | ||
| "x-ms-mutability": ["read", "update"], | ||
| }, | ||
| length: { | ||
| type: "string", | ||
| readOnly: false, | ||
| "x-ms-mutability": ["read"], | ||
| "x-ms-client-name": "length", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| definitions: { | ||
| Schema: { | ||
| type: "object", | ||
| properties, | ||
| }, | ||
| }; | ||
| }, | ||
| }) | ||
|
|
||
| test("XmsClientName: invalid combinations (x-ms-client-name matches name)", () => { | ||
| const myOpenApiDocument = createOpenApiDoc( | ||
| [ | ||
| { | ||
| name: "resourceGroupName", | ||
| in: "path", | ||
| required: true, | ||
| type: "string", | ||
| description: "The name of the resource group.", | ||
| "x-ms-client-name": "resource-group-name", | ||
| }, | ||
| { | ||
| name: "name", | ||
| in: "path", | ||
| required: true, | ||
| type: "string", | ||
| description: "The name of the Redis cache.", | ||
| "x-ms-client-name": "name", | ||
| }, | ||
| ], | ||
| { | ||
| length: { | ||
| type: "string", | ||
| "x-ms-client-name": "length", | ||
| }, | ||
| } | ||
| ) | ||
| return linter.run(myOpenApiDocument).then((results) => { | ||
| expect(results.length).toBe(2); | ||
| results.sort((a, b) => a.path.join('.').localeCompare(b.path.join('.'))); | ||
| expect(results[0].message).toBe(`Value of 'x-ms-client-name' cannot be the same as 'name' Property/Model.`); | ||
| expect(results[0].path.join(".")).toBe("paths./api/Paths.put.parameters.1"); | ||
| expect(results[1].message).toBe(`Value of 'x-ms-client-name' cannot be the same as 'length' Property/Model.`); | ||
| expect(results[1].path.join(".")).toBe("paths./api/Paths.put.responses.200.schema.properties.length"); | ||
| }); | ||
| }); | ||
| // 1 invalid parameter + 1 invalid property = 2 total errors | ||
| expect(results.length).toBe(2) | ||
| results.sort((a, b) => a.path.join(".").localeCompare(b.path.join("."))) | ||
| expect(results[0].message).toBe(`Value of 'x-ms-client-name' cannot be the same as 'name' Property/Model.`) | ||
| expect(results[0].path.join(".")).toBe("paths./api/Paths.put.parameters.1") | ||
| expect(results[1].message).toBe(`Value of 'x-ms-client-name' cannot be the same as 'length' Property/Model.`) | ||
| expect(results[1].path.join(".")).toBe("paths./api/Paths.put.responses.200.schema.properties.length") | ||
| }) | ||
| }) | ||
|
|
||
| test("XmsClientName should find no errors", () => { | ||
| const myOpenApiDocument = { | ||
| swagger: "2.0", | ||
| paths: { | ||
| "/api/Paths": { | ||
| put: { | ||
| operationId: "Path_Create", | ||
| parameters: [ | ||
| { | ||
| name: "resourceGroupName", | ||
| in: "path", | ||
| required: true, | ||
| type: "string", | ||
| description: "The name of the resource group.", | ||
| "x-ms-client-name": "resource-group-name", | ||
| }, | ||
| { | ||
| name: "name", | ||
| in: "path", | ||
| required: true, | ||
| type: "string", | ||
| description: "The name of the Redis cache.", | ||
| "x-ms-client-name": "Name", | ||
| }, | ||
| ], | ||
| responses: { | ||
| 200: { | ||
| description: "Success", | ||
| schema: { | ||
| $ref: "#/definitions/LroStatusCodeSchema", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| test("XmsClientName: valid combinations (x-ms-client-name differs from name)", () => { | ||
| const myOpenApiDocument = createOpenApiDoc( | ||
| [ | ||
| { | ||
| name: "resourceGroupName", | ||
| in: "path", | ||
| required: true, | ||
| type: "string", | ||
| description: "The name of the resource group.", | ||
| "x-ms-client-name": "resource-group-name", | ||
| }, | ||
| }, | ||
| definitions: { | ||
| LroStatusCodeSchema: { | ||
| type: "object", | ||
| properties: { | ||
| name: { | ||
| type: "string", | ||
| readOnly: true, | ||
| "x-ms-mutability": ["read", "update"], | ||
| }, | ||
| length: { | ||
| type: "string", | ||
| readOnly: false, | ||
| "x-ms-mutability": ["read"], | ||
| "x-ms-client-name": "Length", | ||
| }, | ||
| }, | ||
| { | ||
| name: "name", | ||
| in: "path", | ||
| required: true, | ||
| type: "string", | ||
| description: "The name of the Redis cache.", | ||
| "x-ms-client-name": "Name", | ||
| }, | ||
| ], | ||
| { | ||
| length: { | ||
| type: "string", | ||
| "x-ms-client-name": "Length", | ||
| }, | ||
| } | ||
| ) | ||
| return linter.run(myOpenApiDocument).then((results) => { | ||
| expect(results.length).toBe(0) | ||
| }) | ||
| }) | ||
|
|
||
| test("XmsClientName: properties ignored by given clause", () => { | ||
| const myOpenApiDocument = createOpenApiDoc( | ||
| [ | ||
| { | ||
| name: "paramWithoutClientName", | ||
| in: "path", | ||
| required: true, | ||
| type: "string", | ||
| description: "Parameter without x-ms-client-name.", | ||
| }, | ||
| ], | ||
| { | ||
| noClientName: { | ||
| type: "string", | ||
| }, | ||
| } | ||
| ) | ||
| return linter.run(myOpenApiDocument).then((results) => { | ||
| // Properties/parameters without x-ms-client-name should be filtered out by the given clause | ||
| expect(results.length).toBe(0) | ||
| }) | ||
| }) | ||
|
|
||
| test("XmsClientName: null property values are filtered by given clause", () => { | ||
| const myOpenApiDocument = createOpenApiDoc([], { | ||
| nullProperty: null, | ||
| validProperty: { | ||
| type: "string", | ||
| "x-ms-client-name": "ValidName", | ||
| }, | ||
| }; | ||
| }) | ||
| return linter.run(myOpenApiDocument).then((results) => { | ||
| expect(results.length).toBe(0); | ||
| }); | ||
| }); | ||
| // Null property should be filtered out by the given clause (@ != null check) | ||
| // Only the valid property should pass through, and it's valid so 0 errors | ||
| expect(results.length).toBe(0) | ||
| }) | ||
| }) |
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.