From 5ee150566b8f90261d8f9ac7f102fbbf696f98f1 Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Mon, 7 Mar 2022 10:41:28 +0000 Subject: [PATCH] Fix build, additional tests, updated body detection, support variables in urls --- .../src/api/controllers/query/import/index.ts | 4 +- .../query/import/sources/base/index.ts | 8 +++- .../query/import/sources/openapi3.ts | 44 +++++++++++++------ .../query/import/tests/index.spec.js | 43 ++++++++++++++++++ 4 files changed, 81 insertions(+), 18 deletions(-) diff --git a/packages/server/src/api/controllers/query/import/index.ts b/packages/server/src/api/controllers/query/import/index.ts index 4573fd27f7..d6f5beedf5 100644 --- a/packages/server/src/api/controllers/query/import/index.ts +++ b/packages/server/src/api/controllers/query/import/index.ts @@ -1,7 +1,7 @@ import { queryValidation } from "../validation" import { generateQueryID } from "../../../../db/utils" import { ImportInfo, ImportSource } from "./sources/base" -import { OpenAPI3 } from "./sources/openapi2" +import { OpenAPI2 } from "./sources/openapi2" import { OpenAPI3 } from "./sources/openapi3" import { Query } from "./../../../../definitions/common" import { Curl } from "./sources/curl" @@ -19,7 +19,7 @@ export class RestImporter { constructor(data: string) { this.data = data - this.sources = [new OpenAPI3(), new OpenAPI3(), new Curl()] + this.sources = [new OpenAPI2(), new OpenAPI3(), new Curl()] } init = async () => { diff --git a/packages/server/src/api/controllers/query/import/sources/base/index.ts b/packages/server/src/api/controllers/query/import/sources/base/index.ts index 096fa10e92..d1584093cf 100644 --- a/packages/server/src/api/controllers/query/import/sources/base/index.ts +++ b/packages/server/src/api/controllers/query/import/sources/base/index.ts @@ -23,7 +23,7 @@ export abstract class ImportSource { name: string, method: string, path: string, - url: URL | null, + url: URL | string | undefined, queryString: string, headers: object = {}, parameters: QueryParameter[] = [], @@ -35,7 +35,11 @@ export abstract class ImportSource { const schema = {} path = this.processPath(path) if (url) { - path = `${url.origin}/${path}` + if (typeof url === "string") { + path = `${url}/${path}` + } else { + path = `${url.origin}/${path}` + } } queryString = this.processQuery(queryString) const requestBody = JSON.stringify(body, null, 2) diff --git a/packages/server/src/api/controllers/query/import/sources/openapi3.ts b/packages/server/src/api/controllers/query/import/sources/openapi3.ts index c9e49595b2..65719ef71d 100644 --- a/packages/server/src/api/controllers/query/import/sources/openapi3.ts +++ b/packages/server/src/api/controllers/query/import/sources/openapi3.ts @@ -47,24 +47,32 @@ const isParameter = ( const getRequestBody = (operation: OpenAPIV3.OperationObject) => { if (requestBodyNotRef(operation.requestBody)) { - const request: OpenAPIV3.RequestBodyObject = - operation.requestBody as OpenAPIV3.RequestBodyObject + const request: OpenAPIV3.RequestBodyObject = operation.requestBody const supportedMimeTypes = getMimeTypes(operation) - return supportedMimeTypes.length > 0 && - schemaNotRef(request.content[supportedMimeTypes[0]].schema) - ? ( - request.content[supportedMimeTypes[0]] - .schema as OpenAPIV3.SchemaObject - ).example - : undefined + if (supportedMimeTypes.length > 0) { + const mimeType = supportedMimeTypes[0] + + // try get example from request + const content = request.content[mimeType] + if (content.example) { + return content.example + } + + // try get example from schema + if (schemaNotRef(content.schema)) { + const schema = content.schema + if (schema.example) { + return schema.example + } + } + } } return undefined } const getMimeTypes = (operation: OpenAPIV3.OperationObject): string[] => { if (requestBodyNotRef(operation.requestBody)) { - const request: OpenAPIV3.RequestBodyObject = - operation.requestBody as OpenAPIV3.RequestBodyObject + const request: OpenAPIV3.RequestBodyObject = operation.requestBody return Object.keys(request.content) } return [] @@ -99,9 +107,17 @@ export class OpenAPI3 extends OpenAPISource { } getQueries = async (datasourceId: string): Promise => { - const url: URL | null = this.document.servers - ? new URL(this.document.servers[0].url) - : null + let url: string | URL | undefined + if (this.document.servers?.length) { + url = this.document.servers[0].url + try { + url = new URL(url) + } catch (err) { + // unable to construct url, e.g. with variables + // proceed with string form of url + } + } + const queries: Query[] = [] for (let [path, pathItemObject] of Object.entries(this.document.paths)) { diff --git a/packages/server/src/api/controllers/query/import/tests/index.spec.js b/packages/server/src/api/controllers/query/import/tests/index.spec.js index 8d074ea885..0cfb4f4f19 100644 --- a/packages/server/src/api/controllers/query/import/tests/index.spec.js +++ b/packages/server/src/api/controllers/query/import/tests/index.spec.js @@ -23,14 +23,27 @@ const oapi2CrudYaml = getData("openapi2/data/crud/crud.json") const oapi2PetstoreJson = getData("openapi2/data/petstore/petstore.json") const oapi2PetstoreYaml = getData("openapi2/data/petstore/petstore.json") +// openapi3 +const oapi3CrudJson = getData("openapi3/data/crud/crud.json") +const oapi3CrudYaml = getData("openapi3/data/crud/crud.json") +const oapi3PetstoreJson = getData("openapi3/data/petstore/petstore.json") +const oapi3PetstoreYaml = getData("openapi3/data/petstore/petstore.json") + // curl const curl = getData("curl/data/post.txt") const datasets = { + // openapi2 (swagger) oapi2CrudJson, oapi2CrudYaml, oapi2PetstoreJson, oapi2PetstoreYaml, + // openapi3 + oapi3CrudJson, + oapi3CrudYaml, + oapi3PetstoreJson, + oapi3PetstoreYaml, + // curl curl } @@ -56,6 +69,7 @@ describe("Rest Importer", () => { it("gets info", async () => { const assertions = { + // openapi2 (swagger) "oapi2CrudJson" : { name: "CRUD", }, @@ -68,6 +82,20 @@ describe("Rest Importer", () => { "oapi2PetstoreYaml" :{ name: "Swagger Petstore", }, + // openapi3 + "oapi3CrudJson" : { + name: "CRUD", + }, + "oapi3CrudYaml" : { + name: "CRUD", + }, + "oapi3PetstoreJson" : { + name: "Swagger Petstore - OpenAPI 3.0", + }, + "oapi3PetstoreYaml" :{ + name: "Swagger Petstore - OpenAPI 3.0", + }, + // curl "curl": { name: "example.com", } @@ -89,6 +117,7 @@ describe("Rest Importer", () => { // simple sanity assertions that the whole dataset // makes it through the importer const assertions = { + // openapi2 (swagger) "oapi2CrudJson" : { count: 6, }, @@ -101,6 +130,20 @@ describe("Rest Importer", () => { "oapi2PetstoreYaml" :{ count: 20, }, + // openapi3 + "oapi3CrudJson" : { + count: 6, + }, + "oapi3CrudYaml" :{ + count: 6, + }, + "oapi3PetstoreJson" : { + count: 19, + }, + "oapi3PetstoreYaml" :{ + count: 19, + }, + // curl "curl": { count: 1 }