Skip to content

Commit

Permalink
fix: missing baseurl on import openapi (#3323)
Browse files Browse the repository at this point in the history
* 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 <andrewbastin.k@gmail.com>
  • Loading branch information
anwarulislam and AndrewBastin committed Sep 18, 2023
1 parent bb5c333 commit f5b1300
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type HoppImporterDefinition<T, Y, E> = {
export const defineImporter = <ReturnType, StepType, Errors>(input: {
id: string
name: string
icon: Component
icon: object | Component
importer: HoppImporter<ReturnType, StepType, Errors>
applicableTo: HoppImporterApplicableTo
steps: StepType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? "<<baseUrl>>"
}

// If the document is neither v2 nor v3 then return a env variable as placeholder
return "<<baseUrl>>"
}

const convertPathToHoppReqs = (
doc: OpenAPI.Document,
pathName: string,
Expand All @@ -535,7 +561,9 @@ const convertPathToHoppReqs = (
makeRESTRequest({
name: info.operationId ?? info.summary ?? "Untitled Request",
method: method.toUpperCase(),
endpoint: `<<baseUrl>>${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(
Expand Down

0 comments on commit f5b1300

Please sign in to comment.