Skip to content

Commit 210648f

Browse files
committed
Improvement: Added safe checks for invalid YAML
1 parent 38f2bdc commit 210648f

File tree

4 files changed

+78
-37
lines changed

4 files changed

+78
-37
lines changed

CHANGELOG.md

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

3+
- Improvement: Added safe checks for invalid YAML
4+
- CLI - Fix relative path generation in $ref when splitting document (#175)
5+
36
## [1.27.3] - 2025-07-30
47

58
- Convert: convert nullable for anyOf oneOf to 3.1 (#172)

test/util-file.test.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,6 @@ describe('openapi-format CLI file tests', () => {
448448
});
449449

450450
test('should handle null when extracting OpenAPI info', () => {
451-
const mockOpenApi = require('./__utils__/mockOpenApi.json');
452451
const result = analyzeOpenApi(null);
453452

454453
expect(result).toEqual({
@@ -463,5 +462,36 @@ describe('openapi-format CLI file tests', () => {
463462
tags: []
464463
});
465464
});
465+
466+
test('should ignore null path items when extracting OpenAPI info', () => {
467+
const mockOpenApi = {
468+
openapi: '3.0.0',
469+
info: {title: 'x', version: '1'},
470+
paths: {
471+
'/pets': {
472+
get: {
473+
responses: {
474+
200: {description: 'ok'}
475+
}
476+
}
477+
},
478+
'/empty': null
479+
}
480+
};
481+
482+
const result = analyzeOpenApi(mockOpenApi);
483+
484+
expect(result).toEqual({
485+
flags: [],
486+
tags: [],
487+
operationIds: [],
488+
paths: ['/pets', '/empty'],
489+
methods: ['GET'],
490+
operations: ['GET::/pets'],
491+
responseContent: [],
492+
requestContent: [],
493+
flagValues: []
494+
});
495+
});
466496
});
467497
});

utils/file.js

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -359,47 +359,51 @@ function analyzeOpenApi(oaObj) {
359359
paths.push(path);
360360
const pathItem = oaObj.paths[path];
361361

362-
Object.keys(pathItem).forEach(method => {
363-
methods.add(method.toUpperCase());
364-
const operation = pathItem[method];
365-
operations.push(`${method.toUpperCase()}::${path}`);
366-
367-
if (operation?.tags && Array.isArray(operation.tags)) {
368-
operation.tags.forEach(tag => {
369-
if (tag.startsWith('x-')) {
370-
flags.add(tag);
371-
} else {
372-
tags.add(tag);
362+
if (pathItem && typeof pathItem === 'object') {
363+
Object.keys(pathItem).forEach(method => {
364+
methods.add(method.toUpperCase());
365+
const operation = pathItem[method];
366+
operations.push(`${method.toUpperCase()}::${path}`);
367+
368+
if (operation && typeof operation === 'object') {
369+
if (operation?.tags && Array.isArray(operation.tags)) {
370+
operation.tags.forEach(tag => {
371+
if (tag.startsWith('x-')) {
372+
flags.add(tag);
373+
} else {
374+
tags.add(tag);
375+
}
376+
});
377+
}
378+
379+
if (operation?.operationId) {
380+
operationIds.push(operation.operationId);
373381
}
374-
});
375-
}
376-
377-
if (operation?.operationId) {
378-
operationIds.push(operation.operationId);
379-
}
380-
381-
if (operation?.requestBody?.content) {
382-
Object.keys(operation.requestBody.content).forEach(contentType => {
383-
requestContent.add(contentType);
384-
});
385-
}
386-
387-
if (operation?.responses) {
388-
Object.values(operation.responses).forEach(response => {
389-
if (response?.content) {
390-
Object.keys(response.content).forEach(contentType => {
391-
responseContent.add(contentType);
382+
383+
if (operation?.requestBody?.content) {
384+
Object.keys(operation.requestBody.content).forEach(contentType => {
385+
requestContent.add(contentType);
392386
});
393387
}
394-
});
395-
}
396388

397-
Object.keys(operation).forEach(key => {
398-
if (key.startsWith('x-')) {
399-
flagValues.add(`${key}: ${operation[key]}`);
389+
if (operation?.responses) {
390+
Object.values(operation.responses).forEach(response => {
391+
if (response?.content) {
392+
Object.keys(response.content).forEach(contentType => {
393+
responseContent.add(contentType);
394+
});
395+
}
396+
});
397+
}
398+
399+
Object.keys(operation).forEach(key => {
400+
if (key.startsWith('x-')) {
401+
flagValues.add(`${key}: ${operation[key]}`);
402+
}
403+
});
400404
}
401405
});
402-
});
406+
}
403407
});
404408
}
405409

utils/split.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ async function writeComponents(components, options) {
6060
const filePath = path.join(fileDir, `${componentName}.${ext}`);
6161

6262
// Update any component references within components
63-
const updatedComponent = convertComponentsToRef(components[componentType][componentName], ext, path.join('components', componentType));
63+
const updatedComponent = convertComponentsToRef(
64+
components[componentType][componentName],
65+
ext,
66+
path.join('components', componentType)
67+
);
6468

6569
// Write each component (schema, parameter, etc.) to its own YAML file
6670
await writeFile(filePath, updatedComponent, options);

0 commit comments

Comments
 (0)