diff --git a/.gitignore b/.gitignore index 21cc8698a..a21abc31a 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,7 @@ node_modules/ .yarn-integrity .env .idea +*.iml .DS_Store *test-plugins graphql.config.json diff --git a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/dereferenced-output.json b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/dereferenced-output.json index fda3f6ccb..868fc12dd 100644 --- a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/dereferenced-output.json +++ b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/dereferenced-output.json @@ -21,7 +21,7 @@ "parentId": "__WORKSPACE_ID__" }, { - "_id": "env___BASE_ENVIRONMENT_ID___sub", + "_id": "env___BASE_ENVIRONMENT_ID___sub__33ebaa67", "_type": "environment", "data": { "apiKey": "apiKey", @@ -31,7 +31,7 @@ "oauth2RedirectUrl": "http://localhost/", "scheme": "http" }, - "name": "OpenAPI env", + "name": "OpenAPI env petstore.swagger.io", "parentId": "__BASE_ENVIRONMENT_ID__" }, { diff --git a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/dereferenced-with-tags-output.json b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/dereferenced-with-tags-output.json index fa779ab59..776cf788f 100644 --- a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/dereferenced-with-tags-output.json +++ b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/dereferenced-with-tags-output.json @@ -21,7 +21,7 @@ "parentId": "__WORKSPACE_ID__" }, { - "_id": "env___BASE_ENVIRONMENT_ID___sub", + "_id": "env___BASE_ENVIRONMENT_ID___sub__33ebaa67", "_type": "environment", "data": { "apiKey": "apiKey", @@ -31,7 +31,7 @@ "oauth2RedirectUrl": "http://localhost/", "scheme": "http" }, - "name": "OpenAPI env", + "name": "OpenAPI env petstore.swagger.io", "parentId": "__BASE_ENVIRONMENT_ID__" }, { diff --git a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/endpoint-security-output.json b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/endpoint-security-output.json index a7d6b0136..d77a866d8 100644 --- a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/endpoint-security-output.json +++ b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/endpoint-security-output.json @@ -21,7 +21,7 @@ "parentId": "__WORKSPACE_ID__" }, { - "_id": "env___BASE_ENVIRONMENT_ID___sub", + "_id": "env___BASE_ENVIRONMENT_ID___sub__64c0ce7d", "_type": "environment", "data": { "anotherCookieName": "anotherCookieName", @@ -42,7 +42,7 @@ "xApiKey": "xApiKey", "xAppVersion": "xAppVersion" }, - "name": "OpenAPI env", + "name": "OpenAPI env api.server.test", "parentId": "__BASE_ENVIRONMENT_ID__" }, { diff --git a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/example-with-server-variables-output.json b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/example-with-server-variables-output.json index 3ab2f3384..14079b94d 100644 --- a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/example-with-server-variables-output.json +++ b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/example-with-server-variables-output.json @@ -21,14 +21,14 @@ "parentId": "__WORKSPACE_ID__" }, { - "_id": "env___BASE_ENVIRONMENT_ID___sub", + "_id": "env___BASE_ENVIRONMENT_ID___sub__b0168b4d", "_type": "environment", "data": { "base_path": "/filemanagement", "host": "localhost:8080", "scheme": "https" }, - "name": "OpenAPI env", + "name": "OpenAPI env localhost:8080", "parentId": "__BASE_ENVIRONMENT_ID__" }, { diff --git a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/example-without-servers-output.json b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/example-without-servers-output.json index 193f170d0..e6893901d 100644 --- a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/example-without-servers-output.json +++ b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/example-without-servers-output.json @@ -21,14 +21,14 @@ "parentId": "__WORKSPACE_ID__" }, { - "_id": "env___BASE_ENVIRONMENT_ID___sub", + "_id": "env___BASE_ENVIRONMENT_ID___sub__9c17e047", "_type": "environment", "data": { "base_path": "", "host": "example.com", "scheme": "http" }, - "name": "OpenAPI env", + "name": "OpenAPI env example.com", "parentId": "__BASE_ENVIRONMENT_ID__" }, { diff --git a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/global-security-output.json b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/global-security-output.json index caf5ca8ce..6849b2d04 100644 --- a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/global-security-output.json +++ b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/global-security-output.json @@ -22,7 +22,7 @@ }, { "parentId": "__BASE_ENVIRONMENT_ID__", - "name": "OpenAPI env", + "name": "OpenAPI env api.server.test", "data": { "scheme": "https", "apiKeyHere": "apiKeyHere", @@ -35,7 +35,7 @@ "xApiKey": "xApiKey" }, "_type": "environment", - "_id": "env___BASE_ENVIRONMENT_ID___sub" + "_id": "env___BASE_ENVIRONMENT_ID___sub__64c0ce7d" }, { "parentId": "__WORKSPACE_ID__", diff --git a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/multiple-api-keys-output.json b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/multiple-api-keys-output.json index b5ba10c3d..a10973603 100644 --- a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/multiple-api-keys-output.json +++ b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/multiple-api-keys-output.json @@ -21,7 +21,7 @@ "parentId": "__WORKSPACE_ID__" }, { - "_id": "env___BASE_ENVIRONMENT_ID___sub", + "_id": "env___BASE_ENVIRONMENT_ID___sub__3d8ac28f", "_type": "environment", "data": { "base_path": "/v2", @@ -31,7 +31,7 @@ "xAppId": "xAppId", "xAppSecret": "xAppSecret" }, - "name": "OpenAPI env", + "name": "OpenAPI env petstore.swagger.io", "parentId": "__BASE_ENVIRONMENT_ID__" }, { diff --git a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/oauth2-output.json b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/oauth2-output.json index 8fdcdbd50..1ce6c82b3 100644 --- a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/oauth2-output.json +++ b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/oauth2-output.json @@ -21,7 +21,7 @@ "parentId": "__WORKSPACE_ID__" }, { - "_id": "env___BASE_ENVIRONMENT_ID___sub", + "_id": "env___BASE_ENVIRONMENT_ID___sub__64c0ce7d", "_type": "environment", "data": { "base_path": "/v1", @@ -31,7 +31,7 @@ "oauth2RedirectUrl": "http://localhost/", "scheme": "https" }, - "name": "OpenAPI env", + "name": "OpenAPI env api.server.test", "parentId": "__BASE_ENVIRONMENT_ID__" }, { diff --git a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/path-plugin-output.json b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/path-plugin-output.json index a7da81376..7461e30bb 100644 --- a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/path-plugin-output.json +++ b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/path-plugin-output.json @@ -21,14 +21,14 @@ "parentId": "__WORKSPACE_ID__" }, { - "_id": "env___BASE_ENVIRONMENT_ID___sub", + "_id": "env___BASE_ENVIRONMENT_ID___sub__64b4aec9", "_type": "environment", "data": { "base_path": "", "host": "api.insomnia.rest", "scheme": "https" }, - "name": "OpenAPI env", + "name": "OpenAPI env api.insomnia.rest", "parentId": "__BASE_ENVIRONMENT_ID__" }, { diff --git a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-input.json b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-input.json index b233fc653..c3d37d83c 100644 --- a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-input.json +++ b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-input.json @@ -20,6 +20,9 @@ "servers": [ { "url": "http://petstore.swagger.io/v2/" + }, + { + "url": "http://petstore-preprod.swagger.io/v2/" } ], "tags": [], diff --git a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-output.json b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-output.json index a79043b01..f29f67f5e 100644 --- a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-output.json +++ b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-output.json @@ -21,7 +21,7 @@ "parentId": "__WORKSPACE_ID__" }, { - "_id": "env___BASE_ENVIRONMENT_ID___sub", + "_id": "env___BASE_ENVIRONMENT_ID___sub__c88af77d", "_type": "environment", "data": { "apiKey": "apiKey", @@ -31,7 +31,21 @@ "oauth2RedirectUrl": "http://localhost/", "scheme": "http" }, - "name": "OpenAPI env", + "name": "OpenAPI env petstore.swagger.io", + "parentId": "__BASE_ENVIRONMENT_ID__" + }, + { + "_id": "env___BASE_ENVIRONMENT_ID___sub__33d9f12e", + "_type": "environment", + "data": { + "apiKey": "apiKey", + "base_path": "/v2", + "host": "petstore-preprod.swagger.io", + "oauth2ClientId": "clientId", + "oauth2RedirectUrl": "http://localhost/", + "scheme": "http" + }, + "name": "OpenAPI env petstore-preprod.swagger.io", "parentId": "__BASE_ENVIRONMENT_ID__" }, { diff --git a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-readonly-output.json b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-readonly-output.json index 2158b5529..0b59c6c09 100644 --- a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-readonly-output.json +++ b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-readonly-output.json @@ -21,14 +21,14 @@ "parentId": "__WORKSPACE_ID__" }, { - "_id": "env___BASE_ENVIRONMENT_ID___sub", + "_id": "env___BASE_ENVIRONMENT_ID___sub__5be46d1c", "_type": "environment", "data": { "base_path": "/v1", "host": "petstore.swagger.io", "scheme": "http" }, - "name": "OpenAPI env", + "name": "OpenAPI env petstore.swagger.io", "parentId": "__BASE_ENVIRONMENT_ID__" }, { diff --git a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-with-tags-output.json b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-with-tags-output.json index e13c3f478..0bdd7c42c 100644 --- a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-with-tags-output.json +++ b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-with-tags-output.json @@ -21,7 +21,7 @@ "parentId": "__WORKSPACE_ID__" }, { - "_id": "env___BASE_ENVIRONMENT_ID___sub", + "_id": "env___BASE_ENVIRONMENT_ID___sub__33ebaa67", "_type": "environment", "data": { "apiKey": "apiKey", @@ -31,7 +31,7 @@ "oauth2RedirectUrl": "http://localhost/", "scheme": "http" }, - "name": "OpenAPI env", + "name": "OpenAPI env petstore.swagger.io", "parentId": "__BASE_ENVIRONMENT_ID__" }, { diff --git a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-yml-output.json b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-yml-output.json index 5ed42cbb7..5c51187ab 100644 --- a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-yml-output.json +++ b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-yml-output.json @@ -21,14 +21,14 @@ "parentId": "__WORKSPACE_ID__" }, { - "_id": "env___BASE_ENVIRONMENT_ID___sub", + "_id": "env___BASE_ENVIRONMENT_ID___sub__5be46d1c", "_type": "environment", "data": { "base_path": "/v1", "host": "petstore.swagger.io", "scheme": "http" }, - "name": "OpenAPI env", + "name": "OpenAPI env petstore.swagger.io", "parentId": "__BASE_ENVIRONMENT_ID__" }, { diff --git a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-yml-with-tags-output.json b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-yml-with-tags-output.json index 2f45f909a..0bbaa6e65 100644 --- a/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-yml-with-tags-output.json +++ b/packages/insomnia/src/utils/importers/importers/fixtures/openapi3/petstore-yml-with-tags-output.json @@ -21,14 +21,14 @@ "parentId": "__WORKSPACE_ID__" }, { - "_id": "env___BASE_ENVIRONMENT_ID___sub", + "_id": "env___BASE_ENVIRONMENT_ID___sub__5be46d1c", "_type": "environment", "data": { "base_path": "/v1", "host": "petstore.swagger.io", "scheme": "http" }, - "name": "OpenAPI env", + "name": "OpenAPI env petstore.swagger.io", "parentId": "__BASE_ENVIRONMENT_ID__" }, { diff --git a/packages/insomnia/src/utils/importers/importers/openapi-3.ts b/packages/insomnia/src/utils/importers/importers/openapi-3.ts index 25cf463ca..f101f3b34 100644 --- a/packages/insomnia/src/utils/importers/importers/openapi-3.ts +++ b/packages/insomnia/src/utils/importers/importers/openapi-3.ts @@ -64,17 +64,14 @@ let requestCounts: Record = {}; * * @returns the resolved server URL */ -const getDefaultServerUrl = (api: OpenAPIV3.Document) => { +const getServerUrl = (server: OpenAPIV3.ServerObject) => { const exampleServer = 'http://example.com/'; - const servers = api.servers || []; - const firstServer = servers[0]; - const foundServer = firstServer && firstServer.url; - if (!foundServer) { + if (!(server && server.url)) { return urlParse(exampleServer); } - const url = resolveVariables(firstServer); + const url = resolveVariables(server); return urlParse(url); }; @@ -128,6 +125,57 @@ const isSpecExtension = (property: string): property is SpecExtension => { return property.indexOf('x-') === 0; }; +/** + * Create env definitions based on openapi document. + */ +const parseEnvs = (baseEnv: ImportRequest, document?: OpenAPIV3.Document | null) => { + if (!document) { + return []; + } + + let servers: OpenAPIV3.ServerObject[] | undefined; + + if (!document.servers) { + servers = [{ url: 'http://example.com/' }]; + } else { + servers = document.servers; + } + + const securityVariables = getSecurityEnvVariables( + document.components?.securitySchemes as unknown as OpenAPIV3.SecuritySchemeObject, + ); + + return servers + .map(server => { + const currentServerUrl = getServerUrl(server); + const protocol = currentServerUrl.protocol || ''; + + // Base path is pulled out of the URL, and the trailing slash is removed + const basePath = (currentServerUrl.pathname || '').replace(/\/$/, ''); + + const hash = crypto + .createHash('sha1') + .update(server.url) + .digest('hex') + .slice(0, 8); + const openapiEnv: ImportRequest = { + _type: 'environment', + _id: `env___BASE_ENVIRONMENT_ID___sub__${hash}`, + parentId: baseEnv._id, + name: `OpenAPI env ${currentServerUrl.host}`, + data: { + // note: `URL.protocol` returns with trailing `:` (i.e. "https:") + scheme: protocol.replace(/:$/, '') || ['http'], + base_path: basePath, + host: currentServerUrl.host || '', + ...securityVariables, + }, + }; + + return openapiEnv; + }) || []; +}; + /** * Create request definitions based on openapi document. */ @@ -767,35 +815,14 @@ export const convert: Converter = async rawData => { base_url: '{{ scheme }}://{{ host }}{{ base_path }}', }, }; - const defaultServerUrl = getDefaultServerUrl(apiDocument); - const securityVariables = getSecurityEnvVariables( - apiDocument.components?.securitySchemes as unknown as OpenAPIV3.SecuritySchemeObject, - ); - const protocol = defaultServerUrl.protocol || ''; - - // Base path is pulled out of the URL, and the trailing slash is removed - const basePath = (defaultServerUrl.pathname || '').replace(/\/$/, ''); - - const openapiEnv: ImportRequest = { - _type: 'environment', - _id: 'env___BASE_ENVIRONMENT_ID___sub', - parentId: baseEnv._id, - name: 'OpenAPI env', - data: { - // note: `URL.protocol` returns with trailing `:` (i.e. "https:") - scheme: protocol.replace(/:$/, '') || ['http'], - base_path: basePath, - host: defaultServerUrl.host || '', - ...securityVariables, - }, - }; + const openapiEnvs = parseEnvs(baseEnv, apiDocument); const endpoints = parseEndpoints(apiDocument); return [ workspace, baseEnv, - openapiEnv, + ...openapiEnvs, ...endpoints, ]; };