Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.

Commit 741a55b

Browse files
committed
fix globbing bug and update interface
1 parent cb268dd commit 741a55b

File tree

15 files changed

+189
-149
lines changed

15 files changed

+189
-149
lines changed

README.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,30 @@ $ yarn add openapi-comment-parser
2020

2121
## Usage
2222

23-
```js
24-
const commentParser = require('openapi-comment-parser');
23+
Create a yaml file with a base OpenAPI definition:
24+
```yaml
25+
openapi: 3.0.0
26+
info:
27+
title: Title of your service
28+
version: 1.0.0
29+
```
2530
26-
// normal OpenAPI definition
27-
const baseDefinition = {
28-
openapi: '3.0.0',
29-
info: {
30-
title: 'Title of your service',
31-
version: '1.0.0',
32-
},
33-
};
31+
Add the following to your code:
32+
```js
33+
const openapi = require('openapi-comment-parser');
3434

35-
const spec = commentParser(baseDefinition);
35+
const spec = openapi();
3636
```
3737

3838
### Swagger UI Express example
3939
Swagger UI Express is a popular module that allows you to serve OpenAPI docs from express.
4040
The result is living documentation for your API hosted from your API server via a route.
4141

4242
```js
43-
const path = require('path');
44-
const commentParser = require('openapi-comment-parser');
43+
const openapi = require('openapi-comment-parser');
4544
const swaggerUi = require('swagger-ui-express');
4645

47-
const spec = commentParser(baseDefinition);
46+
const spec = openapi();
4847

4948
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(spec));
5049
```
@@ -53,7 +52,7 @@ app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(spec));
5352
There are a few configuring options. For example, including and excluding certain
5453
files and paths:
5554
```js
56-
const spec = commentParser(baseDefinition, {
55+
const spec = openapi({
5756
exclude: ['**/some/path/**']
5857
});
5958
```

example/src/app.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
const swaggerUi = require('swagger-ui-express');
22
const express = require('express');
33

4-
const parseComments = require('../../dist/openapi-comment-parser');
4+
const openapi = require('../../dist/openapi-comment-parser');
55

66
const petRouter = require('./routes/pet');
77
const storeRouter = require('./routes/store');
88
const userRouter = require('./routes/user');
9-
const definition = require('./openapi-definition');
109

1110
const app = express();
1211
const PORT = 3000;
@@ -15,7 +14,7 @@ const PORT = 3000;
1514
app.use(express.json());
1615
app.use(express.urlencoded({ extended: false }));
1716

18-
const spec = parseComments(definition);
17+
const spec = openapi();
1918

2019
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(spec));
2120

example/src/openapi-definition.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

example/src/petstore.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
openapi: 3.0.3
2+
info:
3+
title: Swagger Petstore
4+
version: 1.0.5
5+
description: >
6+
This is a sample server Petstore server. You can find out more about Swagger
7+
at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).
8+
For this sample, you can use the api key `special-key` to test the authorization filters.
9+
termsOfService: http://swagger.io/terms/
10+
contact:
11+
12+
license:
13+
name: Apache 2.0
14+
url: http://www.apache.org/licenses/LICENSE-2.0.html
15+
16+
tags:
17+
- name: pet
18+
description: Everything about your Pets
19+
externalDocs:
20+
description: Find out more
21+
url: http://swagger.io
22+
- name: store
23+
description: Access to Petstore orders
24+
- name: user
25+
description: Operations about user
26+
externalDocs:
27+
description: Find out more about our store
28+
url: http://swagger.io
29+
30+
externalDocs:
31+
description: Find out more about Swagger
32+
url: http://swagger.io
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/SpecBuilder.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,18 @@ class SpecBuilder implements OpenApiObject {
3434

3535
addData(parsedFile: OpenApiObject[]) {
3636
parsedFile.forEach((file) => {
37-
objectMerge(this, file);
37+
const { paths, components, ...rest } = file;
38+
39+
// only merge paths and components
40+
objectMerge(this, {
41+
paths: paths,
42+
components: components,
43+
} as OpenApiObject);
44+
45+
// overwrite everthing else:
46+
Object.entries(rest).forEach(([key, value]) => {
47+
this[key as keyof OpenApiObject] = value;
48+
});
3849
});
3950
}
4051
}

0 commit comments

Comments
 (0)