refactor(schema): share resource ID collision handling (#17426)

This commit is contained in:
Vincent Koc
2026-12-06 14:59:20 +08:00
committed by GitHub
parent 96394458e7
commit ae5e3d4276
+14 -19
View File
@@ -227,27 +227,22 @@ export class JsonSchemaReferences {
dialect: dialectOf(schema, parent.dialect),
parent,
};
if (baseUri !== undefined) {
const existing = this.#identities.get(baseUri);
if (existing !== undefined && existing.root !== schema) {
this.#ambiguous.add(existing);
this.#ambiguous.add(scope);
} else if (existing !== undefined) scope = existing;
else this.#identities.set(baseUri, scope);
} else {
let identities = this.#relativeIdentities.get(parent);
if (identities === undefined) {
identities = new Map();
this.#relativeIdentities.set(parent, identities);
let identities = this.#identities;
if (baseUri === undefined) {
let relative = this.#relativeIdentities.get(parent);
if (relative === undefined) {
relative = new Map();
this.#relativeIdentities.set(parent, relative);
}
const identity = schema.$id.replace(/#$/u, "");
const existing = identities.get(identity);
if (existing !== undefined && existing.root !== schema) {
this.#ambiguous.add(existing);
this.#ambiguous.add(scope);
} else if (existing !== undefined) scope = existing;
else identities.set(identity, scope);
identities = relative;
}
const identity = baseUri ?? schema.$id.replace(/#$/u, "");
const existing = identities.get(identity);
if (existing !== undefined && existing.root !== schema) {
this.#ambiguous.add(existing);
this.#ambiguous.add(scope);
} else if (existing !== undefined) scope = existing;
else identities.set(identity, scope);
} catch {
// An opaque or unknown URI base must not be replaced with a guessed scope.
}