Skip to content

Commit 92034dd

Browse files
authored
Merge pull request #5 from temando/readme
Readme
2 parents 58d454d + 62f545c commit 92034dd

File tree

3 files changed

+80
-45
lines changed

3 files changed

+80
-45
lines changed

README.md

+72-42
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,28 @@
1-
# Serverless OpenAPI v3 Documentation Plugin
21

3-
This plugin generates OpenAPI v3 documentation from serverless configuration files. OpenAPI is formerly known as Swagger. The configuration is extends the format specified by [serverless-aws-documentation](https://www.npmjs.com/package/serverless-aws-documentation).
2+
[![Travis CI](https://img.shields.io/travis/temando/serverless-openapi-documentation.svg)](https://travis-ci.org/temando/serverless-openapi-documentation)
3+
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
44

55

6-
## Install
7-
8-
This plugin works for Serverless 1.x and up. Serverless 0.5 is not supported.
9-
10-
To add this plugin to your package.json:
11-
12-
**Using npm:**
13-
```bash
14-
npm install serverless-openapi-documentation --save-dev
15-
```
16-
17-
**Using Yarn:**
18-
```bash
19-
yarn add serverless-openapi-documentation --dev
20-
```
21-
22-
Next you need to add the plugin to the `plugins` section of your `serverless.yml` file.
6+
# Serverless OpenAPI Documentation Plugin
237

24-
```yml
25-
plugins:
26-
- serverless-openapi-documentation
27-
```
8+
Generates [**OpenAPI 3.0 RC2**](https://github.com/OAI/OpenAPI-Specification/tree/OpenAPI.next) documentation from serverless configuration files. OpenAPI is formerly known as Swagger. The configuration is inspired by the format used in [serverless-aws-documentation](https://www.npmjs.com/package/serverless-aws-documentation).
289

29-
You can confirm the plugin is correctly installed by running:
10+
---
3011

31-
```bash
32-
serverless | grep "ServerlessOpenAPIDocumentation"
33-
```
12+
- [Usage](#usage)
13+
- [Options](#options)
14+
- [Configuration](#configuration)
15+
- [Models](#models)
16+
- [Functions](#functions)
17+
- [`queryParams`](#queryparams)
18+
- [`pathParams`](#pathparams)
19+
- [`cookieParams`](#cookieparams)
20+
- [`requestModels`](#requestmodels)
21+
- [`methodResponses`](#methodresponses)
22+
- [Example Configuration](#example-configuration)
23+
- [Install](#install)
3424

35-
It should return `ServerlessOpenAPIDocumentation` as one of the plugins on the list.
25+
---
3626

3727
## Usage
3828

@@ -43,7 +33,9 @@ Below are the commandline options to run the generator:
4333
```bash
4434
serverless openapi generate [options]
4535
```
36+
4637
### Options
38+
4739
```bash
4840
Plugin: ServerlessOpenAPIDocumentation
4941
openapi generate ...................... Generate OpenAPI v3 Documentation
@@ -57,7 +49,7 @@ openapi generate ...................... Generate OpenAPI v3 Documentation
5749

5850
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.
5951

60-
This plugin is compatible with the same documentation configuration as per [serverless-aws-documentation](https://www.npmjs.com/package/serverless-aws-documentation) and can run beside it.
52+
This plugin is compatible with the same documentation configuration structure in [serverless-aws-documentation](https://www.npmjs.com/package/serverless-aws-documentation) and can run beside it.
6153

6254
The `custom` section of your `serverless.yml` can be configured as below:
6355

@@ -67,18 +59,27 @@ custom:
6759
version: '1'
6860
summary: 'My API'
6961
description: 'This is my API'
62+
models: {}
7063
```
7164
72-
If you find this configuration too verbose, you can separate it out into it's own file, such as `serverless.doc.yml` by replacing it with the following:
65+
These configurations can be quite verbose; you can separate it out into it's own file, such as `serverless.doc.yml` as below:
7366

7467
```yml
7568
custom:
7669
documentation: ${file(serverless.doc.yml):documentation}
70+
71+
functions:
72+
myFunc:
73+
events:
74+
- http:
75+
path: getStuff
76+
method: get
77+
documentation: ${file(serverless.doc.yml):endpoints.myFunc}
7778
```
7879

79-
And instead putting the `documentation` section directly into `serverless.doc.yml`.
80+
For more info on `serverless.yml` syntax, see their docs.
8081

81-
#### Defining models
82+
#### Models
8283

8384
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.
8485

@@ -114,7 +115,7 @@ custom:
114115
type: "string"
115116
```
116117

117-
### Function event specific documentation
118+
#### Functions
118119

119120
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.
120121

@@ -182,7 +183,7 @@ functions:
182183
application/json: "ErrorResponse"
183184
```
184185

185-
#### queryParams
186+
#### `queryParams`
186187

187188
Query parameters can be described as follow:
188189

@@ -200,7 +201,7 @@ queryParams:
200201
type: "string"
201202
```
202203

203-
#### pathParams
204+
#### `pathParams`
204205

205206
Path parameters can be described as follow:
206207

@@ -216,7 +217,7 @@ pathParams:
216217
type: "string"
217218
```
218219

219-
#### cookieParams
220+
#### `cookieParams`
220221

221222
Cookie parameters can be described as follow:
222223

@@ -234,7 +235,7 @@ cookieParams:
234235
type: "string"
235236
```
236237

237-
#### requestModels
238+
#### `requestModels`
238239

239240
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](#defining-models) section).
240241

@@ -244,7 +245,7 @@ requestModels:
244245
application/xml: "CreateRequestXML"
245246
```
246247

247-
#### methodResponses
248+
#### `methodResponses`
248249

249250
You can define the response schemas by defining properties for your function event.
250251

@@ -263,7 +264,7 @@ methodResponse:
263264
application/xml: "CreateResponseXML"
264265
```
265266

266-
##### responseModels
267+
##### `responseModels`
267268

268269
The `responseModels` property allows you to define models for the HTTP Response of the function event. You can define a different model for each different `Content-Type`. You can define a reference to the relevant response model named in the `models` section of your configuration (see [Defining Models](#defining-models) section).
269270

@@ -273,7 +274,7 @@ responseModels:
273274
application/xml: "CreateResponseXML"
274275
```
275276

276-
##### responseHeaders
277+
##### `responseHeaders`
277278

278279
The `responseHeaders` section of the configuration allows you to define the HTTP response headers for the function event.
279280

@@ -295,8 +296,37 @@ responseHeaders:
295296

296297
Please view the example [serverless.yml](test/serverless.yml).
297298

298-
## Contribution
299-
`Insert: information on contibution here`
299+
## Install
300+
301+
This plugin works for Serverless 1.x and up. Serverless 0.5 is not supported.
302+
303+
To add this plugin to your package.json:
304+
305+
**Using npm:**
306+
```bash
307+
npm install serverless-openapi-documentation --save-dev
308+
```
309+
310+
**Using Yarn:**
311+
```bash
312+
yarn add serverless-openapi-documentation --dev
313+
```
314+
315+
Next you need to add the plugin to the `plugins` section of your `serverless.yml` file.
316+
317+
```yml
318+
plugins:
319+
- serverless-openapi-documentation
320+
```
321+
322+
You can confirm the plugin is correctly installed by running:
323+
324+
```bash
325+
serverless | grep "ServerlessOpenAPIDocumentation"
326+
```
327+
328+
It should return `ServerlessOpenAPIDocumentation` as one of the plugins on the list.
329+
300330

301331
## License
302332

src/DefinitionGenerator.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ export class DefinitionGenerator {
195195
private getRequestBodiesFromConfig (documentationConfig) {
196196
const requestBodies = {};
197197

198+
if (!documentationConfig.requestModels) {
199+
throw new Error(`Required requestModels in: ${JSON.stringify(documentationConfig, null, 2)}`);
200+
}
201+
198202
// Does this event have a request model?
199203
if (documentationConfig.requestModels) {
200204
// For each request model type (Sorted by "Content-Type")
@@ -211,7 +215,7 @@ export class DefinitionGenerator {
211215
},
212216
};
213217

214-
this.applyExamples(requestModel, reqModelConfig);
218+
this.attachExamples(requestModel, reqModelConfig);
215219

216220
const reqBodyConfig: { content: object, description?: string } = {
217221
content: {
@@ -231,7 +235,7 @@ export class DefinitionGenerator {
231235
return requestBodies;
232236
}
233237

234-
private applyExamples (target, config) {
238+
private attachExamples (target, config) {
235239
if (target.examples && Array.isArray(target.examples)) {
236240
merge(config, { examples: clone(target.examples) });
237241
} else if (target.example) {
@@ -292,7 +296,7 @@ export class DefinitionGenerator {
292296
},
293297
};
294298

295-
this.applyExamples(responseModel, resModelConfig);
299+
this.attachExamples(responseModel, resModelConfig);
296300

297301
merge(content, { [responseKey] : resModelConfig });
298302
}

src/ServerlessOpenApiDocumentation.ts

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export class ServerlessOpenApiDocumentation {
132132

133133
for (const info of validation.error) {
134134
this.log(c.grey('\n\n--------\n\n'));
135+
this.log(' ', c.blue(info.dataPath), '\n');
135136
this.log(' ', info.schemaPath, c.bold.yellow(info.message));
136137
this.log(c.grey('\n\n--------\n\n'));
137138
this.log(`${inspect(info, { colors: true, depth: 2 })}\n\n`);

0 commit comments

Comments
 (0)