Skip to content

Commit 861cac9

Browse files
authored
Merge pull request #71 from JaredCE/allow-for-security-schemes
Allow for security schemes
2 parents ecef9c5 + 18d5d4d commit 861cac9

File tree

6 files changed

+663
-27
lines changed

6 files changed

+663
-27
lines changed

README.md

Lines changed: 108 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,16 @@ Options:
5959
| info.title | custom.documentation.title OR service |
6060
| info.description | custom.documentation.description OR blank string |
6161
| info.version | custom.documentation.version OR random v4 uuid if not provided |
62-
| info.termsOfService | custom.documentation.termsOfService |
63-
| info.contact | custom.documentation.contact |
64-
| info.contact.name | custom.documentation.contact.name OR blank string |
65-
| info.contact.url | custom.documentation.contact.url if provided |
66-
| info.license | custom.documentation.license |
67-
| info.license.name | custom.documentation.license.name OR blank string |
68-
| info.license.url | custom.documentation.license.url if provided |
62+
| info.termsOfService | custom.documentation.termsOfService |
63+
| info.contact | custom.documentation.contact |
64+
| info.contact.name | custom.documentation.contact.name OR blank string |
65+
| info.contact.url | custom.documentation.contact.url if provided |
66+
| info.license | custom.documentation.license |
67+
| info.license.name | custom.documentation.license.name OR blank string |
68+
| info.license.url | custom.documentation.license.url if provided |
6969
| externalDocs.description | custom.documentation.externalDocumentation.description |
7070
| externalDocs.url | custom.documentation.externalDocumentation.url |
71+
| security | custom.documentation.security |
7172
| servers[].description | custom.documentation.servers.description |
7273
| servers[].url | custom.documentation.servers.url |
7374
| servers[].variables | custom.documentation.servers.variables |
@@ -89,6 +90,7 @@ Options:
8990
| path[path].[operation].externalDocs.url | functions.functions.[http OR httpApi].documentation.externalDocumentation.url |
9091
| path[path].[operation].servers[].description | functions.functions.[http OR httpApi].documentation.servers.description |
9192
| path[path].[operation].servers[].url | functions.functions.[http OR httpApi].documentation.servers.url |
93+
| path[path].[operation].security | functions.functions.[http OR httpApi].documentation.security |
9294
| path[path].[operation].deprecated | functions.functions.[http OR httpApi].documentation.deprecated |
9395
| path[path].[operation].parameters | functions.functions.[http OR httpApi].documentation.[path/query/cookie/header]Params |
9496
| path[path].[operation].parameters.name | functions.functions.[http OR httpApi].documentation.[path/query/cookie/header]Params.name |
@@ -219,6 +221,40 @@ functions:
219221

220222
For more info on `serverless.yml` syntax, see their docs.
221223

