Skip to content

Commit f26d8c7

Browse files
committed
Revert "fix: support 2020-12 anchors and ref siblings"
This reverts commit f3f7e35.
1 parent f3f7e35 commit f26d8c7

File tree

9 files changed

+59
-235
lines changed

9 files changed

+59
-235
lines changed

lib/bundle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function crawl<S extends object = JSONSchema, O extends ParserOptions<S> = Parse
9797

9898
if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj) && !isExcludedPath(pathFromRoot)) {
9999
const currentScopeBase = dynamicIdScope ? getSchemaBasePath(scopeBase, obj) : scopeBase;
100-
if ($Ref.isAllowed$Ref(obj, options, dynamicIdScope)) {
100+
if ($Ref.isAllowed$Ref(obj)) {
101101
inventory$Ref(parent, key, path, currentScopeBase, dynamicIdScope, pathFromRoot, indirections, inventory, $refs, options);
102102
} else {
103103
// Crawl the object in a specific order that's optimized for bundling.
@@ -122,7 +122,7 @@ function crawl<S extends object = JSONSchema, O extends ParserOptions<S> = Parse
122122
const keyPathFromRoot = Pointer.join(pathFromRoot, key);
123123
const value = obj[key];
124124

125-
if ($Ref.isAllowed$Ref(value, options, dynamicIdScope)) {
125+
if ($Ref.isAllowed$Ref(value)) {
126126
const valueScopeBase = dynamicIdScope ? getSchemaBasePath(currentScopeBase, value) : currentScopeBase;
127127
inventory$Ref(
128128
obj,

lib/dereference.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function crawl<S extends object = JSONSchema, O extends ParserOptions<S> = Parse
9494
processedObjects.add(obj);
9595
const currentScopeBase = dynamicIdScope ? getSchemaBasePath(scopeBase, obj) : scopeBase;
9696

97-
if ($Ref.isAllowed$Ref(obj, options, dynamicIdScope)) {
97+
if ($Ref.isAllowed$Ref(obj, options)) {
9898
dereferenced = dereference$Ref(
9999
obj,
100100
path,
@@ -125,7 +125,7 @@ function crawl<S extends object = JSONSchema, O extends ParserOptions<S> = Parse
125125
const value = obj[key];
126126
let circular;
127127

128-
if ($Ref.isAllowed$Ref(value, options, dynamicIdScope)) {
128+
if ($Ref.isAllowed$Ref(value, options)) {
129129
const valueScopeBase = dynamicIdScope ? getSchemaBasePath(currentScopeBase, value) : currentScopeBase;
130130
dereferenced = dereference$Ref(
131131
value,
@@ -257,10 +257,18 @@ function dereference$Ref<S extends object = JSONSchema, O extends ParserOptions<
257257
// If the cached object however is _not_ circular and there are additional keys alongside our
258258
// `$ref` pointer here we should merge them back in and return that.
259259
if (!cache.circular) {
260-
if (Object.keys($ref).length > 1) {
260+
const refKeys = Object.keys($ref);
261+
if (refKeys.length > 1) {
262+
const extraKeys = {};
263+
for (const key of refKeys) {
264+
if (key !== "$ref" && !(key in cache.value)) {
265+
// @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
266+
extraKeys[key] = $ref[key];
267+
}
268+
}
261269
return {
262270
circular: cache.circular,
263-
value: $Ref.dereference($ref, cache.value, options, dynamicIdScope),
271+
value: Object.assign({}, cache.value, extraKeys),
264272
};
265273
}
266274

@@ -310,7 +318,7 @@ function dereference$Ref<S extends object = JSONSchema, O extends ParserOptions<
310318
}
311319

312320
// Dereference the JSON reference
313-
let dereferencedValue = $Ref.dereference($ref, pointer.value, options, dynamicIdScope);
321+
let dereferencedValue = $Ref.dereference($ref, pointer.value, options);
314322

315323
// Crawl the dereferenced value (unless it's circular)
316324
if (!circular) {

lib/pointer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ function resolveIf$Ref<S extends object = JSONSchema, O extends ParserOptions<S>
308308
) {
309309
// Is the value a JSON reference? (and allowed?)
310310

311-
if ($Ref.isAllowed$Ref(pointer.value, options, pointer.$ref.dynamicIdScope)) {
311+
if ($Ref.isAllowed$Ref(pointer.value, options)) {
312312
const resolutionBase = pointer.$ref.dynamicIdScope ? pointer.scopeBase : pointer.path;
313313
const $refPath = url.resolve(resolutionBase, pointer.value.$ref);
314314

@@ -326,7 +326,7 @@ function resolveIf$Ref<S extends object = JSONSchema, O extends ParserOptions<S>
326326
if ($Ref.isExtended$Ref(pointer.value)) {
327327
// This JSON reference "extends" the resolved value, rather than simply pointing to it.
328328
// So the resolved path does NOT change. Just the value does.
329-
pointer.value = $Ref.dereference(pointer.value, resolved.value, options, pointer.$ref.dynamicIdScope);
329+
pointer.value = $Ref.dereference(pointer.value, resolved.value, options);
330330
if (pointer.$ref.dynamicIdScope) {
331331
pointer.scopeBase = getSchemaBasePath(pointer.scopeBase, pointer.value);
332332
}

lib/ref.ts

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,11 @@ class $Ref<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOpt
199199
* @param options
200200
* @returns
201201
*/
202-
static isAllowed$Ref<S extends object = JSONSchema>(
203-
value: unknown,
204-
options?: ParserOptions<S>,
205-
allowPlainNameFragments = false,
206-
) {
202+
static isAllowed$Ref<S extends object = JSONSchema>(value: unknown, options?: ParserOptions<S>) {
207203
if (this.is$Ref(value)) {
208204
if (value.$ref.substring(0, 2) === "#/" || value.$ref === "#") {
209205
// It's a JSON Pointer reference, which is always allowed
210206
return true;
211-
} else if (allowPlainNameFragments && value.$ref[0] === "#") {
212-
// JSON Schema 2019-09+ allows plain-name fragments declared via $anchor/$dynamicAnchor
213-
return true;
214207
} else if (value.$ref[0] !== "#" && (!options || options.resolve?.external)) {
215208
// It's an external reference, which is allowed by the options
216209
return true;
@@ -293,12 +286,7 @@ class $Ref<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOpt
293286
$ref: $Ref<S, O>,
294287
resolvedValue: S,
295288
options?: O,
296-
useSpecCompliantRefSiblings = false,
297289
): S {
298-
if ($Ref.isExtended$Ref($ref) && useSpecCompliantRefSiblings) {
299-
return preserveRefSiblings($ref, resolvedValue) as S;
300-
}
301-
302290
if (resolvedValue && typeof resolvedValue === "object" && $Ref.isExtended$Ref($ref)) {
303291
const merged = {} as Partial<S>;
304292
for (const key of Object.keys($ref)) {
@@ -337,27 +325,6 @@ class $Ref<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOpt
337325
}
338326
}
339327

340-
function preserveRefSiblings<T>(refValue: Record<string, any>, resolvedValue: T): T {
341-
const preserved = {} as Record<string, any>;
342-
const existingAllOf = "allOf" in refValue ? refValue.allOf : undefined;
343-
344-
for (const key of Object.keys(refValue)) {
345-
if (key !== "$ref" && key !== "allOf") {
346-
preserved[key] = refValue[key];
347-
}
348-
}
349-
350-
const allOf = [resolvedValue];
351-
if (Array.isArray(existingAllOf)) {
352-
allOf.push(...existingAllOf);
353-
} else if (existingAllOf !== undefined) {
354-
allOf.push(existingAllOf);
355-
}
356-
357-
preserved.allOf = allOf;
358-
return preserved as T;
359-
}
360-
361328
function deepMerge<T>(target: Partial<T>, source: Partial<T>): T {
362329
//return {...target, ...source};
363330

lib/refs.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export default class $Refs<S extends object = JSONSchema, O extends ParserOption
112112
*/
113113
_get$Ref(path: string) {
114114
path = url.resolve(this._root$Ref.path!, path);
115-
return this._getRef(this._exactAliases[path] || path);
115+
return this._getRef(path);
116116
}
117117

118118
/**
@@ -149,15 +149,6 @@ export default class $Refs<S extends object = JSONSchema, O extends ParserOption
149149
return $ref;
150150
}
151151

152-
_addExactAlias(path: string, targetPath: string) {
153-
if (!path || this._exactAliases[path]) {
154-
return this._exactAliases[path];
155-
}
156-
157-
this._exactAliases[path] = targetPath;
158-
return targetPath;
159-
}
160-
161152
/**
162153
* Resolves the given JSON reference.
163154
*
@@ -169,14 +160,13 @@ export default class $Refs<S extends object = JSONSchema, O extends ParserOption
169160
*/
170161
_resolve(path: string, pathFromRoot: string, options?: O) {
171162
const absPath = url.resolve(this._root$Ref.path!, path);
172-
const canonicalPath = this._exactAliases[absPath] || absPath;
173-
const $ref = this._getRef(canonicalPath);
163+
const $ref = this._getRef(absPath);
174164

175165
if (!$ref) {
176166
throw new Error(`Error resolving $ref pointer "${path}". \n"${url.stripHash(absPath)}" not found.`);
177167
}
178168

179-
return $ref.resolve(canonicalPath, options, path, pathFromRoot);
169+
return $ref.resolve(absPath, options, path, pathFromRoot);
180170
}
181171

182172
/**
@@ -189,8 +179,6 @@ export default class $Refs<S extends object = JSONSchema, O extends ParserOption
189179

190180
_aliases: $RefsMap<S, O> = {};
191181

192-
_exactAliases: Record<string, string> = {};
193-
194182
/**
195183
* The {@link $Ref} object that is the root of the JSON schema.
196184
*
@@ -209,7 +197,6 @@ export default class $Refs<S extends object = JSONSchema, O extends ParserOption
209197

210198
this._$refs = {};
211199
this._aliases = {};
212-
this._exactAliases = {};
213200

214201
// @ts-ignore
215202
this._root$Ref = null;

lib/util/schema-resources.ts

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,24 @@ export function registerSchemaResources<S extends object = JSONSchema, O extends
3838

3939
const seen = new Set<object>();
4040

41-
const visit = (node: unknown, scopeBase: string, pointerTokens: string[]) => {
41+
const visit = (node: unknown, scopeBase: string) => {
4242
if (!node || typeof node !== "object" || ArrayBuffer.isView(node) || seen.has(node)) {
4343
return;
4444
}
4545

4646
seen.add(node);
4747

4848
const nextScopeBase = getSchemaBasePath(scopeBase, node);
49-
const resourcePointerTokens = nextScopeBase === scopeBase ? pointerTokens : [];
5049
if (nextScopeBase !== scopeBase) {
5150
$refs._addAlias(nextScopeBase, node as S, pathType, dynamicIdScope);
5251
}
53-
registerAnchorAliases($refs, nextScopeBase, resourcePointerTokens, node);
5452

5553
for (const key of Object.keys(node)) {
56-
visit((node as Record<string, unknown>)[key], nextScopeBase, [...resourcePointerTokens, key]);
54+
visit((node as Record<string, unknown>)[key], nextScopeBase);
5755
}
5856
};
5957

60-
visit(value, basePath, []);
58+
visit(value, basePath);
6159
}
6260

6361
function getSchemaId(value: unknown): string | undefined {
@@ -73,37 +71,3 @@ function getSchemaId(value: unknown): string | undefined {
7371

7472
return undefined;
7573
}
76-
77-
function registerAnchorAliases<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
78-
$refs: $Refs<S, O>,
79-
scopeBase: string,
80-
pointerTokens: string[],
81-
value: unknown,
82-
) {
83-
if (!value || typeof value !== "object" || ArrayBuffer.isView(value)) {
84-
return;
85-
}
86-
87-
const resourceBase = url.stripHash(scopeBase);
88-
const targetPath = pointerTokens.length > 0 ? joinPointerPath(resourceBase, pointerTokens) : `${resourceBase}#`;
89-
const anchors = [
90-
(value as { $anchor?: unknown }).$anchor,
91-
(value as { $dynamicAnchor?: unknown }).$dynamicAnchor,
92-
];
93-
94-
for (const anchor of anchors) {
95-
if (typeof anchor === "string" && anchor.length > 0) {
96-
$refs._addExactAlias(`${resourceBase}#${anchor}`, targetPath);
97-
}
98-
}
99-
}
100-
101-
function joinPointerPath(basePath: string, tokens: string[]) {
102-
let path = `${basePath}#`;
103-
104-
for (const token of tokens) {
105-
path += `/${token.replace(/~/g, "~0").replace(/\//g, "~1")}`;
106-
}
107-
108-
return path;
109-
}

test/specs/defs/dereferencedA.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
}
3131
},
3232
{
33-
"type": "object",
33+
"type": "number",
3434
"format": "float"
3535
}
3636
]
@@ -76,7 +76,7 @@
7676
}
7777
},
7878
{
79-
"type": "object",
79+
"type": "number",
8080
"format": "float"
8181
}
8282
]
@@ -110,7 +110,7 @@
110110
}
111111
},
112112
{
113-
"type": "object",
113+
"type": "number",
114114
"format": "float"
115115
}
116116
]

0 commit comments

Comments
 (0)