Skip to content

Commit 32ee022

Browse files
committed
Add field ordering to output
1 parent 319d55f commit 32ee022

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/overlay.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ function applyOverlayToOpenAPI(spec, overlay) {
3434
return spec;
3535
}
3636

37+
function sortOpenAPIFields(field1, field2) {
38+
const orderedKeys = ["info", "servers", "paths", "components", "summary", "objectId", "description", "tags", "parameters", "responses"];
39+
40+
const index1 = orderedKeys.indexOf(field1);
41+
const index2 = orderedKeys.indexOf(field2);
42+
43+
if (index1 === -1 || index2 === -1) {
44+
return 0;
45+
} else if (index1 > index2) {
46+
return 1;
47+
} else {
48+
return -1;
49+
}
50+
}
51+
3752
export function overlayFiles(openapiFile, overlayFile) {
3853
// Parse the "input" OpenAPI document
3954
const specraw = fs.readFileSync(openapiFile, 'utf8');
@@ -46,7 +61,7 @@ export function overlayFiles(openapiFile, overlayFile) {
4661
spec = applyOverlayToOpenAPI(spec, overlay);
4762

4863
// Return the new spec
49-
return safeStringify(spec);
50-
64+
return safeStringify(spec, {"sortKeys": sortOpenAPIFields});
5165
}
5266

67+

test/expected/output1.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ paths:
1010
/pets:
1111
get:
1212
operationId: listPets
13+
info:
14+
x-overlay-applied: structured-overlay
15+
description: This is an added description
1316
tags:
1417
- pets
1518
- wildcard-done
@@ -41,9 +44,6 @@ paths:
4144
application/json:
4245
schema:
4346
$ref: '#/components/schemas/Error'
44-
info:
45-
x-overlay-applied: structured-overlay
46-
description: This is an added description
4747
post:
4848
foo:
4949
bar: hello

0 commit comments

Comments
 (0)