Skip to content

Commit e043602

Browse files
authored
Merge pull request #14 from hey-api/feat--multiple-input-support
feat: multiple input support by `bundleMany` method
2 parents 0f7f409 + f5f2c67 commit e043602

File tree

5 files changed

+434
-23
lines changed

5 files changed

+434
-23
lines changed

README.md

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
Install using [npm](https://docs.npmjs.com/about-npm/):
1616

1717
```bash
18-
npm install @apidevtools/json-schema-ref-parser
19-
yarn add @apidevtools/json-schema-ref-parser
20-
bun add @apidevtools/json-schema-ref-parser
18+
npm install @hey-api/json-schema-ref-parser
19+
yarn add @hey-api/json-schema-ref-parser
20+
bun add @hey-api/json-schema-ref-parser
2121
```
2222

2323
## The Problem:
@@ -69,21 +69,61 @@ JavaScript objects.
6969
## Example
7070

7171
```javascript
72-
import $RefParser from "@apidevtools/json-schema-ref-parser";
72+
import { $RefParser } from "@hey-api/json-schema-ref-parser";
7373

7474
try {
75-
await $RefParser.dereference(mySchema);
76-
// note - by default, mySchema is modified in place, and the returned value is a reference to the same object
77-
console.log(mySchema.definitions.person.properties.firstName);
78-
79-
// if you want to avoid modifying the original schema, you can disable the `mutateInputSchema` option
80-
let clonedSchema = await $RefParser.dereference(mySchema, { mutateInputSchema: false });
81-
console.log(clonedSchema.definitions.person.properties.firstName);
75+
const parser = new $RefParser();
76+
await parser.dereference({ pathOrUrlOrSchema: mySchema });
77+
console.log(parser.schema.definitions.person.properties.firstName);
8278
} catch (err) {
8379
console.error(err);
8480
}
8581
```
8682

83+
### New in this fork (@hey-api)
84+
85+
- **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.
86+
87+
```javascript
88+
import { $RefParser } from "@hey-api/json-schema-ref-parser";
89+
90+
const parser = new $RefParser();
91+
const merged = await parser.bundleMany({
92+
pathOrUrlOrSchemas: [
93+
"./specs/a.yaml",
94+
"https://example.com/b.yaml",
95+
{ openapi: "3.1.0", info: { title: "Inline" }, paths: {} },
96+
],
97+
});
98+
99+
// merged.components.* will contain prefixed names like a_<name>, b_<name>, etc.
100+
```
101+
102+
- **Dereference hooks**: Fine-tune dereferencing with `excludedPathMatcher(path) => boolean` to skip subpaths and `onDereference(path, value, parent, parentPropName)` to observe replacements.
103+
104+
```javascript
105+
const parser = new $RefParser();
106+
parser.options.dereference.excludedPathMatcher = (p) => p.includes("/example/");
107+
parser.options.dereference.onDereference = (p, v) => {
108+
// inspect p / v as needed
109+
};
110+
await parser.dereference({ pathOrUrlOrSchema: "./openapi.yaml" });
111+
```
112+
113+
- **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.
114+
115+
```javascript
116+
await new $RefParser().bundle({
117+
pathOrUrlOrSchema: {
118+
$id: "https://api.example.com/openapi.json",
119+
openapi: "3.1.0",
120+
paths: {
121+
"/ping": { get: { responses: { 200: { description: "ok" } } } },
122+
},
123+
},
124+
});
125+
```
126+
87127
## Polyfills
88128

89129
If you are using Node.js < 18, you'll need a polyfill for `fetch`,

lib/bundle.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,21 @@ function remap(parser: $RefParser, inventory: InventoryEntry[]) {
607607

608608
let defName = namesForPrefix.get(targetKey);
609609
if (!defName) {
610-
const proposed = `${baseName(entry.file)}_${lastToken(entry.hash)}`;
610+
// If the external file is one of the original input sources, prefer its assigned prefix
611+
let proposedBase = baseName(entry.file);
612+
try {
613+
const parserAny: any = parser as any;
614+
if (parserAny && parserAny.sourcePathToPrefix && typeof parserAny.sourcePathToPrefix.get === "function") {
615+
const withoutHash = (entry.file || "").split("#")[0];
616+
const mapped = parserAny.sourcePathToPrefix.get(withoutHash);
617+
if (mapped && typeof mapped === "string") {
618+
proposedBase = mapped;
619+
}
620+
}
621+
} catch {
622+
// Ignore errors
623+
}
624+
const proposed = `${proposedBase}_${lastToken(entry.hash)}`;
611625
defName = uniqueName(container, proposed);
612626
namesForPrefix.set(targetKey, defName);
613627
// Store the resolved value under the container

0 commit comments

Comments
 (0)