Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

feat: add support to openapi importer for yaml

Andrew Bastin9e6a3883ac1f
diff --git a/packages/hoppscotch-app/helpers/import-export/import/openapi.ts b/packages/hoppscotch-app/helpers/import-export/import/openapi.ts
index 2299dda1d..82d01568b 100644
--- a/packages/hoppscotch-app/helpers/import-export/import/openapi.ts
+++ b/packages/hoppscotch-app/helpers/import-export/import/openapi.ts
@@ -5,6 +5,7 @@ import {
OpenAPIV3_1 as OpenAPIV31,
} from "openapi-types"
import SwaggerParser from "@apidevtools/swagger-parser"
+import yaml from "js-yaml"
import {
FormDataKeyValue,
HoppRESTAuth,
@@ -28,11 +29,11 @@ import { Collection, makeCollection } from "~/newstore/collections"
const OPENAPI_DEREF_ERROR = "openapi/deref_error" as const
// TODO: URL Import Support
-// TODO: YAMLLLLLLL import support!!!!!
-// TODO: Oauth!
const safeParseJSON = (str: string) => O.tryCatch(() => JSON.parse(str))
+const safeParseYAML = (str: string) => O.tryCatch(() => yaml.load(str))
+
const objectHasProperty = <T extends string>(
obj: unknown,
propName: T
@@ -573,13 +574,23 @@ const convertOpenApiDocToHopp = (
])
}
+const parseOpenAPIDocContent = (str: string) =>
+ pipe(
+ str,
+ safeParseJSON,
+ O.match(
+ () => safeParseYAML(str),
+ (data) => O.of(data)
+ )
+ )
+
export default defineImporter({
name: "Swagger/OpenAPI v3 Schema",
steps: [
step({
stepName: "FILE_OR_URL_IMPORT",
metadata: {
- acceptedFileTypes: "application/json",
+ acceptedFileTypes: ".json, .yaml, .yml",
},
}),
] as const,
@@ -587,7 +598,7 @@ export default defineImporter({
pipe(
// See if we can parse JSON properly
fileContent,
- safeParseJSON,
+ parseOpenAPIDocContent,
TE.fromOption(() => IMPORTER_INVALID_FILE_FORMAT),
// Try validating, else the importer is invalid file format
diff --git a/packages/hoppscotch-app/package.json b/packages/hoppscotch-app/package.json
index b976317ac..7b8a22663 100644
--- a/packages/hoppscotch-app/package.json
+++ b/packages/hoppscotch-app/package.json
@@ -84,6 +84,7 @@
"graphql-tag": "^2.12.6",
"httpsnippet": "^2.0.0",
"io-ts": "^2.2.16",
+ "js-yaml": "^4.1.0",
"json-loader": "^0.5.7",
"lodash": "^4.17.21",
"mustache": "^4.2.0",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index f8e3735d7..88bfca941 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -138,6 +138,7 @@ importers:
io-ts: ^2.2.16
jest: ^27.4.5
jest-serializer-vue: ^2.0.2
+ js-yaml: ^4.1.0
json-loader: ^0.5.7
lodash: ^4.17.21
mustache: ^4.2.0
@@ -230,6 +231,7 @@ importers:
graphql-tag: [email protected]
httpsnippet: 2.0.0
+ js-yaml: 4.1.0
json-loader: 0.5.7
lodash: 4.17.21
mustache: 4.2.0