@@ -5,6 +5,7 @@ const { readFileSync, writeFileSync } = require("fs");
5
5
const { tmpdir } = require ( "os" ) ;
6
6
const path = require ( "path" ) ;
7
7
const jp = require ( "jsonpath" ) ;
8
+ const { Resolver } = require ( "@stoplight/json-ref-resolver" ) ;
8
9
9
10
/**
10
11
* convertAllSchemasToJSONSchema takes in the OpenAPIV3 Schemas in an array
@@ -23,17 +24,39 @@ function convertAllSchemasToJSONSchema(schemas) {
23
24
* readSchema will read schema file from the given location, it expects
24
25
* the schema to be in JSON format
25
26
*
26
- * readSchema will also apply the given jsonpath filter to the read schema
27
- * and will return only the filtered JSONs
27
+ * readSchema will also resolve the references if a resolveQuery is passed
28
28
* @param {string } location
29
- * @param {string } query jsonpath based query
30
- * @returns {any[] }
29
+ * @param {string } resolveQuery jsonpath based query - must resolve to EXACTLY one match or else is ignored
30
+ * @returns {Promise< any[]> }
31
31
*/
32
- function readSchema ( location , query ) {
32
+ async function readSchema ( location , resolveQuery ) {
33
33
const data = readFileSync ( location , "utf-8" ) ;
34
34
const parsed = JSON . parse ( data ) ;
35
35
36
- return jp . query ( parsed , query ) ;
36
+ if ( resolveQuery ) {
37
+ const inner = jp . query ( parsed , resolveQuery ) ;
38
+
39
+ if ( inner . length !== 1 ) return parsed ;
40
+
41
+ const resolver = new Resolver ( ) ;
42
+ const resolved = await resolver . resolve ( inner [ 0 ] , { } ) ;
43
+
44
+ if ( resolved . errors . length ) console . error ( resolved . errors ) ;
45
+
46
+ return resolved . result ;
47
+ }
48
+
49
+ return parsed ;
50
+ }
51
+
52
+ /**
53
+ * filterSchemas takes in an array of schemas and will return an array of filtered schemas
54
+ * @param {Array<any> } schemas - OpenAPI schema in JSON format
55
+ * @param {string } query jsonpath based query to filter out the data
56
+ * @returns {Array<any> }
57
+ */
58
+ function filterSchemas ( schemas , query ) {
59
+ return jp . query ( schemas , query ) ;
37
60
}
38
61
39
62
/**
@@ -72,16 +95,19 @@ function setupFiles(location, type) {
72
95
* @param {string } location location of the schemas in open api v3 format
73
96
* @param {"yaml" | "json" } type encoding in which the openapi schema is present
74
97
* @param {string } query jsonpath query to filter the read schemas
98
+ * @param {string } resolve jsonpath query to reach to the root of the openAPI spec
75
99
*/
76
- function ToJSONSchema ( location , type = "yaml" , query = "" ) {
100
+ async function ToJSONSchema ( location , type = "yaml" , query = "" , resolve = "" ) {
77
101
if ( type !== "yaml" && type !== "json" )
78
102
throw Error ( 'invalid type received: can be either "yaml" or "json"' ) ;
79
103
80
104
const source = setupFiles ( location , type ) ;
81
105
82
- const schemas = readSchema ( source , query ) ;
106
+ const schemas = await readSchema ( source , resolve ) ;
107
+
108
+ const filtered = filterSchemas ( schemas , query ) ;
83
109
84
- return convertAllSchemasToJSONSchema ( schemas ) ;
110
+ return convertAllSchemasToJSONSchema ( filtered ) ;
85
111
}
86
112
87
113
module . exports = ToJSONSchema ;
0 commit comments