Skip to content

Commit

Permalink
Merge pull request #48 from lifeomic/yaml-output-fix
Browse files Browse the repository at this point in the history
fix: stop adding newlines to yaml output
  • Loading branch information
swain authored Aug 4, 2022
2 parents c9cf2da + ea8bd19 commit 9e7db8a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ node_modules
coverage
.vscode
dist/
src/test-generated.js
src/test-generated.d.ts
src/test-schema.json
src/test-generated.*
75 changes: 74 additions & 1 deletion src/bin/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { execSync } from 'child_process';
import { writeFileSync } from 'fs';
import { readFileSync, writeFileSync } from 'fs';
import { tmpNameSync } from 'tmp';
import { OneSchemaDefinition } from '../types';
import * as yaml from 'js-yaml';

const executeCLI = (command: string): string => {
try {
Expand All @@ -14,6 +16,15 @@ const executeCLI = (command: string): string => {
}
};

/**
* Writes the `schema`, and returns its filepath.
*/
const writeSchema = (schema: OneSchemaDefinition): string => {
const path = `${__dirname}/../test-schema.json`;
writeFileSync(path, JSON.stringify(schema, null, 2), { encoding: 'utf8' });
return path;
};

describe('schema validation snapshots', () => {
const SCENARIOS: {
test: string;
Expand Down Expand Up @@ -78,3 +89,65 @@ describe('input validation snapshots', () => {
});
});
});

describe('generate-open-api-spec', () => {
it('does not create new newlines when serializing + deserializing to YAML', () => {
const description =
'This is a long description that might go over the js-yaml default line length.\nIt also contains newlines.\n\nLots of newlines!';

const path = writeSchema({
Endpoints: {
'GET /something': {
Name: 'getSomething',
Description: description,
Response: {
type: 'object',
},
},
},
});

const output = `${__dirname}/../test-generated.yaml`;

executeCLI(
`generate-open-api-spec --schema '${path}' --output '${output}' --apiTitle 'test title'`,
);

const yamlContent = readFileSync(output, { encoding: 'utf8' });

// Assert the output looks right.
expect(yamlContent.trim()).toStrictEqual(
`
openapi: 3.0.0
info:
version: 1.0.0
title: test title
components: {}
paths:
/something:
get:
operationId: getSomething
description: |-
This is a long description that might go over the js-yaml default line length.
It also contains newlines.
Lots of newlines!
responses:
"200":
description: A successful response
content:
application/json:
schema:
additionalProperties: false
type: object
`.trim(),
);

// Assert the output deserializes correctly.
const openapi: any = yaml.load(yamlContent);

expect(openapi.paths['/something'].get.description).toStrictEqual(
description,
);
});
});
12 changes: 11 additions & 1 deletion src/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,17 @@ const program = yargs(process.argv.slice(2))

const output =
argv.output.endsWith('.yml') || argv.output.endsWith('.yaml')
? dump(openAPISpec)
? dump(openAPISpec, {
/**
* Without this, js-yaml will default to a line width of 80 and use
* the "folded" multiline style. While this should not actually affect
* serialization or deserialization, it can result in ugly-looking output
* that contains newlines in unexpected places.
*
* This option allows us to preserve the original developer's newlines.
*/
lineWidth: -1,
})
: JSON.stringify(openAPISpec, null, 2);

writeGeneratedFile(argv.output, output, { format: argv.format });
Expand Down

0 comments on commit 9e7db8a

Please sign in to comment.