@amritk/resolve-refs 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/resolve-refs-from-file.js +36 -18
- package/dist/resolve-refs.js +30 -10
- package/package.json +1 -1
|
@@ -166,15 +166,17 @@ const collectRefTargets = (node, out) => {
|
|
|
166
166
|
}
|
|
167
167
|
const obj = node;
|
|
168
168
|
if (typeof obj['$ref'] === 'string') {
|
|
169
|
-
// A `$ref` object's siblings are ignored on resolution, so we do not recurse
|
|
170
|
-
// into them — mirroring resolveAt's behaviour.
|
|
171
169
|
const { filePart } = splitRef(obj['$ref']);
|
|
172
170
|
if (filePart !== '')
|
|
173
171
|
out.add(filePart);
|
|
174
|
-
return out;
|
|
175
172
|
}
|
|
176
|
-
|
|
173
|
+
// Recurse into every key — including a `$ref` node's siblings, which apply
|
|
174
|
+
// alongside the referenced schema (2020-12) and may carry their own refs.
|
|
175
|
+
for (const key of Object.keys(obj)) {
|
|
176
|
+
if (key === '$ref')
|
|
177
|
+
continue;
|
|
177
178
|
collectRefTargets(obj[key], out);
|
|
179
|
+
}
|
|
178
180
|
return out;
|
|
179
181
|
};
|
|
180
182
|
/**
|
|
@@ -219,24 +221,40 @@ const resolveAt = (node, baseLocation, docCache, refCache, origins) => {
|
|
|
219
221
|
const targetLocation = filePart === '' ? baseLocation : joinLocation(baseLocation, filePart);
|
|
220
222
|
const targetRoot = docCache.get(targetLocation) ?? {};
|
|
221
223
|
const cacheKey = `${targetLocation}#${pointer}`;
|
|
224
|
+
let resolved;
|
|
222
225
|
if (refCache.has(cacheKey)) {
|
|
223
226
|
const cached = refCache.get(cacheKey);
|
|
224
|
-
|
|
227
|
+
resolved = cached === CYCLE ? {} : cached;
|
|
225
228
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
origins
|
|
229
|
+
else {
|
|
230
|
+
refCache.set(cacheKey, CYCLE);
|
|
231
|
+
const target = pointer ? getByPointer(targetRoot, pointer) : targetRoot;
|
|
232
|
+
resolved = resolveAt(target, targetLocation, docCache, refCache, origins);
|
|
233
|
+
refCache.set(cacheKey, resolved);
|
|
234
|
+
// Stamp the inlined node with where it was defined so a consumer can map a
|
|
235
|
+
// resolved-tree node back to its source document/path. Only objects/arrays are
|
|
236
|
+
// stamped (primitives can't key the map). First-write-wins: resolution recurses
|
|
237
|
+
// to the deepest `$ref` before returning, so the *definition* site stamps first;
|
|
238
|
+
// an outer ref that merely points (transitively) at the same object must not
|
|
239
|
+
// overwrite it with an intermediate location.
|
|
240
|
+
if (origins && resolved !== null && typeof resolved === 'object' && !origins.has(resolved)) {
|
|
241
|
+
origins.set(resolved, { location: targetLocation, pointer: pointerToPath(pointer) });
|
|
242
|
+
}
|
|
238
243
|
}
|
|
239
|
-
|
|
244
|
+
// Keywords sibling to `$ref` apply alongside the referenced schema (2020-12),
|
|
245
|
+
// so preserve them by combining both in an `allOf` rather than dropping them.
|
|
246
|
+
const siblingKeys = Object.keys(obj).filter((key) => key !== '$ref');
|
|
247
|
+
if (siblingKeys.length === 0)
|
|
248
|
+
return resolved;
|
|
249
|
+
const siblings = {};
|
|
250
|
+
for (const key of siblingKeys)
|
|
251
|
+
siblings[key] = resolveAt(obj[key], baseLocation, docCache, refCache, origins);
|
|
252
|
+
const existingAllOf = Array.isArray(siblings['allOf']) ? siblings['allOf'] : [];
|
|
253
|
+
const merged = { ...siblings, allOf: [...existingAllOf, resolved] };
|
|
254
|
+
// Stamp the wrapper too, so origin lookups resolve for a `$ref`-with-siblings node.
|
|
255
|
+
if (origins && !origins.has(merged))
|
|
256
|
+
origins.set(merged, { location: targetLocation, pointer: pointerToPath(pointer) });
|
|
257
|
+
return merged;
|
|
240
258
|
}
|
|
241
259
|
const result = {};
|
|
242
260
|
for (const key of Object.keys(obj)) {
|
package/dist/resolve-refs.js
CHANGED
|
@@ -19,20 +19,40 @@ const resolveInternal = (node, root, cache, origins) => {
|
|
|
19
19
|
const ref = obj['$ref'];
|
|
20
20
|
if (!ref.startsWith('#'))
|
|
21
21
|
return obj; // external ref — leave as-is
|
|
22
|
+
let target;
|
|
22
23
|
if (cache.has(ref)) {
|
|
23
24
|
const cached = cache.get(ref);
|
|
24
|
-
|
|
25
|
+
target = cached === CYCLE ? {} : cached;
|
|
25
26
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
origins
|
|
27
|
+
else {
|
|
28
|
+
cache.set(ref, CYCLE);
|
|
29
|
+
target = resolveInternal(getByPointer(root, ref.slice(1)), root, cache, origins);
|
|
30
|
+
cache.set(ref, target);
|
|
31
|
+
// Stamp the inlined node with the path it was defined at (see resolveAt).
|
|
32
|
+
// First-write-wins so the deepest definition stamps before any outer ref that
|
|
33
|
+
// transitively points at the same object. Primitives can't key the map.
|
|
34
|
+
if (origins && target !== null && typeof target === 'object' && !origins.has(target)) {
|
|
35
|
+
origins.set(target, { location: '', pointer: pointerToPath(ref.slice(1)) });
|
|
36
|
+
}
|
|
34
37
|
}
|
|
35
|
-
|
|
38
|
+
// Per JSON Schema 2020-12, keywords sibling to `$ref` are *not* ignored: they
|
|
39
|
+
// apply alongside the referenced schema. Preserve them by combining both in an
|
|
40
|
+
// `allOf` (so a constraint present on both sides is never silently dropped)
|
|
41
|
+
// rather than returning the bare target. The cache always stores the
|
|
42
|
+
// sibling-free target so each occurrence applies its own siblings.
|
|
43
|
+
const siblingKeys = Object.keys(obj).filter((key) => key !== '$ref');
|
|
44
|
+
if (siblingKeys.length === 0)
|
|
45
|
+
return target;
|
|
46
|
+
const siblings = {};
|
|
47
|
+
for (const key of siblingKeys)
|
|
48
|
+
siblings[key] = resolveInternal(obj[key], root, cache, origins);
|
|
49
|
+
const existingAllOf = Array.isArray(siblings['allOf']) ? siblings['allOf'] : [];
|
|
50
|
+
const merged = { ...siblings, allOf: [...existingAllOf, target] };
|
|
51
|
+
// Stamp the wrapper too, so a consumer mapping the resolved node back to its
|
|
52
|
+
// origin finds one for a `$ref`-with-siblings node (not only the inner target).
|
|
53
|
+
if (origins && !origins.has(merged))
|
|
54
|
+
origins.set(merged, { location: '', pointer: pointerToPath(ref.slice(1)) });
|
|
55
|
+
return merged;
|
|
36
56
|
}
|
|
37
57
|
const result = {};
|
|
38
58
|
for (const key of Object.keys(obj)) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amritk/resolve-refs",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "Resolve and inline JSON Schema / OpenAPI $refs — internal, cross-file, and remote — with session caching and a default-deny SSRF guard.",
|
|
5
5
|
"module": "./dist/index.js",
|
|
6
6
|
"type": "module",
|