Skip to content

Commit

Permalink
Merge pull request #1001 from godspeedsystems/v2-dev
Browse files Browse the repository at this point in the history
V2 dev
  • Loading branch information
kushal023 committed Mar 7, 2024
2 parents c8cc522 + 49ce7a7 commit dd47cd4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
14 changes: 11 additions & 3 deletions src/godspeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ class Godspeed {
// initilize child logger
initializeChildLogger({});
// TODO: lot's of logging related steps
childLogger.debug('processing event ... %s', event.type);
childLogger.debug('processing event %s', event.type);
// TODO: Once the config loader is sorted, fetch the apiVersion from config


Expand All @@ -449,7 +449,7 @@ class Godspeed {

childLogger.error('Validation of event request failed %s. Will run validation error handler', JSON.stringify(validationError));

event.data = { event: event.data, validation_error: validationError };
event.data = { event: event.data, validationError };

// A workflow is always a series execution of its tasks. ie., a GSSeriesFunction
eventHandlerWorkflow = <GSFunction>(eventSpec.on_request_validation_error);
Expand Down Expand Up @@ -498,6 +498,13 @@ class Godspeed {
setAtPath(authzStatus, 'data.message', authzStatus.message || 'Access Forbidden');
}
return authzStatus;
} else {
//since authz was successful
//in case com.gs.return was used, the exitWithStatus would be there
//because com.gs.return sets exitWithStatus: true for every call
//Remove exitWithStatus otherwise it will be taken as error in
//interfaces.ts when the event handler will be called
delete ctx.exitWithStatus;
}
//Autorization is passed. Proceeding.
// childLogger.debug('Authorization passed at the event level');
Expand Down Expand Up @@ -541,7 +548,8 @@ class Godspeed {

childLogger.error('Validation of event response failed %s', JSON.stringify(validationError));

event.data = { event: event.data, validation_error: validationError };
// event.data = { event: event.data, validationError };
event.data.validation_error = validationError;

// A workflow is always a series execution of its tasks. ie., a GSSeriesFunction
return await (eventSpec.on_response_validation_error)(ctx);
Expand Down
40 changes: 30 additions & 10 deletions src/router/swagger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import def from "ajv/dist/vocabularies/discriminator";
import { PlainObject } from "../types";

// Define the type of eventSourceConfig
Expand All @@ -8,16 +9,16 @@ type EventSourceConfig = {
jwt?: PlainObject //V2 has all authentication configs in authn
}
docs?: {
info: any; // Adjust the type as needed
servers: any[]; // Adjust the type as needed
info: any; // Adjust the type as needed
servers: any[]; // Adjust the type as needed
};
};

export const generateSwaggerJSON = (events: PlainObject, definitions: PlainObject, eventSourceConfig: EventSourceConfig) => {

const finalSpecs: { [key: string]: any } = { openapi: "3.0.0", paths: {} };

const { port, docs} = eventSourceConfig;
const { port, docs } = eventSourceConfig;
const info = docs?.info;
const servers = docs?.servers;
const jwt = eventSourceConfig.authn?.jwt || eventSourceConfig.jwt;
Expand All @@ -36,7 +37,7 @@ export const generateSwaggerJSON = (events: PlainObject, definitions: PlainObjec
summary: eventSchema.summary,
description: eventSchema.description,
tags: eventSchema.tags,
operationId: eventSchema.operationId || eventSchema.id || eventSchema.summary?.replace(' ', '_') || `${method}_${apiEndPoint}`.replace(/\//g,'_'),
operationId: eventSchema.operationId || eventSchema.id || eventSchema.summary?.replace(' ', '_') || `${method}_${apiEndPoint}`.replace(/\//g, '_'),
requestBody: eventSchema.body,
parameters: eventSchema.params,
responses: eventSchema.responses,
Expand Down Expand Up @@ -66,11 +67,9 @@ export const generateSwaggerJSON = (events: PlainObject, definitions: PlainObjec
}

finalSpecs.info = info;
//finalSpecs.definitions = definitions;
finalSpecs.components = {
schemas: definitions
};
replaceStringInJSON(finalSpecs, "#/definitions/", "#/components/schemas/");

setDefinitions(finalSpecs, definitions);

if (jwt) {
finalSpecs.components.securitySchemes = {
bearerAuth: {
Expand All @@ -85,6 +84,27 @@ export const generateSwaggerJSON = (events: PlainObject, definitions: PlainObjec
return finalSpecs;
};

function setDefinitions(finalSpecs: PlainObject, definitions: PlainObject) {
definitions = JSON.parse(JSON.stringify(definitions));
//Flatten the definitions object to store as component schema as per swagger format
const removedKeys: string[] = [];
Object.keys(definitions).forEach((key) => {
if (!definitions[key]?.type) {
const innerObj = definitions[key];
delete definitions[key];
removedKeys.push(key);
definitions = { ...definitions, ...innerObj };
}
});
//finalSpecs.definitions = definitions;
finalSpecs.components = {
schemas: definitions
};
for (let key of removedKeys) {
replaceStringInJSON(finalSpecs, `#/definitions/${key}/`, "#/components/schemas/");
}
}

function replaceStringInJSON(jsonObj: PlainObject, stringToMatch: string, replacementString: string) {
if (jsonObj === null) {
return;
Expand All @@ -103,7 +123,7 @@ function replaceStringInJSON(jsonObj: PlainObject, stringToMatch: string, replac
jsonObj[key] = replaceStringInJSON(jsonObj[key], stringToMatch, replacementString);
}
}
} else if(typeof jsonObj === 'string' ) {
} else if (typeof jsonObj === 'string') {
// If jsonObj is a leaf string value and contains the string to match, replace it
if ((jsonObj as string).includes(stringToMatch)) {
return (jsonObj as string).replace(new RegExp(stringToMatch, 'g'), replacementString);
Expand Down

0 comments on commit dd47cd4

Please sign in to comment.