Skip to content
This repository was archived by the owner on Dec 10, 2021. It is now read-only.

Commit c3262b7

Browse files
authored
Merge pull request #17 from conqa/add-security
Add security
2 parents 8678434 + 8a19881 commit c3262b7

File tree

6 files changed

+49
-8
lines changed

6 files changed

+49
-8
lines changed

src/DefinitionGenerator.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,28 @@ export class DefinitionGenerator {
4040
title = "",
4141
description = "",
4242
version = uuid.v4(),
43-
models
43+
models,
44+
security,
45+
securitySchemes
4446
} = this.config;
4547

4648
_.merge(this.definition, {
4749
openapi: this.version,
4850
info: { title, description, version },
4951
paths: {},
5052
components: {
51-
schemas: {},
52-
securitySchemes: {}
53+
schemas: {}
5354
}
5455
});
5556

57+
if (security) {
58+
this.definition.security = security;
59+
}
60+
61+
if (securitySchemes) {
62+
this.definition.components.securitySchemes = securitySchemes;
63+
}
64+
5665
this.definition.components.schemas = await parseModels(models, this.root);
5766

5867
return this;

src/__tests__/DefinitionGenerator.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ describe("OpenAPI Documentation Generator", () => {
6262
docGen.readFunctions(funcConfigs);
6363

6464
// get the parameters from the `/create POST' endpoint
65-
const actual = docGen.definition.paths["/create"].post.parameters;
65+
const actual =
66+
docGen.definition.paths["/create/{username}"].post.parameters;
6667
const expected = [
6768
{
6869
description: "The username for a user to create",

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { JSONSchema7 } from "json-schema";
2+
import { OpenAPIV3 } from "openapi-types";
23

34
export interface Model {
45
name: string;
@@ -13,6 +14,8 @@ export interface DefinitionConfig {
1314
title: string;
1415
description: string;
1516
version?: string;
17+
securitySchemes: OpenAPIV3.SecuritySchemeObject;
18+
security: Array<OpenAPIV3.SecurityRequirementObject>;
1619
models: Array<Model>;
1720
}
1821

test/project/openapi.yml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,22 @@ components:
185185
properties:
186186
data:
187187
$ref: '#/components/schemas/data'
188-
securitySchemes: {}
188+
securitySchemes:
189+
bearerAuth:
190+
type: oauth2
191+
flows:
192+
implicit:
193+
authorizationUrl: 'https://example.com/oauth/authorize'
194+
scopes:
195+
read: Grants read access
196+
write: Grants write access
197+
admin: Grants access to admin operations
189198
info:
190199
title: ''
191200
description: ''
192-
version: 96d3d004-1a24-4384-9df7-1e7416393223
201+
version: 6e48be1a-d9be-41ef-95eb-41ffad58eac3
193202
paths:
194-
/create:
203+
'/create/{username}':
195204
post:
196205
operationId: createUser
197206
summary: Create User
@@ -239,3 +248,7 @@ paths:
239248
application/json:
240249
schema:
241250
$ref: '#/components/schemas/ErrorResponse'
251+
security:
252+
- bearerAuth:
253+
- read
254+
- write

test/project/serverless.doc.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
documentation:
2+
components:
3+
securitySchemes:
4+
bearerAuth:
5+
type: oauth2
6+
flows:
7+
implicit:
8+
authorizationUrl: https://example.com/oauth/authorize
9+
scopes:
10+
read: Grants read access
11+
write: Grants write access
12+
admin: Grants access to admin operations
13+
security:
14+
- bearerAuth:
15+
- read
16+
- write
217
models:
318
- name: ErrorResponse
419
description: This is an error

test/project/serverless.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ functions:
1414
handler: handler.create
1515
events:
1616
- http:
17-
path: create
17+
path: create/{username}
1818
method: post
1919
documentation:
2020
summary: Create User

0 commit comments

Comments
 (0)