@apidevtools/json-schema-ref-parser 11.7.3 → 11.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -9
- package/dist/lib/dereference.js +24 -1
- package/dist/lib/index.d.ts +6 -6
- package/dist/lib/options.d.ts +17 -0
- package/dist/lib/pointer.js +7 -1
- package/dist/lib/util/errors.d.ts +5 -1
- package/dist/lib/util/errors.js +5 -1
- package/dist/lib/util/plugins.d.ts +2 -2
- package/dist/lib/util/url.js +3 -2
- package/lib/dereference.ts +28 -1
- package/lib/index.ts +6 -12
- package/lib/options.ts +17 -0
- package/lib/pointer.ts +12 -1
- package/lib/util/errors.ts +10 -1
- package/lib/util/plugins.ts +6 -7
- package/lib/util/url.ts +3 -2
- package/package.json +27 -27
package/README.md
CHANGED
|
@@ -155,14 +155,10 @@ To build/test the project locally on your computer:
|
|
|
155
155
|
|
|
156
156
|
JSON Schema $Ref Parser is 100% free and open-source, under the [MIT license](LICENSE). Use it however you want.
|
|
157
157
|
|
|
158
|
-
|
|
159
|
-
tree**](https://plant.treeware.earth/APIDevTools/json-schema-ref-parser) to thank us for our work. By contributing to
|
|
160
|
-
the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
|
|
158
|
+
## Thanks
|
|
161
159
|
|
|
162
|
-
|
|
160
|
+
Thanks to these awesome contributors for their major support of this open-source project.
|
|
163
161
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
[
|
|
167
|
-
[](https://saucelabs.com)
|
|
168
|
-
[](https://coveralls.io)
|
|
162
|
+
- [JonLuca De Caro](https://jonlu.ca)
|
|
163
|
+
- [Phil Sturgeon](https://philsturgeon.com)
|
|
164
|
+
- [Stoplight](https://stoplight.io/?utm_source=github&utm_medium=readme&utm_campaign=json_schema_ref_parser)
|
package/dist/lib/dereference.js
CHANGED
|
@@ -106,7 +106,28 @@ function crawl(obj, path, pathFromRoot, parents, processedObjects, dereferencedC
|
|
|
106
106
|
circular = dereferenced.circular;
|
|
107
107
|
// Avoid pointless mutations; breaks frozen objects to no profit
|
|
108
108
|
if (obj[key] !== dereferenced.value) {
|
|
109
|
+
// If we have properties we want to preserve from our dereferenced schema then we need
|
|
110
|
+
// to copy them over to our new object.
|
|
111
|
+
const preserved = new Map();
|
|
112
|
+
if (derefOptions?.preservedProperties) {
|
|
113
|
+
if (typeof obj[key] === "object" && !Array.isArray(obj[key])) {
|
|
114
|
+
derefOptions?.preservedProperties.forEach((prop) => {
|
|
115
|
+
if (prop in obj[key]) {
|
|
116
|
+
preserved.set(prop, obj[key][prop]);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
109
121
|
obj[key] = dereferenced.value;
|
|
122
|
+
// If we have data to preserve and our dereferenced object is still an object then
|
|
123
|
+
// we need copy back our preserved data into our dereferenced schema.
|
|
124
|
+
if (derefOptions?.preservedProperties) {
|
|
125
|
+
if (preserved.size && typeof obj[key] === "object" && !Array.isArray(obj[key])) {
|
|
126
|
+
preserved.forEach((value, prop) => {
|
|
127
|
+
obj[key][prop] = value;
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
110
131
|
derefOptions?.onDereference?.(value.$ref, obj[key], obj, key);
|
|
111
132
|
}
|
|
112
133
|
}
|
|
@@ -210,7 +231,8 @@ function dereference$Ref($ref, path, pathFromRoot, parents, processedObjects, de
|
|
|
210
231
|
}
|
|
211
232
|
/**
|
|
212
233
|
* Called when a circular reference is found.
|
|
213
|
-
* It sets the {@link $Refs#circular} flag,
|
|
234
|
+
* It sets the {@link $Refs#circular} flag, executes the options.dereference.onCircular callback,
|
|
235
|
+
* and throws an error if options.dereference.circular is false.
|
|
214
236
|
*
|
|
215
237
|
* @param keyPath - The JSON Reference path of the circular reference
|
|
216
238
|
* @param $refs
|
|
@@ -219,6 +241,7 @@ function dereference$Ref($ref, path, pathFromRoot, parents, processedObjects, de
|
|
|
219
241
|
*/
|
|
220
242
|
function foundCircularReference(keyPath, $refs, options) {
|
|
221
243
|
$refs.circular = true;
|
|
244
|
+
options?.dereference?.onCircular?.(keyPath);
|
|
222
245
|
if (!options.dereference.circular) {
|
|
223
246
|
throw ono_1.ono.reference(`Circular $ref pointer found at ${keyPath}`);
|
|
224
247
|
}
|
package/dist/lib/index.d.ts
CHANGED
|
@@ -44,8 +44,8 @@ export declare class $RefParser<S extends object = JSONSchema, O extends ParserO
|
|
|
44
44
|
parse(schema: S | string | unknown, options: O, callback: SchemaCallback<S>): Promise<void>;
|
|
45
45
|
parse(baseUrl: string, schema: S | string | unknown, options: O): Promise<S>;
|
|
46
46
|
parse(baseUrl: string, schema: S | string | unknown, options: O, callback: SchemaCallback<S>): Promise<void>;
|
|
47
|
-
static parse<S extends object = JSONSchema
|
|
48
|
-
static parse<S extends object = JSONSchema
|
|
47
|
+
static parse<S extends object = JSONSchema>(schema: S | string | unknown): Promise<S>;
|
|
48
|
+
static parse<S extends object = JSONSchema>(schema: S | string | unknown, callback: SchemaCallback<S>): Promise<void>;
|
|
49
49
|
static parse<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(schema: S | string | unknown, options: O): Promise<S>;
|
|
50
50
|
static parse<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(schema: S | string | unknown, options: O, callback: SchemaCallback<S>): Promise<void>;
|
|
51
51
|
static parse<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(baseUrl: string, schema: S | string | unknown, options: O): Promise<S>;
|
|
@@ -95,8 +95,8 @@ export declare class $RefParser<S extends object = JSONSchema, O extends ParserO
|
|
|
95
95
|
* @param options (optional)
|
|
96
96
|
* @param callback (optional) A callback that will receive the bundled schema object
|
|
97
97
|
*/
|
|
98
|
-
static bundle<S extends object = JSONSchema
|
|
99
|
-
static bundle<S extends object = JSONSchema
|
|
98
|
+
static bundle<S extends object = JSONSchema>(schema: S | string | unknown): Promise<S>;
|
|
99
|
+
static bundle<S extends object = JSONSchema>(schema: S | string | unknown, callback: SchemaCallback<S>): Promise<void>;
|
|
100
100
|
static bundle<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(schema: S | string | unknown, options: O): Promise<S>;
|
|
101
101
|
static bundle<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(schema: S | string | unknown, options: O, callback: SchemaCallback<S>): Promise<void>;
|
|
102
102
|
static bundle<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(baseUrl: string, schema: S | string | unknown, options: O): Promise<S>;
|
|
@@ -129,8 +129,8 @@ export declare class $RefParser<S extends object = JSONSchema, O extends ParserO
|
|
|
129
129
|
* @param options (optional)
|
|
130
130
|
* @param callback (optional) A callback that will receive the dereferenced schema object
|
|
131
131
|
*/
|
|
132
|
-
static dereference<S extends object = JSONSchema
|
|
133
|
-
static dereference<S extends object = JSONSchema
|
|
132
|
+
static dereference<S extends object = JSONSchema>(schema: S | string | unknown): Promise<S>;
|
|
133
|
+
static dereference<S extends object = JSONSchema>(schema: S | string | unknown, callback: SchemaCallback<S>): Promise<void>;
|
|
134
134
|
static dereference<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(schema: S | string | unknown, options: O): Promise<S>;
|
|
135
135
|
static dereference<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(schema: S | string | unknown, options: O, callback: SchemaCallback<S>): Promise<void>;
|
|
136
136
|
static dereference<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(baseUrl: string, schema: S | string | unknown, options: O): Promise<S>;
|
package/dist/lib/options.d.ts
CHANGED
|
@@ -17,6 +17,12 @@ export interface DereferenceOptions {
|
|
|
17
17
|
* subpaths contain literal $ref keys that should not be dereferenced.
|
|
18
18
|
*/
|
|
19
19
|
excludedPathMatcher?(path: string): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Callback invoked during circular reference detection.
|
|
22
|
+
*
|
|
23
|
+
* @argument {string} path - The path that is circular (ie. the `$ref` string)
|
|
24
|
+
*/
|
|
25
|
+
onCircular?(path: string): void;
|
|
20
26
|
/**
|
|
21
27
|
* Callback invoked during dereferencing.
|
|
22
28
|
*
|
|
@@ -26,6 +32,15 @@ export interface DereferenceOptions {
|
|
|
26
32
|
* @argument {string} parentPropName - The prop name of the parent object whose value was dereferenced
|
|
27
33
|
*/
|
|
28
34
|
onDereference?(path: string, value: JSONSchemaObject, parent?: JSONSchemaObject, parentPropName?: string): void;
|
|
35
|
+
/**
|
|
36
|
+
* An array of properties to preserve when dereferencing a `$ref` schema. Useful if you want to
|
|
37
|
+
* enforce non-standard dereferencing behavior like present in the OpenAPI 3.1 specification where
|
|
38
|
+
* `description` and `summary` properties are preserved when alongside a `$ref` pointer.
|
|
39
|
+
*
|
|
40
|
+
* If none supplied then no properties will be preserved and the object will be fully replaced
|
|
41
|
+
* with the dereferenced `$ref`.
|
|
42
|
+
*/
|
|
43
|
+
preservedProperties?: string[];
|
|
29
44
|
/**
|
|
30
45
|
* Whether a reference should resolve relative to its directory/path, or from the cwd
|
|
31
46
|
*
|
|
@@ -376,7 +391,9 @@ export declare const getNewOptions: <S extends object = JSONSchema, O extends Pa
|
|
|
376
391
|
dereference?: {
|
|
377
392
|
circular?: boolean | "ignore" | undefined;
|
|
378
393
|
excludedPathMatcher?: {} | undefined;
|
|
394
|
+
onCircular?: {} | undefined;
|
|
379
395
|
onDereference?: {} | undefined;
|
|
396
|
+
preservedProperties?: (string | undefined)[] | undefined;
|
|
380
397
|
externalReferenceResolution?: "relative" | "root" | undefined;
|
|
381
398
|
} | undefined;
|
|
382
399
|
mutateInputSchema?: boolean | undefined;
|
package/dist/lib/pointer.js
CHANGED
|
@@ -83,6 +83,7 @@ class Pointer {
|
|
|
83
83
|
*/
|
|
84
84
|
resolve(obj, options, pathFromRoot) {
|
|
85
85
|
const tokens = Pointer.parse(this.path, this.originalPath);
|
|
86
|
+
const found = [];
|
|
86
87
|
// Crawl the object, one token at a time
|
|
87
88
|
this.value = unwrapOrThrow(obj);
|
|
88
89
|
for (let i = 0; i < tokens.length; i++) {
|
|
@@ -110,11 +111,16 @@ class Pointer {
|
|
|
110
111
|
continue;
|
|
111
112
|
}
|
|
112
113
|
this.value = null;
|
|
113
|
-
|
|
114
|
+
const path = this.$ref.path || "";
|
|
115
|
+
const targetRef = this.path.replace(path, "");
|
|
116
|
+
const targetFound = Pointer.join("", found);
|
|
117
|
+
const parentPath = pathFromRoot?.replace(path, "");
|
|
118
|
+
throw new errors_js_1.MissingPointerError(token, decodeURI(this.originalPath), targetRef, targetFound, parentPath);
|
|
114
119
|
}
|
|
115
120
|
else {
|
|
116
121
|
this.value = this.value[token];
|
|
117
122
|
}
|
|
123
|
+
found.push(token);
|
|
118
124
|
}
|
|
119
125
|
// Resolve the final value
|
|
120
126
|
if (!this.value || (this.value.$ref && url.resolve(this.path, this.value.$ref) !== pathFromRoot)) {
|
|
@@ -41,7 +41,11 @@ export declare class UnmatchedResolverError extends JSONParserError {
|
|
|
41
41
|
export declare class MissingPointerError extends JSONParserError {
|
|
42
42
|
code: JSONParserErrorType;
|
|
43
43
|
name: string;
|
|
44
|
-
|
|
44
|
+
targetToken: any;
|
|
45
|
+
targetRef: string;
|
|
46
|
+
targetFound: string;
|
|
47
|
+
parentPath: string;
|
|
48
|
+
constructor(token: any, path: any, targetRef: any, targetFound: any, parentPath: any);
|
|
45
49
|
}
|
|
46
50
|
export declare class TimeoutError extends JSONParserError {
|
|
47
51
|
code: JSONParserErrorType;
|
package/dist/lib/util/errors.js
CHANGED
|
@@ -78,10 +78,14 @@ class UnmatchedResolverError extends JSONParserError {
|
|
|
78
78
|
}
|
|
79
79
|
exports.UnmatchedResolverError = UnmatchedResolverError;
|
|
80
80
|
class MissingPointerError extends JSONParserError {
|
|
81
|
-
constructor(token, path) {
|
|
81
|
+
constructor(token, path, targetRef, targetFound, parentPath) {
|
|
82
82
|
super(`Missing $ref pointer "${(0, url_js_1.getHash)(path)}". Token "${token}" does not exist.`, (0, url_js_1.stripHash)(path));
|
|
83
83
|
this.code = "EMISSINGPOINTER";
|
|
84
84
|
this.name = "MissingPointerError";
|
|
85
|
+
this.targetToken = token;
|
|
86
|
+
this.targetRef = targetRef;
|
|
87
|
+
this.targetFound = targetFound;
|
|
88
|
+
this.parentPath = parentPath;
|
|
85
89
|
}
|
|
86
90
|
}
|
|
87
91
|
exports.MissingPointerError = MissingPointerError;
|
|
@@ -18,7 +18,7 @@ export declare function filter(plugins: Plugin[], method: any, file: any): Plugi
|
|
|
18
18
|
* Sorts the given plugins, in place, by their `order` property.
|
|
19
19
|
*/
|
|
20
20
|
export declare function sort(plugins: Plugin[]): Plugin[];
|
|
21
|
-
export interface PluginResult<S extends object = JSONSchema
|
|
21
|
+
export interface PluginResult<S extends object = JSONSchema> {
|
|
22
22
|
plugin: Plugin;
|
|
23
23
|
result?: string | Buffer | S;
|
|
24
24
|
error?: any;
|
|
@@ -31,4 +31,4 @@ export interface PluginResult<S extends object = JSONSchema, O extends ParserOpt
|
|
|
31
31
|
* If the promise rejects, or the callback is called with an error, then the next plugin is called.
|
|
32
32
|
* If ALL plugins fail, then the last error is thrown.
|
|
33
33
|
*/
|
|
34
|
-
export declare function run<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(plugins: Plugin[], method: keyof Plugin | keyof ResolverOptions<S>, file: FileInfo, $refs: $Refs<S, O>): Promise<PluginResult<S
|
|
34
|
+
export declare function run<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(plugins: Plugin[], method: keyof Plugin | keyof ResolverOptions<S>, file: FileInfo, $refs: $Refs<S, O>): Promise<PluginResult<S>>;
|
package/dist/lib/util/url.js
CHANGED
|
@@ -73,10 +73,11 @@ exports.parse = parse;
|
|
|
73
73
|
* @returns
|
|
74
74
|
*/
|
|
75
75
|
function resolve(from, to) {
|
|
76
|
-
|
|
76
|
+
// we use a non-existent URL to check if its a relative URL
|
|
77
|
+
const fromUrl = new URL((0, convert_path_to_posix_1.default)(from), "https://aaa.nonexistanturl.com");
|
|
77
78
|
const resolvedUrl = new URL((0, convert_path_to_posix_1.default)(to), fromUrl);
|
|
78
79
|
const endSpaces = to.match(/(\s*)$/)?.[1] || "";
|
|
79
|
-
if (resolvedUrl.
|
|
80
|
+
if (resolvedUrl.hostname === "aaa.nonexistanturl.com") {
|
|
80
81
|
// `from` is a relative URL.
|
|
81
82
|
const { pathname, search, hash } = resolvedUrl;
|
|
82
83
|
return pathname + search + hash + endSpaces;
|
package/lib/dereference.ts
CHANGED
|
@@ -123,7 +123,31 @@ function crawl<S extends object = JSONSchema, O extends ParserOptions<S> = Parse
|
|
|
123
123
|
circular = dereferenced.circular;
|
|
124
124
|
// Avoid pointless mutations; breaks frozen objects to no profit
|
|
125
125
|
if (obj[key] !== dereferenced.value) {
|
|
126
|
+
// If we have properties we want to preserve from our dereferenced schema then we need
|
|
127
|
+
// to copy them over to our new object.
|
|
128
|
+
const preserved: Map<string, unknown> = new Map();
|
|
129
|
+
if (derefOptions?.preservedProperties) {
|
|
130
|
+
if (typeof obj[key] === "object" && !Array.isArray(obj[key])) {
|
|
131
|
+
derefOptions?.preservedProperties.forEach((prop) => {
|
|
132
|
+
if (prop in obj[key]) {
|
|
133
|
+
preserved.set(prop, obj[key][prop]);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
126
139
|
obj[key] = dereferenced.value;
|
|
140
|
+
|
|
141
|
+
// If we have data to preserve and our dereferenced object is still an object then
|
|
142
|
+
// we need copy back our preserved data into our dereferenced schema.
|
|
143
|
+
if (derefOptions?.preservedProperties) {
|
|
144
|
+
if (preserved.size && typeof obj[key] === "object" && !Array.isArray(obj[key])) {
|
|
145
|
+
preserved.forEach((value, prop) => {
|
|
146
|
+
obj[key][prop] = value;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
127
151
|
derefOptions?.onDereference?.(value.$ref, obj[key], obj, key);
|
|
128
152
|
}
|
|
129
153
|
} else {
|
|
@@ -272,7 +296,8 @@ function dereference$Ref<S extends object = JSONSchema, O extends ParserOptions<
|
|
|
272
296
|
|
|
273
297
|
/**
|
|
274
298
|
* Called when a circular reference is found.
|
|
275
|
-
* It sets the {@link $Refs#circular} flag,
|
|
299
|
+
* It sets the {@link $Refs#circular} flag, executes the options.dereference.onCircular callback,
|
|
300
|
+
* and throws an error if options.dereference.circular is false.
|
|
276
301
|
*
|
|
277
302
|
* @param keyPath - The JSON Reference path of the circular reference
|
|
278
303
|
* @param $refs
|
|
@@ -281,6 +306,8 @@ function dereference$Ref<S extends object = JSONSchema, O extends ParserOptions<
|
|
|
281
306
|
*/
|
|
282
307
|
function foundCircularReference(keyPath: any, $refs: any, options: any) {
|
|
283
308
|
$refs.circular = true;
|
|
309
|
+
options?.dereference?.onCircular?.(keyPath);
|
|
310
|
+
|
|
284
311
|
if (!options.dereference.circular) {
|
|
285
312
|
throw ono.reference(`Circular $ref pointer found at ${keyPath}`);
|
|
286
313
|
}
|
package/lib/index.ts
CHANGED
|
@@ -144,10 +144,8 @@ export class $RefParser<S extends object = JSONSchema, O extends ParserOptions<S
|
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
-
public static parse<S extends object = JSONSchema
|
|
148
|
-
|
|
149
|
-
): Promise<S>;
|
|
150
|
-
public static parse<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
|
|
147
|
+
public static parse<S extends object = JSONSchema>(schema: S | string | unknown): Promise<S>;
|
|
148
|
+
public static parse<S extends object = JSONSchema>(
|
|
151
149
|
schema: S | string | unknown,
|
|
152
150
|
callback: SchemaCallback<S>,
|
|
153
151
|
): Promise<void>;
|
|
@@ -269,10 +267,8 @@ export class $RefParser<S extends object = JSONSchema, O extends ParserOptions<S
|
|
|
269
267
|
* @param options (optional)
|
|
270
268
|
* @param callback (optional) A callback that will receive the bundled schema object
|
|
271
269
|
*/
|
|
272
|
-
public static bundle<S extends object = JSONSchema
|
|
273
|
-
|
|
274
|
-
): Promise<S>;
|
|
275
|
-
public static bundle<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
|
|
270
|
+
public static bundle<S extends object = JSONSchema>(schema: S | string | unknown): Promise<S>;
|
|
271
|
+
public static bundle<S extends object = JSONSchema>(
|
|
276
272
|
schema: S | string | unknown,
|
|
277
273
|
callback: SchemaCallback<S>,
|
|
278
274
|
): Promise<void>;
|
|
@@ -343,10 +339,8 @@ export class $RefParser<S extends object = JSONSchema, O extends ParserOptions<S
|
|
|
343
339
|
* @param options (optional)
|
|
344
340
|
* @param callback (optional) A callback that will receive the dereferenced schema object
|
|
345
341
|
*/
|
|
346
|
-
public static dereference<S extends object = JSONSchema
|
|
347
|
-
|
|
348
|
-
): Promise<S>;
|
|
349
|
-
public static dereference<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
|
|
342
|
+
public static dereference<S extends object = JSONSchema>(schema: S | string | unknown): Promise<S>;
|
|
343
|
+
public static dereference<S extends object = JSONSchema>(
|
|
350
344
|
schema: S | string | unknown,
|
|
351
345
|
callback: SchemaCallback<S>,
|
|
352
346
|
): Promise<void>;
|
package/lib/options.ts
CHANGED
|
@@ -29,6 +29,13 @@ export interface DereferenceOptions {
|
|
|
29
29
|
*/
|
|
30
30
|
excludedPathMatcher?(path: string): boolean;
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Callback invoked during circular reference detection.
|
|
34
|
+
*
|
|
35
|
+
* @argument {string} path - The path that is circular (ie. the `$ref` string)
|
|
36
|
+
*/
|
|
37
|
+
onCircular?(path: string): void;
|
|
38
|
+
|
|
32
39
|
/**
|
|
33
40
|
* Callback invoked during dereferencing.
|
|
34
41
|
*
|
|
@@ -39,6 +46,16 @@ export interface DereferenceOptions {
|
|
|
39
46
|
*/
|
|
40
47
|
onDereference?(path: string, value: JSONSchemaObject, parent?: JSONSchemaObject, parentPropName?: string): void;
|
|
41
48
|
|
|
49
|
+
/**
|
|
50
|
+
* An array of properties to preserve when dereferencing a `$ref` schema. Useful if you want to
|
|
51
|
+
* enforce non-standard dereferencing behavior like present in the OpenAPI 3.1 specification where
|
|
52
|
+
* `description` and `summary` properties are preserved when alongside a `$ref` pointer.
|
|
53
|
+
*
|
|
54
|
+
* If none supplied then no properties will be preserved and the object will be fully replaced
|
|
55
|
+
* with the dereferenced `$ref`.
|
|
56
|
+
*/
|
|
57
|
+
preservedProperties?: string[];
|
|
58
|
+
|
|
42
59
|
/**
|
|
43
60
|
* Whether a reference should resolve relative to its directory/path, or from the cwd
|
|
44
61
|
*
|
package/lib/pointer.ts
CHANGED
|
@@ -88,6 +88,7 @@ class Pointer<S extends object = JSONSchema, O extends ParserOptions<S> = Parser
|
|
|
88
88
|
*/
|
|
89
89
|
resolve(obj: S, options?: O, pathFromRoot?: string) {
|
|
90
90
|
const tokens = Pointer.parse(this.path, this.originalPath);
|
|
91
|
+
const found: any = [];
|
|
91
92
|
|
|
92
93
|
// Crawl the object, one token at a time
|
|
93
94
|
this.value = unwrapOrThrow(obj);
|
|
@@ -103,6 +104,7 @@ class Pointer<S extends object = JSONSchema, O extends ParserOptions<S> = Parser
|
|
|
103
104
|
}
|
|
104
105
|
|
|
105
106
|
const token = tokens[i];
|
|
107
|
+
|
|
106
108
|
if (this.value[token] === undefined || (this.value[token] === null && i === tokens.length - 1)) {
|
|
107
109
|
// one final case is if the entry itself includes slashes, and was parsed out as a token - we can join the remaining tokens and try again
|
|
108
110
|
let didFindSubstringSlashMatch = false;
|
|
@@ -120,10 +122,19 @@ class Pointer<S extends object = JSONSchema, O extends ParserOptions<S> = Parser
|
|
|
120
122
|
}
|
|
121
123
|
|
|
122
124
|
this.value = null;
|
|
123
|
-
|
|
125
|
+
|
|
126
|
+
const path = this.$ref.path || "";
|
|
127
|
+
|
|
128
|
+
const targetRef = this.path.replace(path, "");
|
|
129
|
+
const targetFound = Pointer.join("", found);
|
|
130
|
+
const parentPath = pathFromRoot?.replace(path, "");
|
|
131
|
+
|
|
132
|
+
throw new MissingPointerError(token, decodeURI(this.originalPath), targetRef, targetFound, parentPath);
|
|
124
133
|
} else {
|
|
125
134
|
this.value = this.value[token];
|
|
126
135
|
}
|
|
136
|
+
|
|
137
|
+
found.push(token);
|
|
127
138
|
}
|
|
128
139
|
|
|
129
140
|
// Resolve the final value
|
package/lib/util/errors.ts
CHANGED
|
@@ -123,8 +123,17 @@ export class UnmatchedResolverError extends JSONParserError {
|
|
|
123
123
|
export class MissingPointerError extends JSONParserError {
|
|
124
124
|
code = "EMISSINGPOINTER" as JSONParserErrorType;
|
|
125
125
|
name = "MissingPointerError";
|
|
126
|
-
|
|
126
|
+
public targetToken: any;
|
|
127
|
+
public targetRef: string;
|
|
128
|
+
public targetFound: string;
|
|
129
|
+
public parentPath: string;
|
|
130
|
+
constructor(token: any, path: any, targetRef: any, targetFound: any, parentPath: any) {
|
|
127
131
|
super(`Missing $ref pointer "${getHash(path)}". Token "${token}" does not exist.`, stripHash(path));
|
|
132
|
+
|
|
133
|
+
this.targetToken = token;
|
|
134
|
+
this.targetRef = targetRef;
|
|
135
|
+
this.targetFound = targetFound;
|
|
136
|
+
this.parentPath = parentPath;
|
|
128
137
|
}
|
|
129
138
|
}
|
|
130
139
|
|
package/lib/util/plugins.ts
CHANGED
|
@@ -45,8 +45,7 @@ export function sort(plugins: Plugin[]) {
|
|
|
45
45
|
});
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
export interface PluginResult<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>> {
|
|
48
|
+
export interface PluginResult<S extends object = JSONSchema> {
|
|
50
49
|
plugin: Plugin;
|
|
51
50
|
result?: string | Buffer | S;
|
|
52
51
|
error?: any;
|
|
@@ -67,10 +66,10 @@ export async function run<S extends object = JSONSchema, O extends ParserOptions
|
|
|
67
66
|
$refs: $Refs<S, O>,
|
|
68
67
|
) {
|
|
69
68
|
let plugin: Plugin;
|
|
70
|
-
let lastError: PluginResult<S
|
|
69
|
+
let lastError: PluginResult<S>;
|
|
71
70
|
let index = 0;
|
|
72
71
|
|
|
73
|
-
return new Promise<PluginResult<S
|
|
72
|
+
return new Promise<PluginResult<S>>((resolve, reject) => {
|
|
74
73
|
runNextPlugin();
|
|
75
74
|
|
|
76
75
|
function runNextPlugin() {
|
|
@@ -97,7 +96,7 @@ export async function run<S extends object = JSONSchema, O extends ParserOptions
|
|
|
97
96
|
}
|
|
98
97
|
}
|
|
99
98
|
|
|
100
|
-
function callback(err: PluginResult<S
|
|
99
|
+
function callback(err: PluginResult<S>["error"], result: PluginResult<S>["result"]) {
|
|
101
100
|
if (err) {
|
|
102
101
|
onError(err);
|
|
103
102
|
} else {
|
|
@@ -105,7 +104,7 @@ export async function run<S extends object = JSONSchema, O extends ParserOptions
|
|
|
105
104
|
}
|
|
106
105
|
}
|
|
107
106
|
|
|
108
|
-
function onSuccess(result: PluginResult<S
|
|
107
|
+
function onSuccess(result: PluginResult<S>["result"]) {
|
|
109
108
|
// console.log(' success');
|
|
110
109
|
resolve({
|
|
111
110
|
plugin,
|
|
@@ -113,7 +112,7 @@ export async function run<S extends object = JSONSchema, O extends ParserOptions
|
|
|
113
112
|
});
|
|
114
113
|
}
|
|
115
114
|
|
|
116
|
-
function onError(error: PluginResult<S
|
|
115
|
+
function onError(error: PluginResult<S>["error"]) {
|
|
117
116
|
// console.log(' %s', err.message || err);
|
|
118
117
|
lastError = {
|
|
119
118
|
plugin,
|
package/lib/util/url.ts
CHANGED
|
@@ -26,10 +26,11 @@ export const parse = (u: string | URL) => new URL(u);
|
|
|
26
26
|
* @returns
|
|
27
27
|
*/
|
|
28
28
|
export function resolve(from: string, to: string) {
|
|
29
|
-
|
|
29
|
+
// we use a non-existent URL to check if its a relative URL
|
|
30
|
+
const fromUrl = new URL(convertPathToPosix(from), "https://aaa.nonexistanturl.com");
|
|
30
31
|
const resolvedUrl = new URL(convertPathToPosix(to), fromUrl);
|
|
31
32
|
const endSpaces = to.match(/(\s*)$/)?.[1] || "";
|
|
32
|
-
if (resolvedUrl.
|
|
33
|
+
if (resolvedUrl.hostname === "aaa.nonexistanturl.com") {
|
|
33
34
|
// `from` is a relative URL.
|
|
34
35
|
const { pathname, search, hash } = resolvedUrl;
|
|
35
36
|
return pathname + search + hash + endSpaces;
|
package/package.json
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@apidevtools/json-schema-ref-parser",
|
|
3
|
-
"version": "11.
|
|
3
|
+
"version": "11.9.0",
|
|
4
4
|
"description": "Parse, Resolve, and Dereference JSON Schema $ref pointers",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"prepublishOnly": "yarn build",
|
|
7
|
+
"lint": "eslint lib",
|
|
8
|
+
"build": "rimraf dist && tsc",
|
|
9
|
+
"typecheck": "tsc --noEmit",
|
|
10
|
+
"prettier": "prettier --write \"**/*.+(js|jsx|ts|tsx|har||json|css|md)\"",
|
|
11
|
+
"test": "vitest --coverage",
|
|
12
|
+
"test:node": "yarn test",
|
|
13
|
+
"test:browser": "cross-env BROWSER=\"true\" yarn test",
|
|
14
|
+
"test:update": "vitest -u",
|
|
15
|
+
"test:watch": "vitest -w"
|
|
16
|
+
},
|
|
5
17
|
"keywords": [
|
|
6
18
|
"json",
|
|
7
19
|
"schema",
|
|
@@ -54,42 +66,30 @@
|
|
|
54
66
|
"dist",
|
|
55
67
|
"cjs"
|
|
56
68
|
],
|
|
57
|
-
"scripts": {
|
|
58
|
-
"prepublishOnly": "yarn build",
|
|
59
|
-
"lint": "eslint lib",
|
|
60
|
-
"build": "rimraf dist && tsc",
|
|
61
|
-
"typecheck": "tsc --noEmit",
|
|
62
|
-
"prettier": "prettier --write \"**/*.+(js|jsx|ts|tsx|har||json|css|md)\"",
|
|
63
|
-
"test": "vitest --coverage",
|
|
64
|
-
"test:node": "yarn test",
|
|
65
|
-
"test:browser": "cross-env BROWSER=\"true\" yarn test",
|
|
66
|
-
"test:update": "vitest -u",
|
|
67
|
-
"test:watch": "vitest -w"
|
|
68
|
-
},
|
|
69
69
|
"devDependencies": {
|
|
70
|
-
"@eslint/compat": "^1.2.
|
|
71
|
-
"@eslint/js": "^9.
|
|
72
|
-
"@types/eslint": "9.6.1",
|
|
70
|
+
"@eslint/compat": "^1.2.5",
|
|
71
|
+
"@eslint/js": "^9.18.0",
|
|
72
|
+
"@types/eslint": "^9.6.1",
|
|
73
73
|
"@types/js-yaml": "^4.0.9",
|
|
74
74
|
"@types/node": "^22",
|
|
75
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
76
|
-
"@typescript-eslint/parser": "^8.
|
|
77
|
-
"@vitest/coverage-v8": "^
|
|
75
|
+
"@typescript-eslint/eslint-plugin": "^8.21.0",
|
|
76
|
+
"@typescript-eslint/parser": "^8.21.0",
|
|
77
|
+
"@vitest/coverage-v8": "^3.0.4",
|
|
78
78
|
"cross-env": "^7.0.3",
|
|
79
|
-
"eslint": "^9.
|
|
80
|
-
"eslint-config-prettier": "^
|
|
79
|
+
"eslint": "^9.18.0",
|
|
80
|
+
"eslint-config-prettier": "^10.0.1",
|
|
81
81
|
"eslint-config-standard": "^17.1.0",
|
|
82
82
|
"eslint-plugin-import": "^2.31.0",
|
|
83
|
-
"eslint-plugin-prettier": "^5.2.
|
|
83
|
+
"eslint-plugin-prettier": "^5.2.3",
|
|
84
84
|
"eslint-plugin-promise": "^7.2.1",
|
|
85
85
|
"eslint-plugin-unused-imports": "^4.1.4",
|
|
86
|
-
"globals": "^15.
|
|
87
|
-
"jsdom": "^
|
|
86
|
+
"globals": "^15.14.0",
|
|
87
|
+
"jsdom": "^26.0.0",
|
|
88
88
|
"prettier": "^3.4.2",
|
|
89
89
|
"rimraf": "^6.0.1",
|
|
90
|
-
"typescript": "^5.7.
|
|
91
|
-
"typescript-eslint": "^8.
|
|
92
|
-
"vitest": "^
|
|
90
|
+
"typescript": "^5.7.3",
|
|
91
|
+
"typescript-eslint": "^8.21.0",
|
|
92
|
+
"vitest": "^3.0.4"
|
|
93
93
|
},
|
|
94
94
|
"dependencies": {
|
|
95
95
|
"@jsdevtools/ono": "^7.1.3",
|