Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

Commit 0986c9a

Browse files
authored
feat: surfacing circular $refs in a new circularRefs property on the parser (#58)
* feat: surfacing circular $refs in a new `circularRefs` property on the parser * fix: broken tests in chrome
1 parent 6b1eb08 commit 0986c9a

File tree

30 files changed

+384
-60
lines changed

30 files changed

+384
-60
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ import $RefParser from "@readme/json-schema-ref-parser";
9797

9898
* Forces YAML to conform to JSON-compatible types. https://github.com/APIDevTools/json-schema-ref-parser/pull/247
9999
* Improved support for OpenAPI 3.1 definitions where `$ref` pointers may live alongside a `description` property. https://github.com/readmeio/json-schema-ref-parser/pull/2
100+
* Exposes a new `$refs.circularRefs` property containing an array of any circular `$ref` pointers that may exist within the schema definition.
100101

101102
## Browser support
102103
JSON Schema $Ref Parser supports recent versions of every major web browser. Older browsers may require [Babel](https://babeljs.io/) and/or [polyfills](https://babeljs.io/docs/en/next/babel-polyfill).

docs/refs.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This object is a map of JSON References and their resolved values. It also has
88

99
##### Properties
1010
- [`circular`](#circular)
11+
- [`circularRefs`](#circularRefs)
1112

1213
##### Methods
1314
- [`paths()`](#pathstypes)
@@ -33,6 +34,22 @@ if (parser.$refs.circular) {
3334
```
3435

3536

37+
### `circularRefs`
38+
39+
- **Type:** `array`
40+
41+
This property contains an array of any [circular references](README.md#circular-refs) that may the schema contains.
42+
43+
```javascript
44+
let parser = new $RefParser();
45+
await parser.dereference("my-schema.json");
46+
47+
if (parser.$refs.circular) {
48+
console.log(parser.$refs.circularRefs);
49+
}
50+
```
51+
52+
3653
### `paths([types])`
3754

3855
- **types** (_optional_) - `string` (one or more)<br>

lib/dereference.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,11 @@ function dereference$Ref($ref, path, pathFromRoot, parents, processedObjects, de
254254
*/
255255
function foundCircularReference(keyPath, $refs, options) {
256256
$refs.circular = true;
257+
$refs.circularRefs.push(keyPath);
258+
257259
if (!options.dereference.circular) {
258260
throw ono.reference(`Circular $ref pointer found at ${keyPath}`);
259261
}
262+
260263
return true;
261264
}

lib/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,11 @@ declare namespace $RefParser {
361361
*/
362362
public circular: boolean;
363363

364+
/**
365+
* This property contains any circular references that may be within the schema.
366+
*/
367+
public circularRefs: string[];
368+
364369
/**
365370
* Returns the paths/URLs of all the files in your schema (including the main schema file).
366371
*

lib/refs.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ function $Refs() {
1616
*/
1717
this.circular = false;
1818

19+
/**
20+
* Contains any circular references that may be present within the schema.
21+
*
22+
* @type {array}
23+
*/
24+
this.circularRefs = [];
25+
1926
/**
2027
* A map of paths/urls to {@link $Ref} objects
2128
*

0 commit comments

Comments
 (0)