1
0
Fork 0
mirror of synced 2024-09-02 18:51:36 +12:00

Fix build, additional tests, updated body detection, support variables in urls

This commit is contained in:
Rory Powell 2022-03-07 10:41:28 +00:00
parent 5bc9051efa
commit 5ee150566b
4 changed files with 81 additions and 18 deletions

View file

@ -1,7 +1,7 @@
import { queryValidation } from "../validation" import { queryValidation } from "../validation"
import { generateQueryID } from "../../../../db/utils" import { generateQueryID } from "../../../../db/utils"
import { ImportInfo, ImportSource } from "./sources/base" import { ImportInfo, ImportSource } from "./sources/base"
import { OpenAPI3 } from "./sources/openapi2" import { OpenAPI2 } from "./sources/openapi2"
import { OpenAPI3 } from "./sources/openapi3" import { OpenAPI3 } from "./sources/openapi3"
import { Query } from "./../../../../definitions/common" import { Query } from "./../../../../definitions/common"
import { Curl } from "./sources/curl" import { Curl } from "./sources/curl"
@ -19,7 +19,7 @@ export class RestImporter {
constructor(data: string) { constructor(data: string) {
this.data = data this.data = data
this.sources = [new OpenAPI3(), new OpenAPI3(), new Curl()] this.sources = [new OpenAPI2(), new OpenAPI3(), new Curl()]
} }
init = async () => { init = async () => {

View file

@ -23,7 +23,7 @@ export abstract class ImportSource {
name: string, name: string,
method: string, method: string,
path: string, path: string,
url: URL | null, url: URL | string | undefined,
queryString: string, queryString: string,
headers: object = {}, headers: object = {},
parameters: QueryParameter[] = [], parameters: QueryParameter[] = [],
@ -35,7 +35,11 @@ export abstract class ImportSource {
const schema = {} const schema = {}
path = this.processPath(path) path = this.processPath(path)
if (url) { if (url) {
path = `${url.origin}/${path}` if (typeof url === "string") {
path = `${url}/${path}`
} else {
path = `${url.origin}/${path}`
}
} }
queryString = this.processQuery(queryString) queryString = this.processQuery(queryString)
const requestBody = JSON.stringify(body, null, 2) const requestBody = JSON.stringify(body, null, 2)

View file

@ -47,24 +47,32 @@ const isParameter = (
const getRequestBody = (operation: OpenAPIV3.OperationObject) => { const getRequestBody = (operation: OpenAPIV3.OperationObject) => {
if (requestBodyNotRef(operation.requestBody)) { if (requestBodyNotRef(operation.requestBody)) {
const request: OpenAPIV3.RequestBodyObject = const request: OpenAPIV3.RequestBodyObject = operation.requestBody
operation.requestBody as OpenAPIV3.RequestBodyObject
const supportedMimeTypes = getMimeTypes(operation) const supportedMimeTypes = getMimeTypes(operation)
return supportedMimeTypes.length > 0 && if (supportedMimeTypes.length > 0) {
schemaNotRef(request.content[supportedMimeTypes[0]].schema) const mimeType = supportedMimeTypes[0]
? (
request.content[supportedMimeTypes[0]] // try get example from request
.schema as OpenAPIV3.SchemaObject const content = request.content[mimeType]
).example if (content.example) {
: undefined return content.example
}
// try get example from schema
if (schemaNotRef(content.schema)) {
const schema = content.schema
if (schema.example) {
return schema.example
}
}
}
} }
return undefined return undefined
} }
const getMimeTypes = (operation: OpenAPIV3.OperationObject): string[] => { const getMimeTypes = (operation: OpenAPIV3.OperationObject): string[] => {
if (requestBodyNotRef(operation.requestBody)) { if (requestBodyNotRef(operation.requestBody)) {
const request: OpenAPIV3.RequestBodyObject = const request: OpenAPIV3.RequestBodyObject = operation.requestBody
operation.requestBody as OpenAPIV3.RequestBodyObject
return Object.keys(request.content) return Object.keys(request.content)
} }
return [] return []
@ -99,9 +107,17 @@ export class OpenAPI3 extends OpenAPISource {
} }
getQueries = async (datasourceId: string): Promise<Query[]> => { getQueries = async (datasourceId: string): Promise<Query[]> => {
const url: URL | null = this.document.servers let url: string | URL | undefined
? new URL(this.document.servers[0].url) if (this.document.servers?.length) {
: null 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[] = [] const queries: Query[] = []
for (let [path, pathItemObject] of Object.entries(this.document.paths)) { for (let [path, pathItemObject] of Object.entries(this.document.paths)) {

View file

@ -23,14 +23,27 @@ const oapi2CrudYaml = getData("openapi2/data/crud/crud.json")
const oapi2PetstoreJson = getData("openapi2/data/petstore/petstore.json") const oapi2PetstoreJson = getData("openapi2/data/petstore/petstore.json")
const oapi2PetstoreYaml = 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 // curl
const curl = getData("curl/data/post.txt") const curl = getData("curl/data/post.txt")
const datasets = { const datasets = {
// openapi2 (swagger)
oapi2CrudJson, oapi2CrudJson,
oapi2CrudYaml, oapi2CrudYaml,
oapi2PetstoreJson, oapi2PetstoreJson,
oapi2PetstoreYaml, oapi2PetstoreYaml,
// openapi3
oapi3CrudJson,
oapi3CrudYaml,
oapi3PetstoreJson,
oapi3PetstoreYaml,
// curl
curl curl
} }
@ -56,6 +69,7 @@ describe("Rest Importer", () => {
it("gets info", async () => { it("gets info", async () => {
const assertions = { const assertions = {
// openapi2 (swagger)
"oapi2CrudJson" : { "oapi2CrudJson" : {
name: "CRUD", name: "CRUD",
}, },
@ -68,6 +82,20 @@ describe("Rest Importer", () => {
"oapi2PetstoreYaml" :{ "oapi2PetstoreYaml" :{
name: "Swagger Petstore", 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": { "curl": {
name: "example.com", name: "example.com",
} }
@ -89,6 +117,7 @@ describe("Rest Importer", () => {
// simple sanity assertions that the whole dataset // simple sanity assertions that the whole dataset
// makes it through the importer // makes it through the importer
const assertions = { const assertions = {
// openapi2 (swagger)
"oapi2CrudJson" : { "oapi2CrudJson" : {
count: 6, count: 6,
}, },
@ -101,6 +130,20 @@ describe("Rest Importer", () => {
"oapi2PetstoreYaml" :{ "oapi2PetstoreYaml" :{
count: 20, count: 20,
}, },
// openapi3
"oapi3CrudJson" : {
count: 6,
},
"oapi3CrudYaml" :{
count: 6,
},
"oapi3PetstoreJson" : {
count: 19,
},
"oapi3PetstoreYaml" :{
count: 19,
},
// curl
"curl": { "curl": {
count: 1 count: 1
} }