Skip to content

Commit

Permalink
Merge 0dea6eb into add5f77
Browse files Browse the repository at this point in the history
  • Loading branch information
toomuchdesign committed Mar 5, 2024
2 parents add5f77 + 0dea6eb commit 31baf86
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ The `dereference` options control how JSON Schema $Ref Parser will dereference `
|:---------------------|:-------------------|:------------
|`circular`|`boolean` or `"ignore"`|Determines whether [circular `$ref` pointers](README.md#circular-refs) are handled.<br><br>If set to `false`, then a `ReferenceError` will be thrown if the schema contains any circular references.<br><br> If set to `"ignore"`, then circular references will simply be ignored. No error will be thrown, but the [`$Refs.circular`](refs.md#circular) property will still be set to `true`.
|`excludedPathMatcher`|`(string) => boolean`|A function, called for each path, which can return true to stop this path and all subpaths from being dereferenced further. This is useful in schemas where some subpaths contain literal `$ref` keys that should not be dereferenced.
|`onDereference`|`(string, JSONSchemaObjectType) => void`|A function, called immediately after dereferencing, with the resolved JSON Schema value and the `$ref` being dereferenced.
|`onDereference`|`(string, JSONSchemaObjectType, JSONSchemaObjectType, string) => void`|A function, called immediately after dereferencing, with: the resolved JSON Schema value, the `$ref` being dereferenced, the object holding the dereferenced prop, the dereferenced prop name.
2 changes: 1 addition & 1 deletion lib/dereference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function crawl(
if (obj[key] !== dereferenced.value) {
obj[key] = dereferenced.value;
if (options.dereference.onDereference) {
options.dereference.onDereference(value.$ref, obj[key]);
options.dereference.onDereference(value.$ref, obj[key], obj, key);
}
}
} else {
Expand Down
8 changes: 5 additions & 3 deletions lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ interface $RefParserOptions {
/**
* Callback invoked during dereferencing.
*
* @argument {string} path The path being dereferenced (ie. the `$ref` string).
* @argument {JSONSchemaObject} object The JSON-Schema that the `$ref` resolved to.
* @argument {string} path - The path being dereferenced (ie. the `$ref` string)
* @argument {JSONSchemaObject} value - The JSON-Schema that the `$ref` resolved to
* @argument {JSONSchemaObject} parent - The parent of the dereferenced object
* @argument {string} parentPropName - The prop name of the parent object whose value was dereferenced
*/
onDereference?(path: string, value: JSONSchemaObject): void;
onDereference?(path: string, value: JSONSchemaObject, parent?: JSONSchemaObject, parentPropName?: string): void;

/**
* Whether a reference should resolve relative to its directory/path, or from the cwd
Expand Down
33 changes: 29 additions & 4 deletions test/specs/dereference-callback/dereference-callback.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,40 @@ describe("Schema with a $ref", () => {
const schema = pathUtils.rel("test/specs/dereference-callback/dereference-callback.yaml");
const options = {
dereference: {
onDereference(path: any, object: any) {
calls.push({ path, object });
onDereference(path, value, parent, parentPropName) {
calls.push({ path, value, parent, parentPropName });
},
},
} as Options;
await parser.dereference(schema, options);

expect(calls).to.deep.equal([
{ path: "#/definitions/b", object: { $ref: "#/definitions/a" } },
{ path: "#/definitions/a", object: { $ref: "#/definitions/a" } },
{
path: "#/definitions/b",
value: { $ref: "#/definitions/a" },
parent: {
a: {
$ref: "#/definitions/a",
},
b: {
$ref: "#/definitions/a",
},
},
parentPropName: "a",
},
{
path: "#/definitions/a",
value: { $ref: "#/definitions/a" },
parent: {
c: {
type: "string",
},
d: {
$ref: "#/definitions/a",
},
},
parentPropName: "d",
},
]);
});
});

0 comments on commit 31baf86

Please sign in to comment.