fix (gateway/config): merge config.patch object arrays by id

This commit is contained in:
Vignesh Natarajan
2026-02-14 19:33:32 -08:00
parent a3e2d0563e
commit 8ec0ef5866
2 changed files with 53 additions and 3 deletions

View File

@@ -2,7 +2,48 @@ import { isPlainObject } from "../utils.js";
type PlainObject = Record<string, unknown>;
export function applyMergePatch(base: unknown, patch: unknown): unknown {
type MergePatchOptions = {
mergeObjectArraysById?: boolean;
};
function isObjectWithStringId(value: unknown): value is Record<string, unknown> & { id: string } {
if (!isPlainObject(value)) {
return false;
}
return typeof value.id === "string" && value.id.length > 0;
}
function mergeObjectArraysById(base: unknown[], patch: unknown[], options: MergePatchOptions) {
if (!base.every(isObjectWithStringId) || !patch.every(isObjectWithStringId)) {
return undefined;
}
const merged = [...base] as Array<Record<string, unknown> & { id: string }>;
const indexById = new Map<string, number>();
for (const [index, entry] of merged.entries()) {
indexById.set(entry.id, index);
}
for (const entry of patch) {
const existingIndex = indexById.get(entry.id);
if (existingIndex === undefined) {
merged.push(structuredClone(entry));
indexById.set(entry.id, merged.length - 1);
continue;
}
merged[existingIndex] = applyMergePatch(merged[existingIndex], entry, options) as Record<
string,
unknown
> & { id: string };
}
return merged;
}
export function applyMergePatch(
base: unknown,
patch: unknown,
options: MergePatchOptions = {},
): unknown {
if (!isPlainObject(patch)) {
return patch;
}
@@ -14,9 +55,16 @@ export function applyMergePatch(base: unknown, patch: unknown): unknown {
delete result[key];
continue;
}
if (options.mergeObjectArraysById && Array.isArray(result[key]) && Array.isArray(value)) {
const mergedArray = mergeObjectArraysById(result[key] as unknown[], value, options);
if (mergedArray) {
result[key] = mergedArray;
continue;
}
}
if (isPlainObject(value)) {
const baseValue = result[key];
result[key] = applyMergePatch(isPlainObject(baseValue) ? baseValue : {}, value);
result[key] = applyMergePatch(isPlainObject(baseValue) ? baseValue : {}, value, options);
continue;
}
result[key] = value;

View File

@@ -342,7 +342,9 @@ export const configHandlers: GatewayRequestHandlers = {
);
return;
}
const merged = applyMergePatch(snapshot.config, parsedRes.parsed);
const merged = applyMergePatch(snapshot.config, parsedRes.parsed, {
mergeObjectArraysById: true,
});
const schemaPatch = loadSchemaWithPlugins();
const restoredMerge = restoreRedactedValues(merged, snapshot.config, schemaPatch.uiHints);
if (!restoredMerge.ok) {