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 47dd06ff..7cccec7e 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 = [ + 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,