Skip to content

Commit

Permalink
Optimize the runOpenApi plugin
Browse files Browse the repository at this point in the history
allow passing in an already dereferenced OpenAPI Document for plugins
that have large openapi specs like github. This takes the runtime for
Github from over 10s to ~1s on an M1 Max.

Adjust the github plugin to use this new capability by dereferencing on
plugin initialization, passing the derefed document to the runOpenApi
function.
  • Loading branch information
snowe2010 committed Mar 8, 2024
1 parent 4f80cae commit d1e6f92
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
6 changes: 4 additions & 2 deletions server/node-service/src/plugins/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { OpenAPIV3, OpenAPI } from "openapi-types";
import { ConfigToType, DataSourcePlugin } from "lowcoder-sdk/dataSource";
import { runOpenApi } from "../openApi";
import { parseOpenApi, ParseOpenApiOptions } from "../openApi/parse";
import SwaggerParser from "@apidevtools/swagger-parser";

const spec = readYaml(path.join(__dirname, "./github.spec.yaml"));

Expand Down Expand Up @@ -34,6 +35,7 @@ const parseOptions: ParseOpenApiOptions = {
return _.upperFirst(operation.operationId || "");
},
};
const deRefedSpec = SwaggerParser.dereference(spec);

type DataSourceConfigType = ConfigToType<typeof dataSourceConfig>;

Expand All @@ -55,13 +57,13 @@ const gitHubPlugin: DataSourcePlugin<any, DataSourceConfigType> = {
actions,
};
},
run: function (actionData, dataSourceConfig): Promise<any> {
run: async function (actionData, dataSourceConfig, ctx): Promise<any> {
const runApiDsConfig = {
url: "",
serverURL: "https://api.github.com",
dynamicParamsConfig: dataSourceConfig,
};
return runOpenApi(actionData, runApiDsConfig, spec as OpenAPIV3.Document);
return runOpenApi(actionData, runApiDsConfig, spec as OpenAPIV3.Document, undefined, await deRefedSpec);
},
};

Expand Down
30 changes: 20 additions & 10 deletions server/node-service/src/plugins/openApi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,28 @@ export async function runOpenApi(
actionData: ActionDataType,
dataSourceConfig: DataSourceDataType,
spec: OpenAPI.Document | MultiOpenApiSpecItem[],
defaultHeaders?: Record<string, string>
defaultHeaders?: Record<string, string>,
openApiSpecDereferenced?: OpenAPI.Document,
) {
const specList = Array.isArray(spec) ? spec : [{ spec, id: "" }];
const definitions = await Promise.all(
specList.map(async ({ id, spec }) => {
const deRefedSpec = await SwaggerParser.dereference(spec);
return {
def: deRefedSpec,
id,
};
})
);
let definitions;

if (!openApiSpecDereferenced) {
definitions = await Promise.all(
specList.map(async ({id, spec}) => {
const deRefedSpec = await SwaggerParser.dereference(spec);
return {
def: deRefedSpec,
id,
};
})
);
} else {
definitions = [{
def: openApiSpecDereferenced,
id: "",
}]
}
const { actionName, ...otherActionData } = actionData;
const { serverURL } = dataSourceConfig;

Expand Down

0 comments on commit d1e6f92

Please sign in to comment.