From 0b0c5baa0e55c847583f679becc5dd8c422a6855 Mon Sep 17 00:00:00 2001 From: Opender Singh Date: Fri, 27 Mar 2020 05:56:11 +1300 Subject: [PATCH] OpenAPI v3 import robustness (#2007) * Make openapi3 import more robust by checking if the server tag exists before consuming it * Fix formating --- .../example-without-servers-input.yaml | 162 ++++++++++++++++++ .../example-without-servers-output.json | 59 +++++++ .../src/importers/openapi3.js | 5 +- 3 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 packages/insomnia-importers/src/__tests__/fixtures/openapi3/example-without-servers-input.yaml create mode 100644 packages/insomnia-importers/src/__tests__/fixtures/openapi3/example-without-servers-output.json diff --git a/packages/insomnia-importers/src/__tests__/fixtures/openapi3/example-without-servers-input.yaml b/packages/insomnia-importers/src/__tests__/fixtures/openapi3/example-without-servers-input.yaml new file mode 100644 index 000000000..92f3d64c8 --- /dev/null +++ b/packages/insomnia-importers/src/__tests__/fixtures/openapi3/example-without-servers-input.yaml @@ -0,0 +1,162 @@ +openapi: '3.0.0' +info: + title: Simple API overview + version: 2.0.0 +paths: + /: + get: + operationId: listVersionsv2 + summary: List API versions + responses: + '200': + description: |- + 200 response + content: + application/json: + examples: + foo: + value: | + { + "versions": [ + { + "status": "CURRENT", + "updated": "2011-01-21T11:33:21Z", + "id": "v2.0", + "links": [ + { + "href": "http://127.0.0.1:8774/v2/", + "rel": "self" + }] + }, + { + "status": "EXPERIMENTAL", + "updated": "2013-07-23T11:33:21Z", + "id": "v3.0", + "links": [ + { + "href": "http://127.0.0.1:8774/v3/", + "rel": "self" + }] + }] + } + '300': + description: |- + 300 response + content: + application/json: + examples: + foo: + value: | + { + "versions": [ + { + "status": "CURRENT", + "updated": "2011-01-21T11:33:21Z", + "id": "v2.0", + "links": [ + { + "href": "http://127.0.0.1:8774/v2/", + "rel": "self" + }] + }, + { + "status": "EXPERIMENTAL", + "updated": "2013-07-23T11:33:21Z", + "id": "v3.0", + "links": [ + { + "href": "http://127.0.0.1:8774/v3/", + "rel": "self" + }] + }] + } + /v2: + get: + operationId: getVersionDetailsv2 + summary: Show API version details + responses: + '200': + description: |- + 200 response + content: + application/json: + examples: + foo: + value: | + { + "version": + { + "status": "CURRENT", + "updated": "2011-01-21T11:33:21Z", + "media-types": [ + { + "base": "application/xml", + "type": "application/vnd.openstack.compute+xml;version=2" + }, + { + "base": "application/json", + "type": "application/vnd.openstack.compute+json;version=2" + }], + "id": "v2.0", + "links": [ + { + "href": "http://127.0.0.1:8774/v2/", + "rel": "self" + }, + { + "href": "http://docs.openstack.org/api/openstack-compute/2/os-compute-devguide-2.pdf", + "type": "application/pdf", + "rel": "describedby" + }, + { + "href": "http://docs.openstack.org/api/openstack-compute/2/wadl/os-compute-2.wadl", + "type": "application/vnd.sun.wadl+xml", + "rel": "describedby" + }, + { + "href": "http://docs.openstack.org/api/openstack-compute/2/wadl/os-compute-2.wadl", + "type": "application/vnd.sun.wadl+xml", + "rel": "describedby" + }] + } + } + '203': + description: |- + 203 response + content: + application/json: + examples: + foo: + value: | + { + "version": + { + "status": "CURRENT", + "updated": "2011-01-21T11:33:21Z", + "media-types": [ + { + "base": "application/xml", + "type": "application/vnd.openstack.compute+xml;version=2" + }, + { + "base": "application/json", + "type": "application/vnd.openstack.compute+json;version=2" + }], + "id": "v2.0", + "links": [ + { + "href": "http://23.253.228.211:8774/v2/", + "rel": "self" + }, + { + "href": "http://docs.openstack.org/api/openstack-compute/2/os-compute-devguide-2.pdf", + "type": "application/pdf", + "rel": "describedby" + }, + { + "href": "http://docs.openstack.org/api/openstack-compute/2/wadl/os-compute-2.wadl", + "type": "application/vnd.sun.wadl+xml", + "rel": "describedby" + }] + } + } diff --git a/packages/insomnia-importers/src/__tests__/fixtures/openapi3/example-without-servers-output.json b/packages/insomnia-importers/src/__tests__/fixtures/openapi3/example-without-servers-output.json new file mode 100644 index 000000000..193f170d0 --- /dev/null +++ b/packages/insomnia-importers/src/__tests__/fixtures/openapi3/example-without-servers-output.json @@ -0,0 +1,59 @@ +{ + "__export_date": "2020-03-24T06:16:20.516Z", + "__export_format": 4, + "__export_source": "insomnia.importers:v0.1.0", + "_type": "export", + "resources": [ + { + "_id": "__WORKSPACE_ID__", + "_type": "workspace", + "description": "", + "name": "Simple API overview 2.0.0", + "parentId": null + }, + { + "_id": "__BASE_ENVIRONMENT_ID__", + "_type": "environment", + "data": { + "base_url": "{{ scheme }}://{{ host }}{{ base_path }}" + }, + "name": "Base environment", + "parentId": "__WORKSPACE_ID__" + }, + { + "_id": "env___BASE_ENVIRONMENT_ID___sub", + "_type": "environment", + "data": { + "base_path": "", + "host": "example.com", + "scheme": "http" + }, + "name": "OpenAPI env", + "parentId": "__BASE_ENVIRONMENT_ID__" + }, + { + "_id": "req___WORKSPACE_ID__316fc296", + "_type": "request", + "authentication": {}, + "body": {}, + "headers": [], + "method": "GET", + "name": "List API versions", + "parameters": [], + "parentId": "__WORKSPACE_ID__", + "url": "{{ base_url }}/" + }, + { + "_id": "req___WORKSPACE_ID__4dd01204", + "_type": "request", + "authentication": {}, + "body": {}, + "headers": [], + "method": "GET", + "name": "Show API version details", + "parameters": [], + "parentId": "__WORKSPACE_ID__", + "url": "{{ base_url }}/v2" + } + ] +} diff --git a/packages/insomnia-importers/src/importers/openapi3.js b/packages/insomnia-importers/src/importers/openapi3.js index b67054c9d..f41620850 100644 --- a/packages/insomnia-importers/src/importers/openapi3.js +++ b/packages/insomnia-importers/src/importers/openapi3.js @@ -65,8 +65,9 @@ module.exports.convert = async function(rawData) { }, }; - const servers = api.servers.map(s => urlParse(s.url)); - const defaultServer = servers[0] || urlParse('http://example.com/'); + const servers = api.servers || []; + const serverUrls = servers.map(s => urlParse(s.url)); + const defaultServer = serverUrls[0] || urlParse('http://example.com/'); const securityVariables = getSecurityEnvVariables( api.components && api.components.securitySchemes, );