Skip to content

Commit 332fba2

Browse files
authored
Merge pull request #122 from JaredCE/improve-configuration-schemas
Improve configuration schemas
2 parents c26f724 + cbccc86 commit 332fba2

File tree

5 files changed

+39
-17
lines changed

5 files changed

+39
-17
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616

17-
This will generate an OpenAPI V3 (up to v3.0.3) file for you from your serverless file. It can optionally generate a [Postman Collection V2](https://github.com/postmanlabs/openapi-to-postman) from the OpenAPI file for you too.
17+
This will generate an OpenAPI V3 (up to v3.0.3) file for you from your serverless file. It can optionally generate a [Postman Collection V2](https://github.com/postmanlabs/openapi-to-postman) from the OpenAPI file for you too. This currently works for `http` and `httpApi` configurations.
1818

1919
Originally based off of: https://github.com/temando/serverless-openapi-documentation
2020

@@ -131,7 +131,7 @@ Options:
131131

132132
### Configuration
133133

134-
To configure this plugin to generate valid OpenAPI documentation there are two places you'll need to modify in your `serverless.yml` file, the `custom` variables section and the `http` event section for each given function in your service.
134+
To configure this plugin to generate valid OpenAPI documentation there are two places you'll need to modify in your `serverless.yml` file, the `custom` variables section and the `http/httpApi` event section for each given function in your service.
135135

136136
The `custom` section of your `serverless.yml` can be configured as below:
137137

@@ -164,7 +164,7 @@ custom:
164164
models: {}
165165
```
166166

167-
Mostly everything here is optional. A version from a UUID will be generated for you if you don't specify one, title will be the name of your service if you don't specify one.
167+
Mostly everything here is optional. A version from a UUID will be generated for you if you don't specify one, title will be the name of your service if you don't specify one. You will need to specify the `documentation` top object.
168168

169169
#### termsOfService
170170

@@ -483,7 +483,7 @@ functions:
483483

484484
#### Functions
485485

486-
To define the documentation for a given function event, you need to create a `documentation` attribute for your http event in your `serverless.yml` file.
486+
To define the documentation for a given function event, you need to create a `documentation` attribute for your `http` or `httpApi` event in your `serverless.yml` file.
487487

488488
The `documentation` section of the event configuration can contain the following attributes:
489489

@@ -506,6 +506,8 @@ The `documentation` section of the event configuration can contain the following
506506
* `responseHeaders`: a list of response headers (see [responseHeaders](#responseheaders) below)
507507
* `responseModels`: a list of models to describe the request bodies (see [responseModels](#responsemodels) below) for each `Content-Type`
508508

509+
If you don't want a `http` or `httpApi` event to be documented, you can leave off the `documentation` object. The configuration schema will only check that you have specified a `methodResponses` on the `documentation` event, previously the plugin would cause serverless to warn or error (depending on your `configValidationMode`) if you had not supplied a `documentation` on an event.
510+
509511
```yml
510512
functions:
511513
createUser:

package-lock.json

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

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
{
22
"name": "serverless-openapi-documenter",
3-
"version": "0.0.65",
3+
"version": "0.0.66",
44
"description": "Generate OpenAPI v3 documentation and Postman Collections from your Serverless Config",
55
"main": "index.js",
66
"keywords": [
77
"serverless",
88
"serverless2",
9+
"Serverless 2",
910
"serverless3",
11+
"Serverless 3",
1012
"serverless framework",
1113
"serverless framework plugin",
1214
"serverless plugin",
@@ -15,7 +17,10 @@
1517
"openAPI3",
1618
"PostmanCollections",
1719
"Postman-Collections",
18-
"Postman Collections"
20+
"Postman Collections",
21+
"AWS",
22+
"AWS APIGateway",
23+
"Api Gateway"
1924
],
2025
"scripts": {
2126
"test": "mocha --config './test/.mocharc.js'"

src/openAPIGenerator.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,33 @@ class OpenAPIGenerator {
7171

7272
this.customVars = this.serverless.variables.service.custom;
7373

74-
this.serverless.configSchemaHandler.defineFunctionEventProperties('aws', 'http', {
74+
const functionEventDocumentationSchema = {
7575
properties: {
76-
documentation: { type: 'object' },
76+
documentation: {
77+
type: 'object',
78+
properties: {
79+
methodResponses: {
80+
type: 'array'
81+
},
82+
},
83+
required: ['methodResponses']
84+
},
7785
},
78-
required: ['documentation'],
79-
});
86+
}
8087

81-
this.serverless.configSchemaHandler.defineFunctionEventProperties('aws', 'httpApi', {
88+
this.serverless.configSchemaHandler.defineCustomProperties({
89+
type: 'object',
8290
properties: {
83-
documentation: { type: 'object' },
91+
documentation: {
92+
type: 'object'
93+
}
8494
},
85-
required: ['documentation'],
86-
});
95+
required: ['documentation']
96+
})
97+
98+
this.serverless.configSchemaHandler.defineFunctionEventProperties('aws', 'http', functionEventDocumentationSchema);
99+
100+
this.serverless.configSchemaHandler.defineFunctionEventProperties('aws', 'httpApi', functionEventDocumentationSchema);
87101

88102
this.serverless.configSchemaHandler.defineFunctionProperties('aws', {
89103
properties: {

test/unit/openAPIGenerator.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ describe('OpenAPIGenerator', () => {
3131
},
3232
configSchemaHandler: {
3333
defineFunctionEventProperties: () => {},
34-
defineFunctionProperties: () => {}
34+
defineFunctionProperties: () => {},
35+
defineCustomProperties: () => {},
3536
},
3637
classes: {
3738
Error: class ServerlessError {constructor(err) {return new Error(err)}}

0 commit comments

Comments
 (0)