Skip to content

Commit

Permalink
feat: surfacing circular $refs in a new circularRefs property on th…
Browse files Browse the repository at this point in the history
…e parser (#58)

* feat: surfacing circular $refs in a new `circularRefs` property on the parser

* fix: broken tests in chrome
  • Loading branch information
erunion authored Dec 7, 2022
1 parent 6b1eb08 commit 0986c9a
Show file tree
Hide file tree
Showing 30 changed files with 384 additions and 60 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ import $RefParser from "@readme/json-schema-ref-parser";

* Forces YAML to conform to JSON-compatible types. https://github.com/APIDevTools/json-schema-ref-parser/pull/247
* 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
* Exposes a new `$refs.circularRefs` property containing an array of any circular `$ref` pointers that may exist within the schema definition.

## Browser support
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).
Expand Down
17 changes: 17 additions & 0 deletions docs/refs.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This object is a map of JSON References and their resolved values. It also has

##### Properties
- [`circular`](#circular)
- [`circularRefs`](#circularRefs)

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


### `circularRefs`

- **Type:** `array`

This property contains an array of any [circular references](README.md#circular-refs) that may the schema contains.

```javascript
let parser = new $RefParser();
await parser.dereference("my-schema.json");

if (parser.$refs.circular) {
console.log(parser.$refs.circularRefs);
}
```


### `paths([types])`

- **types** (_optional_) - `string` (one or more)<br>
Expand Down
3 changes: 3 additions & 0 deletions lib/dereference.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,11 @@ function dereference$Ref($ref, path, pathFromRoot, parents, processedObjects, de
*/
function foundCircularReference(keyPath, $refs, options) {
$refs.circular = true;
$refs.circularRefs.push(keyPath);

if (!options.dereference.circular) {
throw ono.reference(`Circular $ref pointer found at ${keyPath}`);
}

return true;
}
5 changes: 5 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,11 @@ declare namespace $RefParser {
*/
public circular: boolean;

/**
* This property contains any circular references that may be within the schema.
*/
public circularRefs: string[];

/**
* Returns the paths/URLs of all the files in your schema (including the main schema file).
*
Expand Down
7 changes: 7 additions & 0 deletions lib/refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ function $Refs() {
*/
this.circular = false;

/**
* Contains any circular references that may be present within the schema.
*
* @type {array}
*/
this.circularRefs = [];

/**
* A map of paths/urls to {@link $Ref} objects
*
Expand Down
Loading

0 comments on commit 0986c9a

Please sign in to comment.