Skip to content

Commit c7b1109

Browse files
authored
fix: Prevent sorting of required array properties (#167)
1 parent fa25c63 commit c7b1109

File tree

6 files changed

+37
-8
lines changed

6 files changed

+37
-8
lines changed

openapi-format.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,13 @@ async function openapiSort(oaObj, options) {
150150
// Deep sort list of properties
151151
let sortedObj = JSON.parse(JSON.stringify(node)); // Deep copy of the object
152152
for (let keyRes in sortedObj) {
153-
sortedObj[keyRes] = prioritySort(sortedObj[keyRes], sortSet[this.key]);
153+
if (
154+
typeof sortedObj[keyRes] === 'object' &&
155+
!Array.isArray(sortedObj[keyRes]) &&
156+
sortedObj[keyRes] !== null
157+
) {
158+
sortedObj[keyRes] = prioritySort(sortedObj[keyRes], sortSet[this.key]);
159+
}
154160
}
155161
this.update(sortedObj);
156162
} else {

package-lock.json

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const tests = !localTesting
1616
? fs.readdirSync(__dirname).filter(file => {
1717
return fs.statSync(path.join(__dirname, file)).isDirectory() && !file.startsWith('_');
1818
})
19-
: ['yaml-filter-unused-components-path'];
19+
: ['yaml-sort-required'];
2020

2121
describe('openapi-format tests', () => {
2222
let consoleLogSpy, consoleWarnSpy;

test/yaml-sort-required/input.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
openapi: 3.0.3
2+
components:
3+
properties:
4+
type: object
5+
required:
6+
- datetime
7+
properties:
8+
datetime:
9+
type: string
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
verbose: true
2+
sort: true
3+
output: output.yaml
4+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
openapi: 3.0.3
2+
components:
3+
properties:
4+
type: object
5+
required:
6+
- datetime
7+
properties:
8+
datetime:
9+
type: string

0 commit comments

Comments
 (0)