Skip to content

Commit

Permalink
update openApiParser
Browse files Browse the repository at this point in the history
  • Loading branch information
William Murphy committed May 31, 2023
1 parent ac01fed commit 9cfdb05
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 19 deletions.
92 changes: 74 additions & 18 deletions main_process/openapiParser.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,101 @@
const YAML = require('yamljs');

/////////////
const Ajv = require('ajv');
const openapiSchema = require('openapi-schema-validation');

const ajv = new Ajv();
const validateOpenAPI = ajv.compile(openapiSchema);


const errors = validateOpenAPI.errors;
if (errors) {
const errorMessages = errors.map((error) => `Validation Error: ${error.dataPath} ${error.message}`);
throw new Error(`Invalid OpenAPI document.\n${errorMessages.join('\n')}`);
}
/////////////

// TODO The security keys need to be implmented into the OpenApi request

const openapiParserFunc = (input) => {

// Input validation
if (typeof input !== 'string') {
throw new TypeError('Input must be a string.');
}

if (input === undefined || input === null) {
throw new ReferenceError('OpenAPI Document not found.');
}
// Parse the input into JSON or YAML
let doc;
try {
//try json parse
doc = JSON.parse(input);
} catch (SyntaxError) {
doc = YAML.parse(input);
} catch (jsonError) {
// try to parse as yaml
try{
doc = YAML.parse(input)
} catch (yamlError) {
throw new Error('Invalid JSON, or YAML format: ' + yamlError.message)
}
}

// Schema validation
const isValidOpenAPI = validateOpenAPI(doc);
if (!isValidOpenAPI) {
throw new Error('Invalid OpenAPI document. Schema validation failed.');
}

const { info, servers, tags, paths, components } = doc;

const { info = {}, servers = [], tags = [], paths = {}, components = {} } = doc;

info.openapi = doc.openapi;

let serverUrls
if (servers) {
serverUrls = [...servers.map((server) => server.url)];
} else {
serverUrls = []
}
let id = 0;
const serverUrls = servers.map((server) => server.url);

const openapiReqArray = [];
let id = 0;

Object.entries(paths).forEach(([endpoint, pathObj]) => {
Object.entries(pathObj).forEach(([method, operationObj]) => {
id += 1;

const {
summary,
description,
operationId,
tags,
parameters, // security
summary = '',
description = '',
operationId = '',
tags = [],
parameters = [],
security = [],
responses = {},
externalDocs = {},
version = '',
} = operationObj;

const securitySchemes = components.securitySchemes || {};
const responseExamples = {}; // Extract response examples from responses object if available

// const request = {
// id,
// // enabled: true,
// reqTags: tags,
// summary,
// description,
// operationId,
// method: method.toUpperCase(),
// reqServers: [],
// endpoint,
// parameters,
// body: new Map(),
// headers: {},
// cookies: {},
// // params: {},
// // queries: {},
// urls: [],
// };
const request = {
id,
// enabled: true,
reqTags: tags,
summary,
description,
Expand All @@ -53,8 +107,10 @@ const openapiParserFunc = (input) => {
body: new Map(),
headers: {},
cookies: {},
// params: {},
// queries: {},
securitySchemes,
responseExamples,
externalDocs,
version,
urls: [],
};
openapiReqArray.push(request);
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,12 @@ export type NewRequestFields = {
openapiReqObj: Record<string, $TSFixMe>;
};

export interface ReqResRequest {
// Currently, the body for WebRTC connection is an object
// and typescript does not support union between string and object very well
// Ideally we should move the WebRTC body information to a new key value
// to fully resolve the issue

export interface ReqResRequest {
body: string;
bodyType: string;
bodyVariables: string;
Expand Down

0 comments on commit 9cfdb05

Please sign in to comment.