Skip to content

Commit 513b54a

Browse files
committed
CLI: Fix normalize YAML block scalar newlines
1 parent 799fe47 commit 513b54a

6 files changed

Lines changed: 43 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## unreleased
22

3+
- CLI: Fix normalize YAML block scalar newlines (#202)
4+
35
## [1.30.0] - 2026-03-16
46

57
- Overlay: add OpenAPI Overlay 1.1.0 support

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-inverse-flags-stripFlags'];
19+
: ['yaml-default-newline'];
2020

2121
describe('openapi-format tests', () => {
2222
let consoleLogSpy, consoleWarnSpy;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
openapi: 3.0.0
2+
info:
3+
description: |2
4+
Example
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
verbose: true
2+
output: output.yaml
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
openapi: 3.0.0
2+
info:
3+
description: |2
4+
Example

utils/file.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async function parseString(str, options = {}) {
2828
try {
2929
const result = yaml.parseWithPointers(encodedContent, {attachComments: options?.keepComments || false});
3030
options.yamlComments = result.comments;
31-
const obj = result.data;
31+
const obj = normalizeYamlBlockScalarNewlines(result.data);
3232
if (typeof obj === 'object') {
3333
return obj;
3434
} else {
@@ -338,6 +338,35 @@ function addQuotesToRefInString(yamlString) {
338338
return yamlString.replace(/(\$ref:\s*)([^"'\s>]+)/g, '$1"$2"');
339339
}
340340

341+
/**
342+
* Normalize a parser artifact where block scalars with indentation indicators
343+
* gain a spurious leading newline during YAML parsing.
344+
* This keeps genuine blank first lines intact by only stripping a single
345+
* leading newline when the string also has a trailing newline.
346+
* @param {*} value - Parsed YAML value.
347+
* @returns {*} Normalized value.
348+
*/
349+
function normalizeYamlBlockScalarNewlines(value) {
350+
if (typeof value === 'string') {
351+
if (value.startsWith('\n') && !value.startsWith('\n\n') && value.endsWith('\n')) {
352+
return value.slice(1);
353+
}
354+
return value;
355+
}
356+
357+
if (Array.isArray(value)) {
358+
return value.map(item => normalizeYamlBlockScalarNewlines(item));
359+
}
360+
361+
if (value && typeof value === 'object') {
362+
for (const key of Object.keys(value)) {
363+
value[key] = normalizeYamlBlockScalarNewlines(value[key]);
364+
}
365+
}
366+
367+
return value;
368+
}
369+
341370
/**
342371
* Analyze the OpenAPI document.
343372
* @param {object} oaObj - The OpenAPI document as a JSON object.

0 commit comments

Comments
 (0)