1
1
const YAML = require ( 'yamljs' ) ;
2
2
3
+ /////////////
4
+ const Ajv = require ( 'ajv' ) ;
5
+ const openapiSchema = require ( 'openapi-schema-validation' ) ;
6
+
7
+ const ajv = new Ajv ( ) ;
8
+ const validateOpenAPI = ajv . compile ( openapiSchema ) ;
9
+
10
+
11
+ const errors = validateOpenAPI . errors ;
12
+ if ( errors ) {
13
+ const errorMessages = errors . map ( ( error ) => `Validation Error: ${ error . dataPath } ${ error . message } ` ) ;
14
+ throw new Error ( `Invalid OpenAPI document.\n${ errorMessages . join ( '\n' ) } ` ) ;
15
+ }
16
+ /////////////
17
+
3
18
// TODO The security keys need to be implmented into the OpenApi request
4
19
5
20
const openapiParserFunc = ( input ) => {
6
21
22
+ // Input validation
23
+ if ( typeof input !== 'string' ) {
24
+ throw new TypeError ( 'Input must be a string.' ) ;
25
+ }
26
+
7
27
if ( input === undefined || input === null ) {
8
28
throw new ReferenceError ( 'OpenAPI Document not found.' ) ;
9
29
}
10
30
// Parse the input into JSON or YAML
11
31
let doc ;
12
32
try {
33
+ //try json parse
13
34
doc = JSON . parse ( input ) ;
14
- } catch ( SyntaxError ) {
15
- doc = YAML . parse ( input ) ;
35
+ } catch ( jsonError ) {
36
+ // try to parse as yaml
37
+ try {
38
+ doc = YAML . parse ( input )
39
+ } catch ( yamlError ) {
40
+ throw new Error ( 'Invalid JSON, or YAML format: ' + yamlError . message )
41
+ }
42
+ }
43
+
44
+ // Schema validation
45
+ const isValidOpenAPI = validateOpenAPI ( doc ) ;
46
+ if ( ! isValidOpenAPI ) {
47
+ throw new Error ( 'Invalid OpenAPI document. Schema validation failed.' ) ;
16
48
}
17
49
18
- const { info, servers, tags, paths, components } = doc ;
50
+
51
+ const { info = { } , servers = [ ] , tags = [ ] , paths = { } , components = { } } = doc ;
19
52
20
53
info . openapi = doc . openapi ;
21
54
22
- let serverUrls
23
- if ( servers ) {
24
- serverUrls = [ ...servers . map ( ( server ) => server . url ) ] ;
25
- } else {
26
- serverUrls = [ ]
27
- }
28
- let id = 0 ;
55
+ const serverUrls = servers . map ( ( server ) => server . url ) ;
29
56
30
57
const openapiReqArray = [ ] ;
58
+ let id = 0 ;
59
+
31
60
Object . entries ( paths ) . forEach ( ( [ endpoint , pathObj ] ) => {
32
61
Object . entries ( pathObj ) . forEach ( ( [ method , operationObj ] ) => {
33
62
id += 1 ;
63
+
34
64
const {
35
- summary,
36
- description,
37
- operationId,
38
- tags,
39
- parameters, // security
65
+ summary = '' ,
66
+ description = '' ,
67
+ operationId = '' ,
68
+ tags = [ ] ,
69
+ parameters = [ ] ,
70
+ security = [ ] ,
71
+ responses = { } ,
72
+ externalDocs = { } ,
73
+ version = '' ,
40
74
} = operationObj ;
41
75
76
+ const securitySchemes = components . securitySchemes || { } ;
77
+ const responseExamples = { } ; // Extract response examples from responses object if available
78
+
79
+ // const request = {
80
+ // id,
81
+ // // enabled: true,
82
+ // reqTags: tags,
83
+ // summary,
84
+ // description,
85
+ // operationId,
86
+ // method: method.toUpperCase(),
87
+ // reqServers: [],
88
+ // endpoint,
89
+ // parameters,
90
+ // body: new Map(),
91
+ // headers: {},
92
+ // cookies: {},
93
+ // // params: {},
94
+ // // queries: {},
95
+ // urls: [],
96
+ // };
42
97
const request = {
43
98
id,
44
- // enabled: true,
45
99
reqTags : tags ,
46
100
summary,
47
101
description,
@@ -53,8 +107,10 @@ const openapiParserFunc = (input) => {
53
107
body : new Map ( ) ,
54
108
headers : { } ,
55
109
cookies : { } ,
56
- // params: {},
57
- // queries: {},
110
+ securitySchemes,
111
+ responseExamples,
112
+ externalDocs,
113
+ version,
58
114
urls : [ ] ,
59
115
} ;
60
116
openapiReqArray . push ( request ) ;
0 commit comments