Skip to content

Commit

Permalink
fix: yaml export format (#238)
Browse files Browse the repository at this point in the history
* save changes

* refactor
  • Loading branch information
kalinchernev authored Jan 7, 2021
1 parent 7aaa3e0 commit 62d7c69
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
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');
});
});
});

0 comments on commit 62d7c69

Please sign in to comment.