Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/rulesets/generated/spectral/az-arm.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ const mutabilityWithReadOnly = (prop, _opts, ctx) => {
if (prop === null || typeof prop !== "object") {
return [];
}
if (prop.readOnly === undefined ||
prop["x-ms-mutability"] === undefined ||
prop["x-ms-mutability"].length === 0) {
if (!Array.isArray(prop["x-ms-mutability"])) {
return [];
}
const path = ctx.path || [];
Expand Down Expand Up @@ -1067,7 +1065,7 @@ const ruleset$1 = {
severity: "error",
resolved: true,
formats: [oas2],
given: ["$[paths,'x-ms-paths']..?(@property === 'readOnly')^"],
given: ["$[paths,'x-ms-paths']..*[?(@ != null && @.readOnly !== undefined && @['x-ms-mutability'] !== undefined && @['x-ms-mutability'].length > 0)]"],
then: {
function: mutabilityWithReadOnly,
},
Expand Down
6 changes: 2 additions & 4 deletions packages/rulesets/generated/spectral/az-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ const mutabilityWithReadOnly = (prop, _opts, ctx) => {
if (prop === null || typeof prop !== "object") {
return [];
}
if (prop.readOnly === undefined ||
prop["x-ms-mutability"] === undefined ||
prop["x-ms-mutability"].length === 0) {
if (!Array.isArray(prop["x-ms-mutability"])) {
return [];
}
const path = ctx.path || [];
Expand Down Expand Up @@ -821,7 +819,7 @@ const ruleset = {
severity: "error",
resolved: true,
formats: [oas2],
given: ["$[paths,'x-ms-paths']..?(@property === 'readOnly')^"],
given: ["$[paths,'x-ms-paths']..*[?(@ != null && @.readOnly !== undefined && @['x-ms-mutability'] !== undefined && @['x-ms-mutability'].length > 0)]"],
then: {
function: mutabilityWithReadOnly,
},
Expand Down
6 changes: 2 additions & 4 deletions packages/rulesets/generated/spectral/az-dataplane.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ const mutabilityWithReadOnly = (prop, _opts, ctx) => {
if (prop === null || typeof prop !== "object") {
return [];
}
if (prop.readOnly === undefined ||
prop["x-ms-mutability"] === undefined ||
prop["x-ms-mutability"].length === 0) {
if (!Array.isArray(prop["x-ms-mutability"])) {
return [];
}
const path = ctx.path || [];
Expand Down Expand Up @@ -851,7 +849,7 @@ const ruleset$1 = {
severity: "error",
resolved: true,
formats: [oas2],
given: ["$[paths,'x-ms-paths']..?(@property === 'readOnly')^"],
given: ["$[paths,'x-ms-paths']..*[?(@ != null && @.readOnly !== undefined && @['x-ms-mutability'] !== undefined && @['x-ms-mutability'].length > 0)]"],
then: {
function: mutabilityWithReadOnly,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/rulesets/src/spectral/az-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ const ruleset: any = {
severity: "error",
resolved: true,
formats: [oas2],
given: ["$[paths,'x-ms-paths']..?(@property === 'readOnly')^"],
given: ["$[paths,'x-ms-paths']..*[?(@ != null && @.readOnly !== undefined && @['x-ms-mutability'] !== undefined && @['x-ms-mutability'].length > 0)]"],
Comment thread
mikeharder marked this conversation as resolved.
Outdated
then: {
function: mutabilityWithReadOnly,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ export const mutabilityWithReadOnly = (prop: any, _opts: any, ctx: any) => {
if (prop === null || typeof prop !== "object") {
return [];
}
if (
prop.readOnly === undefined ||
prop["x-ms-mutability"] === undefined ||
prop["x-ms-mutability"].length === 0
) {
// The given clause filters for:
// - readOnly !== undefined
// - x-ms-mutability !== undefined
// - x-ms-mutability.length > 0
// We still check Array.isArray as a defensive measure for type safety
if (!Array.isArray(prop["x-ms-mutability"])) {
Comment thread
mikeharder marked this conversation as resolved.
Outdated
return [];
}
const path = ctx.path || [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,77 @@ test("MutabilityWithReadOnly should find no errors", () => {
expect(results.length).toBe(0);
});
});

test("MutabilityWithReadOnly should ignore empty x-ms-mutability arrays", () => {
Comment thread
mikeharder marked this conversation as resolved.
Outdated
const myOpenApiDocument = {
swagger: "2.0",
paths: {
"/api/Paths": {
put: {
operationId: "Path_Create",
responses: {
200: {
description: "Success",
schema: {
$ref: "#/definitions/LroStatusCodeSchema",
},
},
},
},
},
},
definitions: {
LroStatusCodeSchema: {
type: "object",
properties: {
name: {
type: "string",
readOnly: true,
"x-ms-mutability": [],
},
},
},
},
};
return linter.run(myOpenApiDocument).then((results) => {
// Empty x-ms-mutability arrays should be ignored (no errors)
expect(results.length).toBe(0);
});
});

test("MutabilityWithReadOnly should ignore non-array x-ms-mutability values", () => {
Comment thread
mikeharder marked this conversation as resolved.
Outdated
const myOpenApiDocument = {
swagger: "2.0",
paths: {
"/api/Paths": {
put: {
operationId: "Path_Create",
responses: {
200: {
description: "Success",
schema: {
$ref: "#/definitions/LroStatusCodeSchema",
},
},
},
},
},
},
definitions: {
LroStatusCodeSchema: {
type: "object",
properties: {
name: {
type: "string",
readOnly: true,
"x-ms-mutability": "read",
},
},
},
},
};
return linter.run(myOpenApiDocument).then((results) => {
// Non-array x-ms-mutability values should be ignored (no errors)
expect(results.length).toBe(0);
});
});
Loading