Skip to content

Commit

Permalink
feat: Allow parsing of YAML wrongly-indented multiline strings (#49)
Browse files Browse the repository at this point in the history
PR: #49
  • Loading branch information
osama-salman99 authored Aug 21, 2023
1 parent b92ce38 commit 0197c74
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@expediagroup/spec-transformer",
"version": "1.5.0",
"version": "1.6.0",
"description": "API Spec Transformer",
"repository": {
"type": "git",
Expand Down Expand Up @@ -45,8 +45,7 @@
"commander": "11.0.0",
"js-yaml": "4.1.0",
"openapi-to-postmanv2": "4.15.0",
"openapi3-ts": "3.2.0",
"yaml": "2.3.1"
"openapi3-ts": "3.2.0"
},
"devDependencies": {
"@semantic-release/changelog": "6.0.3",
Expand Down
4 changes: 2 additions & 2 deletions src/io/reader/YamlReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import Reader from './Reader';
import { Key, Value } from '../../model/Types';
import { parse } from 'yaml';
import { load } from "js-yaml";

/**
* A reader implementation for reading the specs in YAML format.
Expand All @@ -25,6 +25,6 @@ export class YamlReader implements Reader {
read(specs: string): Record<Key, Value> {
if (specs === undefined || specs === null || specs === '') return {};

return parse(specs);
return load(specs) as Record<Key, Value>
}
}
62 changes: 62 additions & 0 deletions test/io/reader/YamlReader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,68 @@ describe('test YamlReader', () => {
});
});

it('should return the parsed specs when they contain wrongly-indented, multiline strings', () => {
const yamlSpecs =
'openapi: 3.0.0\n' +
'info:\n' +
' title: Pet Store API\n' +
' version: 1.0.0\n' +
'paths:\n' +
' /pets:\n' +
' get:\n' +
' summary: List all pets\n' +
' description: "this is a wrongly\n' +
' indented multiline string that\n' +
' should be parsed correctly"';

expect(new YamlReader().read(yamlSpecs)).toEqual({
openapi: '3.0.0',
info: {
title: 'Pet Store API',
version: '1.0.0'
},
paths: {
'/pets': {
get: {
summary: 'List all pets',
description: 'this is a wrongly indented multiline string that should be parsed correctly'
}
}
}
});
});

it('should return the parsed specs when they contain wrongly-indented, multiline strings with backslashes', () => {
const yamlSpecs =
'openapi: 3.0.0\n' +
'info:\n' +
' title: Pet Store API\n' +
' version: 1.0.0\n' +
'paths:\n' +
' /pets:\n' +
' get:\n' +
' summary: List all pets\n' +
' description: "this is a wrongly\\\n' +
' \\ indented multiline string that\\\n' +
' \\ should be parsed correctly"';

expect(new YamlReader().read(yamlSpecs)).toEqual({
openapi: '3.0.0',
info: {
title: 'Pet Store API',
version: '1.0.0'
},
paths: {
'/pets': {
get: {
summary: 'List all pets',
description: 'this is a wrongly indented multiline string that should be parsed correctly'
}
}
}
});
});

it('should return an empty record when input is empty', () => {
expect(new YamlReader().read('')).toEqual({});
});
Expand Down

0 comments on commit 0197c74

Please sign in to comment.