fork of hey-api/openapi-ts because I need some additional things
1# JSON Schema $Ref Parser
2
3#### Parse, Resolve, and Dereference JSON Schema $ref pointers
4
5## Installation
6
7Install using [npm](https://docs.npmjs.com/about-npm/):
8
9```bash
10npm install @hey-api/json-schema-ref-parser
11yarn add @hey-api/json-schema-ref-parser
12bun add @hey-api/json-schema-ref-parser
13```
14
15## The Problem:
16
17You've got a JSON Schema with `$ref` pointers to other files and/or URLs. Maybe you know all the referenced files ahead
18of time. Maybe you don't. Maybe some are local files, and others are remote URLs. Maybe they are a mix of JSON and YAML
19format. Maybe some of the files contain cross-references to each other.
20
21```json
22{
23 "definitions": {
24 "person": {
25 // references an external file
26 "$ref": "schemas/people/Bruce-Wayne.json"
27 },
28 "place": {
29 // references a sub-schema in an external file
30 "$ref": "schemas/places.yaml#/definitions/Gotham-City"
31 },
32 "thing": {
33 // references a URL
34 "$ref": "http://wayne-enterprises.com/things/batmobile"
35 },
36 "color": {
37 // references a value in an external file via an internal reference
38 "$ref": "#/definitions/thing/properties/colors/black-as-the-night"
39 }
40 }
41}
42```
43
44## The Solution:
45
46JSON Schema $Ref Parser is a full [JSON Reference](https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03)
47and [JSON Pointer](https://tools.ietf.org/html/rfc6901) implementation that crawls even the most
48complex [JSON Schemas](http://json-schema.org/latest/json-schema-core.html) and gives you simple, straightforward
49JavaScript objects.
50
51- Use **JSON** or **YAML** schemas — or even a mix of both!
52- Supports `$ref` pointers to external files and URLs, as well as custom sources such as databases
53- Can bundle multiple files into a single schema that only has _internal_ `$ref` pointers
54- Can dereference your schema, producing a plain-old JavaScript object that's easy to work with
55- Supports circular references, nested references,
56 back-references, and cross-references between files
57- Maintains object reference equality — `$ref` pointers to the same value always resolve to the same object
58 instance
59- Compatible with Node LTS and beyond, and all major web browsers on Windows, Mac, and Linux
60
61### New in this fork (@hey-api)
62
63- **Multiple inputs with `bundleMany`**: Merge and bundle several OpenAPI/JSON Schema inputs (files, URLs, or raw objects) into a single schema. Components are prefixed to avoid name collisions, paths are namespaced on conflict, and `$ref`s are rewritten accordingly.
64
65```javascript
66import { $RefParser } from '@hey-api/json-schema-ref-parser';
67
68const parser = new $RefParser();
69const merged = await parser.bundleMany({
70 pathOrUrlOrSchemas: [
71 './specs/a.yaml',
72 'https://example.com/b.yaml',
73 { openapi: '3.1.0', info: { title: 'Inline' }, paths: {} },
74 ],
75});
76
77// merged.components.* will contain prefixed names like a_<name>, b_<name>, etc.
78```
79
80- **Dereference hooks**: Fine-tune dereferencing with `excludedPathMatcher(path) => boolean` to skip subpaths and `onDereference(path, value, parent, parentPropName)` to observe replacements.
81
82```javascript
83const parser = new $RefParser();
84parser.options.dereference.excludedPathMatcher = (p) => p.includes('/example/');
85parser.options.dereference.onDereference = (p, v) => {
86 // inspect p / v as needed
87};
88await parser.dereference({ pathOrUrlOrSchema: './openapi.yaml' });
89```
90
91- **Smart input resolution**: You can pass a file path, URL, or raw schema object. If a raw schema includes `$id`, it is used as the base URL for resolving relative `$ref`s.
92
93```javascript
94await new $RefParser().bundle({
95 pathOrUrlOrSchema: {
96 $id: 'https://api.example.com/openapi.json',
97 openapi: '3.1.0',
98 paths: {
99 '/ping': { get: { responses: { 200: { description: 'ok' } } } },
100 },
101 },
102});
103```