Skip to content

Commit ffa595a

Browse files
authored
Merge pull request #750 from snowe2010/optimize-runOpenApi
optimize /runOpenApi to allow pre-dereferencing OpenAPI Spec
2 parents 4f80cae + 275cf68 commit ffa595a

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

server/node-service/src/plugins/github/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { OpenAPIV3, OpenAPI } from "openapi-types";
55
import { ConfigToType, DataSourcePlugin } from "lowcoder-sdk/dataSource";
66
import { runOpenApi } from "../openApi";
77
import { parseOpenApi, ParseOpenApiOptions } from "../openApi/parse";
8+
import SwaggerParser from "@apidevtools/swagger-parser";
89

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

@@ -34,6 +35,7 @@ const parseOptions: ParseOpenApiOptions = {
3435
return _.upperFirst(operation.operationId || "");
3536
},
3637
};
38+
const deRefedSpec = SwaggerParser.dereference(spec);
3739

3840
type DataSourceConfigType = ConfigToType<typeof dataSourceConfig>;
3941

@@ -55,13 +57,13 @@ const gitHubPlugin: DataSourcePlugin<any, DataSourceConfigType> = {
5557
actions,
5658
};
5759
},
58-
run: function (actionData, dataSourceConfig): Promise<any> {
60+
run: async function (actionData, dataSourceConfig, ctx): Promise<any> {
5961
const runApiDsConfig = {
6062
url: "",
6163
serverURL: "https://api.github.com",
6264
dynamicParamsConfig: dataSourceConfig,
6365
};
64-
return runOpenApi(actionData, runApiDsConfig, spec as OpenAPIV3.Document);
66+
return runOpenApi(actionData, runApiDsConfig, spec as OpenAPIV3.Document, undefined, await deRefedSpec);
6567
},
6668
};
6769

server/node-service/src/plugins/openApi/index.ts

+28-17
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ import {
1212
isFile,
1313
} from "./util";
1414
import { badRequest } from "../../common/error";
15-
import { safeJsonParse } from "../../common/util";
1615
import { OpenAPI, OpenAPIV2, OpenAPIV3 } from "openapi-types";
1716
import _ from "lodash";
1817
import { fetch } from "../../common/fetch";
19-
import { RequestInit, Response } from "node-fetch";
18+
import { RequestInit } from "node-fetch";
2019

2120
const dataSourceConfig = {
2221
type: "dataSource",
@@ -50,30 +49,42 @@ interface ActionDataType {
5049
[key: string]: any;
5150
}
5251

52+
async function getDefinitions(
53+
spec: OpenAPI.Document | MultiOpenApiSpecItem[],
54+
openApiSpecDereferenced?: OpenAPI.Document,
55+
): Promise<{ def: OpenAPI.Document; id: string }[]> {
56+
if (openApiSpecDereferenced) {
57+
return [{
58+
def: openApiSpecDereferenced,
59+
id: "",
60+
}]
61+
} else {
62+
const specList = Array.isArray(spec) ? spec : [{ spec, id: "" }];
63+
return await Promise.all(
64+
specList.map(async ({id, spec}) => {
65+
const deRefedSpec = await SwaggerParser.dereference(spec);
66+
return {
67+
def: deRefedSpec,
68+
id,
69+
};
70+
})
71+
);
72+
}
73+
}
74+
5375
export async function runOpenApi(
5476
actionData: ActionDataType,
5577
dataSourceConfig: DataSourceDataType,
5678
spec: OpenAPI.Document | MultiOpenApiSpecItem[],
57-
defaultHeaders?: Record<string, string>
79+
defaultHeaders?: Record<string, string>,
80+
openApiSpecDereferenced?: OpenAPI.Document,
5881
) {
59-
const specList = Array.isArray(spec) ? spec : [{ spec, id: "" }];
60-
const definitions = await Promise.all(
61-
specList.map(async ({ id, spec }) => {
62-
const deRefedSpec = await SwaggerParser.dereference(spec);
63-
return {
64-
def: deRefedSpec,
65-
id,
66-
};
67-
})
68-
);
6982
const { actionName, ...otherActionData } = actionData;
7083
const { serverURL } = dataSourceConfig;
7184

72-
let definition: OpenAPI.Document | undefined;
73-
let operation;
74-
let realOperationId;
85+
let operation, realOperationId, definition: OpenAPI.Document | undefined;
7586

76-
for (const { id, def } of definitions) {
87+
for (const {id, def} of await getDefinitions(spec, openApiSpecDereferenced)) {
7788
const ret = findOperation(actionName, def, id);
7889
if (ret) {
7990
definition = def;

0 commit comments

Comments
 (0)