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

dereference + async #262

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const options = {
apis: ['./src/routes*.js'], // files containing annotations as above
};

const openapiSpecification = swaggerJsdoc(options);
const openapiSpecification = await swaggerJsdoc(options);
```

The resulting `openapiSpecification` will be a [swagger tools](https://swagger.io/tools/)-compatible (and validated) specification.
Expand Down Expand Up @@ -70,6 +70,22 @@ yarn add swagger-jsdoc
- OpenAPI 3.x
- Swagger 2

## $ref dereference

If you want to dereference all refs in specification set `dereference` flag in options.

```javascript
const options = {
dereference: true,
};
```

## Breaking changes

## 7.x

- Result of `swaggerJsdoc()` is async

## Documentation

Click on the version you are using for further details:
Expand Down
18 changes: 9 additions & 9 deletions bin/swagger-jsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ if (!program.args.length) {

const format = path.extname(output);

const result = swaggerJsdoc({
swaggerJsdoc({
swaggerDefinition,
apis: program.args,
format,
});

if (format === '.json') {
fs.writeFileSync(output, JSON.stringify(result, null, 2));
} else {
fs.writeFileSync(output, result);
}
}).then((result) => {
if (format === '.json') {
fs.writeFileSync(output, JSON.stringify(result, null, 2));
} else {
fs.writeFileSync(output, result);
}

console.log('Swagger specification is ready.');
console.log('Swagger specification is ready.');
});
7 changes: 3 additions & 4 deletions examples/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ const options = {
apis: ['./examples/app/routes*.js', './examples/app/parameters.yaml'],
};

// Initialize swagger-jsdoc -> returns validated swagger spec in json format
const swaggerSpec = swaggerJsdoc(options);

// Serve swagger docs the way you like (Recommendation: swagger-tools)
app.get('/api-docs.json', (req, res) => {
app.get('/api-docs.json', async (req, res) => {
res.setHeader('Content-Type', 'application/json');
// Initialize swagger-jsdoc -> returns validated swagger spec in json format
const swaggerSpec = await swaggerJsdoc(options);
res.send(swaggerSpec);
});

Expand Down
4 changes: 2 additions & 2 deletions examples/extensions/extensions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const swaggerJsdoc = require('../..');
const referenceSpecification = require('./reference-specification.json');

describe('Example for using extensions', () => {
it('should support x-webhooks', () => {
const result = swaggerJsdoc({
it('should support x-webhooks', async () => {
const result = await swaggerJsdoc({
swaggerDefinition: {
info: {
title: 'Example with extensions',
Expand Down
4 changes: 2 additions & 2 deletions examples/yaml-anchors-aliases/yaml-anchors-aliases.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const swaggerJsdoc = require('../..');
const referenceSpecification = require('./reference-specification.json');

describe('Example for using anchors and aliases in YAML documents', () => {
it('should handle references in a separate YAML file', () => {
const result = swaggerJsdoc({
it('should handle references in a separate YAML file', async () => {
const result = await swaggerJsdoc({
swaggerDefinition: {
info: {
title: 'Example with anchors and aliases',
Expand Down
Loading