Skip to content

Commit 50c6ee3

Browse files
committed
fix: inline internal JSON Pointer refs under #/paths/ for OpenAPI bundling
- Add shouldInlineInternal helper to determine when internal refs should be copied inline - Internal refs under #/paths/ are now dereferenced (copied) instead of left as - Components/schemas, definitions, and declarations still preserved as refs
1 parent f05ba07 commit 50c6ee3

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/bundle.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ export interface InventoryEntry {
2323
value: any;
2424
}
2525

26+
/**
27+
* Determines whether an internal $ref should be inlined (copied) rather than left as a $ref.
28+
* Currently inlines OpenAPI path items ("#/paths/..."), while keeping components/definitions/declarations as refs.
29+
*/
30+
const shouldInlineInternal = (entry: InventoryEntry) => {
31+
if (entry.external) {
32+
return false;
33+
}
34+
const h = entry.hash as string | undefined;
35+
if (!h || h === "#") {
36+
return false;
37+
}
38+
if (h.startsWith("#/components/schemas") || h.indexOf("/definitions") !== -1 || h.startsWith("#/declarations")) {
39+
return false;
40+
}
41+
return h.startsWith("#/paths/");
42+
};
43+
2644
/**
2745
* TODO
2846
*/
@@ -300,7 +318,7 @@ function remap(inventory: InventoryEntry[]) {
300318
for (const entry of inventory) {
301319
// console.log('Re-mapping $ref pointer "%s" at %s', entry.$ref.$ref, entry.pathFromRoot);
302320

303-
if (!entry.external) {
321+
if (!entry.external && !shouldInlineInternal(entry)) {
304322
// This $ref already resolves to the main JSON Schema file
305323
entry.$ref.$ref = entry.hash;
306324
} else if (entry.file === file && entry.hash === hash) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
],
4343
"scripts": {
4444
"build": "rimraf dist && tsc",
45+
"dev": "rimraf dist && tsc --watch",
4546
"lint": "eslint lib",
4647
"prepublishOnly": "yarn build",
4748
"prettier": "prettier --write \"**/*.+(js|jsx|ts|tsx|har||json|css|md)\"",

0 commit comments

Comments
 (0)