serverless-openapi-documenter 0.0.63 → 0.0.65

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -61,6 +61,7 @@ Options:
61
61
  #### Model Details
62
62
  * [Models](#models)
63
63
  * [Notes on Schemas](#notes-on-schemas)
64
+ * [Request Schema Validators](#serverless-request-schema-validators)
64
65
  #### Response Headers
65
66
  * [CORS](#cors)
66
67
  * [OWASP Secure Headers](#owasp)
@@ -409,6 +410,76 @@ custom:
409
410
  type: string
410
411
  ```
411
412
 
413
+ ##### Serverless Request Schema Validators
414
+
415
+ As of 0.0.64, you can now make use of [Request Schema Validators](https://www.serverless.com/framework/docs/providers/aws/events/apigateway#request-schema-validators). This allows you to define Request models via the `apiGateway` settings:
416
+
417
+ ```yml
418
+ provider:
419
+ ...
420
+ apiGateway:
421
+ request:
422
+ schemas:
423
+ post-create-model:
424
+ name: PostCreateModel
425
+ schema: ${file(api_schema/post_add_schema.json)}
426
+ description: "A Model validation for adding posts"
427
+ ```
428
+
429
+ which are then used like:
430
+
431
+ ```yml
432
+ functions:
433
+ create:
434
+ handler: posts.create
435
+ events:
436
+ - http:
437
+ path: posts/create
438
+ method: post
439
+ request:
440
+ schemas:
441
+ application/json: post-create-model
442
+ documentation:
443
+ ...
444
+ ```
445
+
446
+ The generator will match to the model within the `apiGateway` settings model list. If you are using the `apiGateway` to define models, please do not re-use any names that you might define in the [`models`](#models) list.
447
+
448
+ You can also skip writing a `requestBody` and `requestModels` if you have defined a `request` property in your event.
449
+
450
+ If you're not using `apiGateway`, you can still make use of `request` by writing in the other styles that serverless accepts for Request Schema Validators:
451
+
452
+ ```yml
453
+ functions:
454
+ create:
455
+ handler: posts.create
456
+ events:
457
+ - http:
458
+ path: posts/create
459
+ method: post
460
+ request:
461
+ schemas:
462
+ application/json:
463
+ schema: ${file(create_request.json)}
464
+ name: PostCreateModel
465
+ description: 'Validation model for Creating Posts'
466
+
467
+ ```
468
+
469
+ or
470
+
471
+ ```yml
472
+ functions:
473
+ create:
474
+ handler: posts.create
475
+ events:
476
+ - http:
477
+ path: posts/create
478
+ method: post
479
+ request:
480
+ schemas:
481
+ application/json: ${file(create_request.json)}
482
+ ```
412
483
 
413
484
  #### Functions
414
485
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serverless-openapi-documenter",
3
- "version": "0.0.63",
3
+ "version": "0.0.65",
4
4
  "description": "Generate OpenAPI v3 documentation and Postman Collections from your Serverless Config",
5
5
  "main": "index.js",
6
6
  "keywords": [
@@ -38,7 +38,7 @@
38
38
  "json-schema-for-openapi": "^0.3.2",
39
39
  "lodash.isequal": "^4.5.0",
40
40
  "oas-validator": "^5.0.8",
41
- "openapi-to-postmanv2": "^4.13.0",
41
+ "openapi-to-postmanv2": "^4.14.0",
42
42
  "swagger2openapi": "^7.0.8",
43
43
  "uuid": "^9.0.0"
44
44
  },
@@ -341,11 +341,31 @@ class DefinitionGenerator {
341
341
  if (Object.keys(documentation).includes('deprecated'))
342
342
  obj.deprecated = documentation.deprecated
343
343
 
344
- if (documentation.requestBody)
345
- obj.requestBody = await this.createRequestBody(documentation)
344
+ if (documentation.requestBody || this.currentEvent?.request?.schemas) {
345
+ const requestModel = {}
346
+ if (documentation.requestBody) {
347
+ Object.assign(
348
+ requestModel,
349
+ {
350
+ description: documentation.requestBody.description,
351
+ models: documentation.requestModels
352
+ }
353
+ )
354
+ } else {
355
+ Object.assign(
356
+ requestModel,
357
+ {
358
+ description: '',
359
+ models: this.currentEvent?.request?.schemas
360
+ }
361
+ )
362
+ }
363
+
364
+ obj.requestBody = await this.createRequestBody(requestModel)
346
365
  .catch(err => {
347
366
  throw err
348
367
  })
368
+ }
349
369
 
350
370
  if (documentation.methodResponses)
351
371
  obj.responses = await this.createResponses(documentation)
@@ -488,13 +508,13 @@ class DefinitionGenerator {
488
508
  return obj
489
509
  }
490
510
 
491
- async createRequestBody(documentation) {
511
+ async createRequestBody(requestBodyDetails) {
492
512
  const obj = {
493
- description: documentation.requestBody.description,
494
- required: documentation.requestBody.required || false,
513
+ description: requestBodyDetails.description,
514
+ required: false
495
515
  }
496
516
 
497
- obj.content = await this.createMediaTypeObject(documentation.requestModels, 'requestBody')
517
+ obj.content = await this.createMediaTypeObject(requestBodyDetails.models)
498
518
  .catch(err => {
499
519
  throw err
500
520
  })
@@ -548,6 +568,25 @@ class DefinitionGenerator {
548
568
  Object.assign(mediaTypeObj, { [contentKey]: obj })
549
569
  }
550
570
  }
571
+
572
+ if (Object.keys(mediaTypeObj).length === 0) {
573
+ for (const contentKey of Object.keys(models)) {
574
+ const obj = {}
575
+ const schema = (models[contentKey]?.schema) ? models[contentKey].schema : models[contentKey]
576
+ const name = (models[contentKey]?.name) ? models[contentKey].name : uuid()
577
+ const schemaRef = await this.schemaHandler.createSchema(name, schema)
578
+ .catch(err => {
579
+ throw err
580
+ })
581
+
582
+ obj.schema = {
583
+ $ref: schemaRef
584
+ }
585
+
586
+ Object.assign(mediaTypeObj, { [contentKey]: obj })
587
+ }
588
+ }
589
+
551
590
  return mediaTypeObj
552
591
  }
553
592
 
@@ -9,6 +9,7 @@ const { v4: uuid } = require('uuid')
9
9
 
10
10
  class SchemaHandler {
11
11
  constructor(serverless, openAPI) {
12
+ this.apiGatewayModels = serverless.service?.provider?.apiGateway?.request?.schemas || {}
12
13
  this.documentation = serverless.service.custom.documentation
13
14
  this.openAPI = openAPI
14
15
 
@@ -42,7 +43,12 @@ class SchemaHandler {
42
43
  const standardisedModels = this.documentation?.models?.map(standardModel) || []
43
44
  const standardisedModelsList = this.documentation?.modelsList?.map(standardModel) || []
44
45
 
45
- this.models = standardisedModels.length ? standardisedModels.concat(standardisedModelsList) : standardisedModelsList
46
+ const standardisedGatewayModels = Object.keys(this.apiGatewayModels).flatMap(key => {
47
+ const gatewayModel = this.apiGatewayModels[key]
48
+ return standardModel(gatewayModel)
49
+ }) || []
50
+
51
+ this.models = standardisedModels.concat(standardisedModelsList, standardisedGatewayModels)
46
52
  }
47
53
 
48
54
  async addModelsToOpenAPI() {