Skip to content

Commit 9cfdb05

Browse files
author
William Murphy
committed
update openApiParser
1 parent ac01fed commit 9cfdb05

File tree

2 files changed

+76
-19
lines changed

2 files changed

+76
-19
lines changed

main_process/openapiParser.js

Lines changed: 74 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,101 @@
11
const YAML = require('yamljs');
22

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+
318
// TODO The security keys need to be implmented into the OpenApi request
419

520
const openapiParserFunc = (input) => {
621

22+
// Input validation
23+
if (typeof input !== 'string') {
24+
throw new TypeError('Input must be a string.');
25+
}
26+
727
if (input === undefined || input === null) {
828
throw new ReferenceError('OpenAPI Document not found.');
929
}
1030
// Parse the input into JSON or YAML
1131
let doc;
1232
try {
33+
//try json parse
1334
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.');
1648
}
1749

18-
const { info, servers, tags, paths, components } = doc;
50+
51+
const { info = {}, servers = [], tags = [], paths = {}, components = {} } = doc;
1952

2053
info.openapi = doc.openapi;
2154

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);
2956

3057
const openapiReqArray = [];
58+
let id = 0;
59+
3160
Object.entries(paths).forEach(([endpoint, pathObj]) => {
3261
Object.entries(pathObj).forEach(([method, operationObj]) => {
3362
id += 1;
63+
3464
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 = '',
4074
} = operationObj;
4175

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+
// };
4297
const request = {
4398
id,
44-
// enabled: true,
4599
reqTags: tags,
46100
summary,
47101
description,
@@ -53,8 +107,10 @@ const openapiParserFunc = (input) => {
53107
body: new Map(),
54108
headers: {},
55109
cookies: {},
56-
// params: {},
57-
// queries: {},
110+
securitySchemes,
111+
responseExamples,
112+
externalDocs,
113+
version,
58114
urls: [],
59115
};
60116
openapiReqArray.push(request);

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,12 @@ export type NewRequestFields = {
167167
openapiReqObj: Record<string, $TSFixMe>;
168168
};
169169

170-
export interface ReqResRequest {
171170
// Currently, the body for WebRTC connection is an object
172171
// and typescript does not support union between string and object very well
173172
// Ideally we should move the WebRTC body information to a new key value
174173
// to fully resolve the issue
174+
175+
export interface ReqResRequest {
175176
body: string;
176177
bodyType: string;
177178
bodyVariables: string;

0 commit comments

Comments
 (0)