Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/npm-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Node Tests CI
on:
push:
branches:
- '*' # Trigger on all branches
- main
pull_request:
branches:
- '*' # Trigger on all branches
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## unreleased

## [1.30.1] - 2026-03-26

- CLI: Fix normalize YAML block scalar newlines (#202)

## [1.30.0] - 2026-03-16

- Overlay: add OpenAPI Overlay 1.1.0 support
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const tests = !localTesting
? fs.readdirSync(__dirname).filter(file => {
return fs.statSync(path.join(__dirname, file)).isDirectory() && !file.startsWith('_');
})
: ['yaml-filter-inverse-flags-stripFlags'];
: ['yaml-default-newline'];

describe('openapi-format tests', () => {
let consoleLogSpy, consoleWarnSpy;
Expand Down
4 changes: 4 additions & 0 deletions test/yaml-default-newline/input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
openapi: 3.0.0
info:
description: |2
Example
2 changes: 2 additions & 0 deletions test/yaml-default-newline/options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
verbose: true
output: output.yaml
4 changes: 4 additions & 0 deletions test/yaml-default-newline/output.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
openapi: 3.0.0
info:
description: |2
Example
31 changes: 30 additions & 1 deletion utils/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async function parseString(str, options = {}) {
try {
const result = yaml.parseWithPointers(encodedContent, {attachComments: options?.keepComments || false});
options.yamlComments = result.comments;
const obj = result.data;
const obj = normalizeYamlBlockScalarNewlines(result.data);
if (typeof obj === 'object') {
return obj;
} else {
Expand Down Expand Up @@ -338,6 +338,35 @@ function addQuotesToRefInString(yamlString) {
return yamlString.replace(/(\$ref:\s*)([^"'\s>]+)/g, '$1"$2"');
}

/**
* Normalize a parser artifact where block scalars with indentation indicators
* gain a spurious leading newline during YAML parsing.
* This keeps genuine blank first lines intact by only stripping a single
* leading newline when the string also has a trailing newline.
* @param {*} value - Parsed YAML value.
* @returns {*} Normalized value.
*/
function normalizeYamlBlockScalarNewlines(value) {
if (typeof value === 'string') {
if (value.startsWith('\n') && !value.startsWith('\n\n') && value.endsWith('\n')) {
return value.slice(1);
}
return value;
}

if (Array.isArray(value)) {
return value.map(item => normalizeYamlBlockScalarNewlines(item));
}

if (value && typeof value === 'object') {
for (const key of Object.keys(value)) {
value[key] = normalizeYamlBlockScalarNewlines(value[key]);
}
}

return value;
}

/**
* Analyze the OpenAPI document.
* @param {object} oaObj - The OpenAPI document as a JSON object.
Expand Down
Loading