Skip to content

optimize /runOpenApi to allow pre-dereferencing OpenAPI Spec #750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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