Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: yaml export format #238

Merged
merged 2 commits into from
Jan 7, 2021
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: 2 additions & 0 deletions bin/swagger-jsdoc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

const fs = require('fs');
const path = require('path');
const program = require('commander');

const pkg = require('../package.json');
Expand Down Expand Up @@ -76,6 +77,7 @@ fs.writeFileSync(
swaggerJsdoc({
swaggerDefinition,
apis: program.args,
format: path.extname(output || ''),
}),
null,
2
Expand Down
1 change: 1 addition & 0 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { build } = require('./specification');
* Generates the specification.
* @param {object} options - Configuration options
* @param {string} options.encoding Optional, passed to readFileSync options. Defaults to 'utf8'.
* @param {string} options.format Optional, defaults to '.json' - target file format '.yml' or '.yaml'.
* @param {object} options.swaggerDefinition
* @param {object} options.definition
* @param {array} options.apis
Expand Down
19 changes: 15 additions & 4 deletions src/specification.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ function prepare(definition) {
return swaggerObject;
}

/**
* @param {object} obj
* @param {string} ext
*/
function format(swaggerObject, ext) {
if (ext === '.yml' || ext === '.yaml') {
return YAML.stringify(swaggerObject);
}
return swaggerObject;
}

/**
* OpenAPI specification validator does not accept empty values for a few properties.
* Solves validator error: "Schema error should NOT have additional properties"
Expand Down Expand Up @@ -82,7 +93,7 @@ function clean(swaggerObject) {
* @param {object} swaggerObject - Swagger object from parsing the api files.
* @returns {object} The specification.
*/
function finalize(swaggerObject) {
function finalize(swaggerObject, options) {
let specification = swaggerObject;

parser.parse(swaggerObject, (err, api) => {
Expand All @@ -95,7 +106,7 @@ function finalize(swaggerObject) {
specification = clean(specification);
}

return specification;
return format(specification, options.format);
}

/**
Expand Down Expand Up @@ -265,7 +276,7 @@ function build(options) {
}
}

return finalize(specification);
return finalize(specification, options);
}

module.exports = { prepare, build, organize, finalize };
module.exports = { prepare, build, organize, finalize, format };
11 changes: 11 additions & 0 deletions test/specification.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,15 @@ describe('Specification module', () => {
});
});
});

describe('format', () => {
it('should not modify input object when no format specified', () => {
expect(specModule.format({ foo: 'bar' })).toEqual({ foo: 'bar' });
});

it('should support yaml', () => {
expect(specModule.format({ foo: 'bar' }, '.yaml')).toEqual('foo: bar\n');
expect(specModule.format({ foo: 'bar' }, '.yml')).toEqual('foo: bar\n');
});
});
});