Skip to content

Adding support for APIs in definition file. #72

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

Merged
merged 1 commit into from
May 3, 2017
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
14 changes: 12 additions & 2 deletions bin/swagger-jsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,18 @@ fs.readFile(program.definition, 'utf-8', function(err, data) {
}

// Continue only if arguments provided.
if (!program.args.length) {
return console.log('You must provide arguments for reading APIs.');
if (!swaggerDefinition.apis && !program.args.length) {
console.log('You must provide sources for reading API files.');
// jscs:disable maximumLineLength
return console.log('Either add filenames as arguments, or add an api key in your definitions file.');
}

// If there's no argument passed, but the user has defined Apis in
// the definition file, pass them them onwards.
if (program.args.length === 0 &&
swaggerDefinition.apis &&
swaggerDefinition.apis instanceof Array) {
program.args = swaggerDefinition.apis;
}

// If watch flag is turned on, listen for changes.
Expand Down
6 changes: 4 additions & 2 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ $ swagger-jsdoc -h

#### Specify a definition file

Swagger-jsdoc will read the `api` array in your definition file by default for file paths it should read.

```
$ swagger-jsdoc -d swaggerDef.js
$ swagger-jsdoc -d swaggerDef.js -o doc.json
```

This could be any .js or .json file which will be `require()`-ed and parsed/validated as JSON.

#### Specify input files
#### Specify input files (optional)

```
$ swagger-jsdoc route1.js route2.js
Expand Down
17 changes: 16 additions & 1 deletion test/cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('command line interface', function () {
if (error) {
throw new Error(error, stderr);
}
expect(stdout).to.contain('You must provide arguments for reading APIs.');
expect(stdout).to.contain('You must provide sources for reading API files.');
done();
});
});
Expand All @@ -100,6 +100,21 @@ describe('command line interface', function () {
});
});

it('should create swagger.json by default when the API input is from definition file', function (done) {
var goodInput = process.env.PWD + '/bin/swagger-jsdoc.js -d test/fixtures/api_definition.js';
exec(goodInput, function (error, stdout, stderr) {
if (error) {
throw new Error(error, stderr);
}
expect(stdout).to.contain('Swagger specification is ready.');
expect(stderr).to.not.contain('You are using properties to be deprecated');
var specification = fs.statSync('swagger.json');
// Check that the physical file was created.
expect(specification.nlink).to.be.above(0);
done();
});
});

it('should accept custom configuration for output specification', function (done) {
var goodInput = process.env.PWD + '/bin/swagger-jsdoc.js -d example/swaggerDef.js -o customSpec.json example/routes.js';
exec(goodInput, function (error, stdout, stderr) {
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/api_definition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
info: { // API informations (required)
title: 'Hello World', // Title (required)
version: '1.0.0', // Version (required)
description: 'A sample API', // Description (optional)
},
apis: ['./**/*/routes.js']
};