11import * as path from 'path' ;
22import * as Serverless from 'serverless' ;
33import { DefinitionGenerator } from '../DefinitionGenerator' ;
4+ import { merge } from '../utils' ;
45
56class ServerlessInterface extends Serverless {
67 public service : any = { } ;
@@ -11,10 +12,12 @@ class ServerlessInterface extends Serverless {
1112}
1213
1314describe ( 'OpenAPI Documentation Generator' , ( ) => {
14- it ( 'Generates OpenAPI document' , async ( ) => {
15+ let sls : ServerlessInterface ;
16+
17+ beforeEach ( async ( ) => {
1518 const servicePath = path . join ( __dirname , '../../test/project' ) ;
1619 const serverlessYamlPath = path . join ( servicePath , './serverless.yml' ) ;
17- const sls : ServerlessInterface = new Serverless ( ) ;
20+ sls = new Serverless ( ) ;
1821
1922 sls . config . update ( {
2023 servicePath,
@@ -26,12 +29,67 @@ describe('OpenAPI Documentation Generator', () => {
2629 await sls . service . load ( config ) ;
2730 await sls . variables . populateService ( ) ;
2831
29- if ( 'documentation' in sls . service . custom ) {
30- const docGen = new DefinitionGenerator ( sls . service . custom . documentation ) ;
31-
32- expect ( docGen ) . not . toBeNull ( ) ;
33- } else {
32+ if ( ! ( 'documentation' in sls . service . custom ) ) {
3433 throw new Error ( 'Cannot find "documentation" in custom section of "serverless.yml"' ) ;
3534 }
3635 } ) ;
36+
37+ it ( 'Generates OpenAPI document' , async ( ) => {
38+ const docGen = new DefinitionGenerator ( sls . service . custom . documentation ) ;
39+ expect ( docGen ) . not . toBeNull ( ) ;
40+ } ) ;
41+
42+ it ( 'adds paths to OpenAPI output from function configuration' , async ( ) => {
43+ const docGen = new DefinitionGenerator ( sls . service . custom . documentation ) ;
44+
45+ // implementation copied from ServerlessOpenApiDocumentation.ts
46+ docGen . parse ( ) ;
47+
48+ const funcConfigs = sls . service . getAllFunctions ( ) . map ( ( functionName ) => {
49+ const func = sls . service . getFunction ( functionName ) ;
50+ return merge ( { _functionName : functionName } , func ) ;
51+ } ) ;
52+
53+ docGen . readFunctions ( funcConfigs ) ;
54+
55+ // get the parameters from the `/create POST' endpoint
56+ const actual = docGen . definition . paths [ '/create' ] . post . parameters ;
57+ const expected = [
58+ {
59+ description : 'The username for a user to create' ,
60+ in : 'path' ,
61+ name : 'username' ,
62+ required : true ,
63+ schema : {
64+ pattern : '^[-a-z0-9_]+$' ,
65+ type : 'string' ,
66+ } ,
67+ } ,
68+ {
69+ allowEmptyValue : false ,
70+ description : `The user's Membership Type` ,
71+ in : 'query' ,
72+ name : 'membershipType' ,
73+ required : false ,
74+ schema : {
75+ enum : [
76+ 'premium' ,
77+ 'standard' ,
78+ ] ,
79+ type : 'string' ,
80+ } ,
81+ } ,
82+ {
83+ description : 'A Session ID variable' ,
84+ in : 'cookie' ,
85+ name : 'SessionId' ,
86+ required : false ,
87+ schema : {
88+ type : 'string' ,
89+ } ,
90+ } ,
91+ ] ;
92+
93+ expect ( actual ) . toEqual ( expected ) ;
94+ } ) ;
3795} ) ;
0 commit comments