Skip to content

Commit 6011557

Browse files
Jesse O'Brienkalinchernev
authored andcommitted
Adding support for APIs in definition file. (#72)
1 parent dbbbc2c commit 6011557

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

bin/swagger-jsdoc.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,18 @@ fs.readFile(program.definition, 'utf-8', function(err, data) {
111111
}
112112

113113
// Continue only if arguments provided.
114-
if (!program.args.length) {
115-
return console.log('You must provide arguments for reading APIs.');
114+
if (!swaggerDefinition.apis && !program.args.length) {
115+
console.log('You must provide sources for reading API files.');
116+
// jscs:disable maximumLineLength
117+
return console.log('Either add filenames as arguments, or add an api key in your definitions file.');
118+
}
119+
120+
// If there's no argument passed, but the user has defined Apis in
121+
// the definition file, pass them them onwards.
122+
if (program.args.length === 0 &&
123+
swaggerDefinition.apis &&
124+
swaggerDefinition.apis instanceof Array) {
125+
program.args = swaggerDefinition.apis;
116126
}
117127

118128
// If watch flag is turned on, listen for changes.

docs/CLI.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ $ swagger-jsdoc -h
1313

1414
#### Specify a definition file
1515

16+
Swagger-jsdoc will read the `api` array in your definition file by default for file paths it should read.
17+
1618
```
17-
$ swagger-jsdoc -d swaggerDef.js
19+
$ swagger-jsdoc -d swaggerDef.js -o doc.json
1820
```
1921

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

22-
#### Specify input files
24+
#### Specify input files (optional)
2325

2426
```
2527
$ swagger-jsdoc route1.js route2.js

test/cli-test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('command line interface', function () {
8080
if (error) {
8181
throw new Error(error, stderr);
8282
}
83-
expect(stdout).to.contain('You must provide arguments for reading APIs.');
83+
expect(stdout).to.contain('You must provide sources for reading API files.');
8484
done();
8585
});
8686
});
@@ -100,6 +100,21 @@ describe('command line interface', function () {
100100
});
101101
});
102102

103+
it('should create swagger.json by default when the API input is from definition file', function (done) {
104+
var goodInput = process.env.PWD + '/bin/swagger-jsdoc.js -d test/fixtures/api_definition.js';
105+
exec(goodInput, function (error, stdout, stderr) {
106+
if (error) {
107+
throw new Error(error, stderr);
108+
}
109+
expect(stdout).to.contain('Swagger specification is ready.');
110+
expect(stderr).to.not.contain('You are using properties to be deprecated');
111+
var specification = fs.statSync('swagger.json');
112+
// Check that the physical file was created.
113+
expect(specification.nlink).to.be.above(0);
114+
done();
115+
});
116+
});
117+
103118
it('should accept custom configuration for output specification', function (done) {
104119
var goodInput = process.env.PWD + '/bin/swagger-jsdoc.js -d example/swaggerDef.js -o customSpec.json example/routes.js';
105120
exec(goodInput, function (error, stdout, stderr) {

test/fixtures/api_definition.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
info: { // API informations (required)
3+
title: 'Hello World', // Title (required)
4+
version: '1.0.0', // Version (required)
5+
description: 'A sample API', // Description (optional)
6+
},
7+
apis: ['./**/*/routes.js']
8+
};

0 commit comments

Comments
 (0)