Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

fix(common): use operation title as fallback in OpenAPI import (#5950)

Gadam Jumayev744f4346989c
diff --git a/packages/hoppscotch-common/src/helpers/import-export/import/openapi/index.ts b/packages/hoppscotch-common/src/helpers/import-export/import/openapi/index.ts
index 47dd06ff9..7cccec7ec 100644
--- a/packages/hoppscotch-common/src/helpers/import-export/import/openapi/index.ts
+++ b/packages/hoppscotch-common/src/helpers/import-export/import/openapi/index.ts
@@ -120,6 +120,31 @@ type OpenAPIOperationType =
| OpenAPIV3.OperationObject
| OpenAPIV31.OperationObject
+// Resolve request name: operationId > summary > title > "Untitled Request"
+const getOpenAPIOperationName = (info: OpenAPIOperationType): string => {
+ const title =
+ objectHasProperty(info, "title") && typeof info.title === "string"
+ ? info.title
+ : undefined
+
+ const candidates: Array<string | undefined> = [
+ info.operationId,
+ info.summary,
+ title,
+ ]
+
+ for (const candidate of candidates) {
+ if (typeof candidate === "string") {
+ const trimmed = candidate.trim()
+ if (trimmed !== "") {
+ return trimmed
+ }
+ }
+ }
+
+ return "Untitled Request"
+}
+
// Removes the OpenAPI Path Templating to the Hoppscotch Templating (<< ? >>)
const replaceOpenApiPathTemplating = flow(
S.replace(/{/g, "<<"),
@@ -1039,7 +1064,7 @@ const convertPathToHoppReqs = (
}
} = {
request: makeRESTRequest({
- name: info.operationId ?? info.summary ?? "Untitled Request",
+ name: getOpenAPIOperationName(info),
description: info.description ?? null,
method: method.toUpperCase(),
endpoint,
@@ -1067,7 +1092,7 @@ const convertPathToHoppReqs = (
doc,
info,
makeHoppRESTResponseOriginalRequest({
- name: info.operationId ?? info.summary ?? "Untitled Request",
+ name: getOpenAPIOperationName(info),
auth: parseOpenAPIAuth(doc, info),
body: parseOpenAPIBody(doc, info),
endpoint,