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

Remove 'api' attr from swagger definition #234

Merged
merged 2 commits into from
Dec 12, 2020
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
6 changes: 3 additions & 3 deletions bin/swagger-jsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ if (
}

// Continue only if arguments provided.
if (!swaggerDefinition.apis && !program.args.length) {
if (!program.args.length) {
console.log('You must provide sources for reading API files.');
console.log(
'Either add filenames as arguments, or add an "apis" key in your definitions file.'
'Either add filenames as arguments, or add an "apis" key in your configuration.'
);
process.exit();
}
Expand All @@ -75,7 +75,7 @@ fs.writeFileSync(
JSON.stringify(
swaggerJsdoc({
swaggerDefinition,
apis: swaggerDefinition.apis || program.args,
apis: program.args,
}),
null,
2
Expand Down
8 changes: 4 additions & 4 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ swagger-jsdoc -h

### Definition file

By default, the library will read the `apis` array from your definition file:
Adding `-d` parameter you can speciify easily a definition file.

```bash
swagger-jsdoc -d swaggerDefinition.js
swagger-jsdoc -d swaggerDefinition.js route*.js component*.yaml
```

This could be any `.js`, `.json`, `.yml` or `.yaml` extensions.

### Input files (optional)

When you don't want to or can't pass `apis` from the definition above, specify like following:
Except the `definition file` mostly you would like to add `apis` definitions to swagger. You can specify it like following:

One by one:

Expand All @@ -55,7 +55,7 @@ These paths are relative to current directory from where `swagger-jsdoc` is ran,
The output is `swagger.json` by default, but can be changed:

```bash
swagger-jsdoc -d swaggerDefinition.js -o my_spec.json
swagger-jsdoc -d swaggerDefinition.js route1.js -o my_spec.json
```

When `.yaml` or `.yml` extension is used, the specification will be parsed and saved in YAML.
1 change: 0 additions & 1 deletion example/yaml-anchors-aliases/yaml-anchors-aliases.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ describe('Example for using anchors and aliases in YAML documents', () => {
'./example/yaml-anchors-aliases/example.js',
],
});

expect(result).toEqual(referenceSpecification);
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "swagger-jsdoc",
"description": "Generates swagger doc based on JSDoc",
"version": "6.0.0-rc.3",
"version": "6.0.0-rc.4",
"engines": {
"node": ">=12.0.0"
},
Expand Down
21 changes: 12 additions & 9 deletions src/specification.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,18 @@ function build(options) {
});
}

// Place to provide feedback for errors. Previously throwing, now reporting only.
console.info(
'Not all input has been taken into account at your final specification.'
);
const errReport = yamlDocsErrors.map((documentWithErrors) => {
const { errors } = documentWithErrors;
return errors.join('\n');
});
console.error(`Here's the report: \n\n\n ${errReport}`);
const errReport = yamlDocsErrors
.map(({ errors }) => errors.join('\n'))
.filter((error) => !!error);

if (errReport.length) {
// Place to provide feedback for errors. Previously throwing, now reporting only.
console.info(
'Not all input has been taken into account at your final specification.'
);

console.error(`Here's the report: \n\n\n ${errReport}`);
}
}

for (const document of yamlDocsReady) {
Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/cli.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ More at http://swagger.io/specification/#infoObject

exports[`CLI module should require arguments with jsDoc data about an API 1`] = `
"You must provide sources for reading API files.
Either add filenames as arguments, or add an \\"apis\\" key in your definitions file.
Either add filenames as arguments, or add an \\"apis\\" key in your configuration.
"
`;

Expand Down
17 changes: 13 additions & 4 deletions test/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ describe('CLI module', () => {
});

it('should create swagger.json by default when the API input is from definition file', async () => {
const result = await sh(`${bin} -d test/files/v2/api_definition.js`);
const result = await sh(
`${bin} -d test/files/v2/api_definition.js example/app/routes.js`
);
expect(result.stdout).toBe('Swagger specification is ready.\n');
const specification = fs.statSync('swagger.json');
expect(specification.nlink).not.toBe(0);
Expand All @@ -72,21 +74,27 @@ describe('CLI module', () => {
});

it('should allow a JavaScript definition file', async () => {
const result = await sh(`${bin} -d test/files/v2/api_definition.js`);
const result = await sh(
`${bin} -d test/files/v2/api_definition.js example/app/routes.js`
);
expect(result.stdout).toBe('Swagger specification is ready.\n');
const specification = fs.statSync('swagger.json');
expect(specification.nlink).not.toBe(0);
});

it('should allow a JSON definition file', async () => {
const result = await sh(`${bin} -d test/files/v2/api_definition.json`);
const result = await sh(
`${bin} -d test/files/v2/api_definition.json example/app/routes.js`
);
expect(result.stdout).toBe('Swagger specification is ready.\n');
const specification = fs.statSync('swagger.json');
expect(specification.nlink).not.toBe(0);
});

it('should allow a YAML definition file', async () => {
const result = await sh(`${bin} -d test/files/v2/api_definition.yaml`);
const result = await sh(
`${bin} -d test/files/v2/api_definition.yaml example/app/routes.js`
);
expect(result.stdout).toBe('Swagger specification is ready.\n');
const specification = fs.statSync('swagger.json');
expect(specification.nlink).not.toBe(0);
Expand All @@ -106,6 +114,7 @@ describe('CLI module', () => {
const result = await sh(
`${bin} -d example/app/swaggerDefinition.js test/files/v2/wrong-yaml-identation.js`
);

expect(result.stdout).toContain(
'Not all input has been taken into account at your final specification.'
);
Expand Down
1 change: 0 additions & 1 deletion test/files/v2/api_definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ module.exports = {
version: '1.0.0', // Version (required)
description: 'A sample API', // Description (optional)
},
apis: ['./**/*/routes.js'],
};
3 changes: 1 addition & 2 deletions test/files/v2/api_definition.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
"title": "Hello World",
"version": "1.0.0",
"description": "A sample API"
},
"apis": ["./**/*/routes.js"]
}
}
1 change: 0 additions & 1 deletion test/files/v2/api_definition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ info:
title: Hello World
version: 1.0.0
description: A sample API
apis: [./**/*/routes.js]