Fix build, additional tests, updated body detection, support variables in urls
This commit is contained in:
parent
090da34a99
commit
183b01dd47
|
@ -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 () => {
|
||||
|
|
|
@ -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,8 +35,12 @@ export abstract class ImportSource {
|
|||
const schema = {}
|
||||
path = this.processPath(path)
|
||||
if (url) {
|
||||
if (typeof url === "string") {
|
||||
path = `${url}/${path}`
|
||||
} else {
|
||||
path = `${url.origin}/${path}`
|
||||
}
|
||||
}
|
||||
queryString = this.processQuery(queryString)
|
||||
const requestBody = JSON.stringify(body, null, 2)
|
||||
|
||||
|
|
|
@ -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<Query[]> => {
|
||||
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)) {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue