@@ -16,6 +16,127 @@ const replaceSingleToDoubleCurlyBraces = (str) => {
1616 return str ;
1717} ;
1818
19+ const generateExample = ( schema ) => {
20+ if ( ! schema ) return { } ;
21+
22+ if ( schema . enum ) {
23+ return schema . example || schema . enum [ 0 ] ;
24+ }
25+
26+ if ( schema . oneOf ) {
27+ return generateExample ( schema . oneOf [ 0 ] ) ;
28+ }
29+
30+ if ( schema . anyOf ) {
31+ return generateExample ( schema . anyOf [ 0 ] ) ;
32+ }
33+
34+ if ( schema . allOf ) {
35+ return generateAllOfExample ( schema . allOf ) ;
36+ }
37+
38+ switch ( schema . type ) {
39+ case 'object' :
40+ return generateObjectExample ( schema ) ;
41+ case 'array' :
42+ return generateArrayExample ( schema ) ;
43+ case 'string' :
44+ return generateStringExample ( schema ) ;
45+ case 'integer' :
46+ return generateIntegerExample ( schema ) ;
47+ case 'number' :
48+ return generateNumberExample ( schema ) ;
49+ case 'boolean' :
50+ return schema . example || true ;
51+ default :
52+ return schema . example || null ;
53+ }
54+ } ;
55+
56+ const generateAllOfExample = ( schemas ) => {
57+ const example = { } ;
58+ schemas . forEach ( ( subSchema ) => {
59+ const subExample = generateExample ( subSchema ) ;
60+ Object . assign ( example , subExample ) ;
61+ } ) ;
62+ return example ;
63+ } ;
64+
65+ const generateObjectExample = ( schema ) => {
66+ const example = { } ;
67+ const properties = schema . properties || { } ;
68+
69+ for ( const [ key , propertySchema ] of Object . entries ( properties ) ) {
70+ example [ key ] = generateExample ( propertySchema ) ;
71+ }
72+
73+ return example ;
74+ } ;
75+
76+ const generateArrayExample = ( schema ) => {
77+ const itemsSchema = schema . items || { } ;
78+ return [ generateExample ( itemsSchema ) ] ;
79+ } ;
80+
81+ const generateStringExample = ( schema ) => {
82+ let example = schema . example || 'string' ;
83+
84+ if ( schema . minLength || schema . maxLength ) {
85+ example = generateStringWithLengthConstraints ( example , schema . minLength , schema . maxLength ) ;
86+ }
87+
88+ switch ( schema . format ) {
89+ case 'date-time' :
90+ return schema . example || new Date ( ) . toISOString ( ) ;
91+ case 'date' :
92+ return schema . example || new Date ( ) . toISOString ( ) . split ( 'T' ) [ 0 ] ;
93+ case 'email' :
94+ return schema . example || 'example@example.com' ;
95+ case 'uuid' :
96+ return schema . example || '123e4567-e89b-12d3-a456-426614174000' ;
97+ case 'uri' :
98+ return schema . example || 'https://example.com' ;
99+ case 'hostname' :
100+ return schema . example || 'example.com' ;
101+ case 'ipv4' :
102+ return schema . example || '192.168.0.1' ;
103+ case 'ipv6' :
104+ return schema . example || '2001:0db8:85a3:0000:0000:8a2e:0370:7334' ;
105+ case 'byte' :
106+ return schema . example || btoa ( 'example' ) ;
107+ case 'binary' :
108+ return schema . example || 'binary data' ;
109+ case 'password' :
110+ return schema . example || 'password' ;
111+ default :
112+ return example ;
113+ }
114+ } ;
115+
116+ const generateStringWithLengthConstraints = ( str , minLength , maxLength ) => {
117+ if ( minLength ) {
118+ while ( str . length < minLength ) {
119+ str += 'a' ;
120+ }
121+ }
122+ if ( maxLength ) {
123+ str = str . substring ( 0 , maxLength ) ;
124+ }
125+ return str ;
126+ } ;
127+
128+ const generateIntegerExample = ( schema ) => {
129+ const min = schema . minimum || 0 ;
130+ const max = schema . maximum || min + 100 ;
131+ return schema . example || Math . floor ( Math . random ( ) * ( max - min + 1 ) ) + min ;
132+ } ;
133+
134+ const generateNumberExample = ( schema ) => {
135+ const min = schema . minimum || 0.0 ;
136+ const max = schema . maximum || min + 100.0 ;
137+ return schema . example || Math . random ( ) * ( max - min ) + min ;
138+ } ;
139+
19140const parseOpenAPISpec = ( collection ) => {
20141 let parsedNodes = [ ] ;
21142 try {
@@ -28,10 +149,9 @@ const parseOpenAPISpec = (collection) => {
28149 const tags = request [ 'tags' ] ;
29150 var url = replaceSingleToDoubleCurlyBraces ( computeUrl ( baseUrl , path ) ) ;
30151 var variables = { } ;
152+ var requestBody = { } ;
31153
32- // console.log(operationId)
33- // Get is easy, others are hard
34- if ( requestType . toUpperCase ( ) === 'GET' && request [ 'parameters' ] ) {
154+ if ( request [ 'parameters' ] ) {
35155 let firstQueryParam = true ;
36156 request [ 'parameters' ] . map ( ( value , _ ) => {
37157 // path parameters are included in url
@@ -49,9 +169,22 @@ const parseOpenAPISpec = (collection) => {
49169 }
50170
51171 if ( request [ 'requestBody' ] ) {
52- if ( request [ 'requestBody' ] [ 'application/json' ] ) {
53- // console.log('requestBody: ', request["requestBody"]["content"]["schema"])
54- // generate an example to be used in request body
172+ if ( request [ 'requestBody' ] [ 'content' ] [ 'application/json' ] ) {
173+ requestBody = {
174+ type : 'raw-json' ,
175+ body : JSON . stringify ( generateExample ( request [ 'requestBody' ] [ 'content' ] [ 'application/json' ] [ 'schema' ] ) ) ,
176+ } ;
177+ }
178+
179+ if ( request [ 'requestBody' ] [ 'content' ] [ 'multipart/form-data' ] ) {
180+ requestBody = {
181+ type : 'form-data' ,
182+ body : {
183+ key : '' ,
184+ value : '' ,
185+ name : '' ,
186+ } ,
187+ } ;
55188 }
56189 }
57190
@@ -61,8 +194,9 @@ const parseOpenAPISpec = (collection) => {
61194 operationId : operationId ,
62195 requestType : requestType . toUpperCase ( ) ,
63196 tags : tags ,
197+ requestBody,
64198 } ;
65- // console.log(finalNode);
199+
66200 parsedNodes . push ( finalNode ) ;
67201 } ) ;
68202 } ) ;
@@ -72,4 +206,7 @@ const parseOpenAPISpec = (collection) => {
72206 return parsedNodes ;
73207} ;
74208
75- module . exports = parseOpenAPISpec ;
209+ module . exports = {
210+ parseOpenAPISpec,
211+ generateExample,
212+ } ;
0 commit comments