From f5b130024ee5dca2ab13351cf1dad5fbe947e569 Mon Sep 17 00:00:00 2001 From: Anwarul Islam Date: Mon, 18 Sep 2023 15:12:04 +0600 Subject: [PATCH] fix: missing baseurl on import openapi (#3323) * fix: missing baseurl on import openapi * fix: url parser for openapi v3 * chore: revert to baseURL for cases where doc servers is present but url is null --------- Co-authored-by: Andrew Bastin --- .../src/helpers/import-export/import/index.ts | 2 +- .../helpers/import-export/import/openapi.ts | 30 ++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/packages/hoppscotch-common/src/helpers/import-export/import/index.ts b/packages/hoppscotch-common/src/helpers/import-export/import/index.ts index 6477b293b..28733b23b 100644 --- a/packages/hoppscotch-common/src/helpers/import-export/import/index.ts +++ b/packages/hoppscotch-common/src/helpers/import-export/import/index.ts @@ -58,7 +58,7 @@ type HoppImporterDefinition = { export const defineImporter = (input: { id: string name: string - icon: Component + icon: object | Component importer: HoppImporter applicableTo: HoppImporterApplicableTo steps: StepType diff --git a/packages/hoppscotch-common/src/helpers/import-export/import/openapi.ts b/packages/hoppscotch-common/src/helpers/import-export/import/openapi.ts index 0b7e436ff..d0a60cf18 100644 --- a/packages/hoppscotch-common/src/helpers/import-export/import/openapi.ts +++ b/packages/hoppscotch-common/src/helpers/import-export/import/openapi.ts @@ -514,6 +514,32 @@ const parseOpenAPIAuth = ( ? parseOpenAPIV3Auth(doc as OpenAPIV3.Document | OpenAPIV31.Document, op) : parseOpenAPIV2Auth(doc as OpenAPIV2.Document, op) +const parseOpenAPIUrl = ( + doc: OpenAPI.Document | OpenAPIV2.Document | OpenAPIV3.Document +): string => { + /** + * OpenAPI V2 has version as a string in the document's swagger property. + * And host and basePath are in the document's host and basePath properties. + * Relevant v2 reference: https://swagger.io/specification/v2/#:~:text=to%20be%20obscured.-,Schema,-Swagger%20Object + **/ + + if (objectHasProperty(doc, "swagger")) { + return `${doc.host}${doc.basePath}` + } + + /** + * OpenAPI V3 has version as a string in the document's openapi property. + * And host and basePath are in the document's servers property. + * Relevant v3 reference: https://swagger.io/specification/#server-object + **/ + if (objectHasProperty(doc, "servers")) { + return doc.servers?.[0].url ?? "<>" + } + + // If the document is neither v2 nor v3 then return a env variable as placeholder + return "<>" +} + const convertPathToHoppReqs = ( doc: OpenAPI.Document, pathName: string, @@ -535,7 +561,9 @@ const convertPathToHoppReqs = ( makeRESTRequest({ name: info.operationId ?? info.summary ?? "Untitled Request", method: method.toUpperCase(), - endpoint: `<>${replaceOpenApiPathTemplating(pathName)}`, // TODO: Make this proper + endpoint: `${parseOpenAPIUrl(doc)}${replaceOpenApiPathTemplating( + pathName + )}`, // We don't need to worry about reference types as the Dereferencing pass should remove them params: parseOpenAPIParams(