Skip to content

Commit

Permalink
Merge pull request #750 from snowe2010/optimize-runOpenApi
Browse files Browse the repository at this point in the history
optimize /runOpenApi to allow pre-dereferencing OpenAPI Spec
  • Loading branch information
FalkWolsky authored Mar 9, 2024
2 parents 4f80cae + 275cf68 commit ffa595a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 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
45 changes: 28 additions & 17 deletions server/node-service/src/plugins/openApi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import {
isFile,
} from "./util";
import { badRequest } from "../../common/error";
import { safeJsonParse } from "../../common/util";
import { OpenAPI, OpenAPIV2, OpenAPIV3 } from "openapi-types";
import _ from "lodash";
import { fetch } from "../../common/fetch";
import { RequestInit, Response } from "node-fetch";
import { RequestInit } from "node-fetch";

const dataSourceConfig = {
type: "dataSource",
Expand Down Expand Up @@ -50,30 +49,42 @@ interface ActionDataType {
[key: string]: any;
}

async function getDefinitions(
spec: OpenAPI.Document | MultiOpenApiSpecItem[],
openApiSpecDereferenced?: OpenAPI.Document,
): Promise<{ def: OpenAPI.Document; id: string }[]> {
if (openApiSpecDereferenced) {
return [{
def: openApiSpecDereferenced,
id: "",
}]
} else {
const specList = Array.isArray(spec) ? spec : [{ spec, id: "" }];
return await Promise.all(
specList.map(async ({id, spec}) => {
const deRefedSpec = await SwaggerParser.dereference(spec);
return {
def: deRefedSpec,
id,
};
})
);
}
}

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,
};
})
);
const { actionName, ...otherActionData } = actionData;
const { serverURL } = dataSourceConfig;

let definition: OpenAPI.Document | undefined;
let operation;
let realOperationId;
let operation, realOperationId, definition: OpenAPI.Document | undefined;

for (const { id, def } of definitions) {
for (const {id, def} of await getDefinitions(spec, openApiSpecDereferenced)) {
const ret = findOperation(actionName, def, id);
if (ret) {
definition = def;
Expand Down

0 comments on commit ffa595a

Please sign in to comment.