224+
#### securitySchemes
225+
226+
You can provide optional Security Schemes:
227+
228+
```yml
229+
custom:
230+
documentation:
231+
securitySchemes:
232+
my_api_key:
233+
type: apiKey
234+
name: api_key
235+
in: header
236+
```
237+
238+
It accepts all available Security Schemes and follows the specification: https://spec.openapis.org/oas/v3.0.3#security-scheme-object
239+
240+
#### Security on each operation
241+
242+
To apply an overall security scheme to all of your operations without having to add the documentation to each one, you can write it like:
243+
244+
```yml
245+
custom:
246+
documentation:
247+
securitySchemes:
248+
my_api_key:
249+
type: apiKey
250+
name: api_key
251+
in: header
252+
security:
253+
- my_api_key: []
254+
```
255+
256+
This will apply the requirement of each operation requiring your `my_api_key` security scheme, [you can override this](#security).
257+
222258
#### Models
223259

224260
There are two ways to write the Models. Models contain additional information that you can use to define schemas for endpoints. You must define the *content type* for each schema that you provide in the models.
@@ -304,20 +340,20 @@ custom:
304340
content:
305341
application/json:
306342
schema: &ErrorItem
307-
type: object
308-
properties:
309-
message:
310-
type: string
311-
code:
312-
type: integer
343+
type: object
344+
properties:
345+
message:
346+
type: string
347+
code:
348+
type: integer
313349
314350
- name: "PutDocumentResponse"
315351
description: "PUT Document response model (external reference example)"
316352
content:
317353
application/json:
318354
schema:
319-
type: array
320-
items: *ErrorItem
355+
type: array
356+
items: *ErrorItem
321357
```
322358

323359
`&ErrorItem` in the above example creates a node anchor (&ErrorItem) to the `ErrorResponse` schema which then can be used in the `PutDocumentResponse` schema via the reference (*ErrorItem). The node anchor needs to be declared first before it can be used elsewhere via the reference, swapping the above example around would result in an error.
@@ -340,6 +376,7 @@ The `documentation` section of the event configuration can contain the following
340376
* `pathParams`: a list of path parameters (see [pathParams](#pathparams) below)
341377
* `cookieParams`: a list of cookie parameters (see [cookieParams](#cookieparams) below)
342378
* `headerParams`: a list of headers (see [headerParams](#headerparams---request-headers) below)
379+
* `security`: The security requirement to apply (see [security](#security) below)
343380
* `methodResponses`: an array of response models and applicable status codes
344381
* `statusCode`: applicable http status code (ie. 200/404/500 etc.)
345382
* `responseBody`: contains description of the response
@@ -480,6 +517,62 @@ headerParams:
480517
type: "string"
481518
```
482519

520+
#### `security`
521+
522+
The `security` property allows you to specify the [Security Scheme](#securityschemes) to apply to the HTTP Request. If you have applied an `security` ([see Security on each operation](#security-on-each-operation)) then you can either leave this field off, or to override it with a different scheme you can write it like:
523+
524+
```yml
525+
custom:
526+
documentation:
527+
securitySchemes:
528+
my_api_key:
529+
type: apiKey
530+
name: api_key
531+
in: header
532+
petstore_auth:
533+
type: oauth2
534+
flows:
535+
implicit:
536+
authorizationUrl: https://example.com/api/oauth/dialog
537+
scopes:
538+
write:pets: modify pets in your account
539+
read:pets: read your pets
540+
security:
541+
- my_api_key: []
542+
543+
functions:
544+
getData:
545+
events:
546+
- http:
547+
documentation:
548+
security:
549+
- petstore_auth:
550+
- write:pets
551+
- read:pets
552+
```
553+
554+
If you have specified an `security` at the document root, but this HTTP Request should not apply any security schemes, you should set security to be an array with an empty object:
555+
556+
```yml
557+
custom:
558+
documentation:
559+
securitySchemes:
560+
my_api_key:
561+
type: apiKey
562+
name: api_key
563+
in: header
564+
security:
565+
- my_api_key: []
566+
567+
functions:
568+
getData:
569+
events:
570+
- http:
571+
documentation:
572+
security:
573+
- {}
574+
```
575+
483576
#### `requestModels`
484577

485578
The `requestModels` property allows you to define models for the HTTP Request of the function event. You can define a different model for each different `Content-Type`. You can define a reference to the relevant request model named in the `models` section of your configuration (see [Defining Models](#models) section).

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-openapi-documenter",
3-
"version": "0.0.32",
3+
"version": "0.0.33",
44
"description": "Generate OpenAPI v3 documentation and Postman Collections from your Serverless Config",
55
"main": "index.js",
66
"keywords": [
@@ -42,7 +42,7 @@
4242
},
4343
"devDependencies": {
4444
"chai": "^4.3.7",
45-
"mocha": "^10.1.0",
45+
"mocha": "^10.2.0",
4646
"sinon": "^15.0.0"
4747
}
4848
}

0 commit comments

Comments
 (0)