Skip to content

Commit 71534dd

Browse files
stephentoubCopilot
andcommitted
Use order-insensitive stable stringify for Java codegen collision check
renameBrandDefinitionKeys compared schema fragments with JSON.stringify, which is sensitive to object key order and could report false-positive collisions (or miss real ones). Add a local key-sorted stableStringify helper (mirroring scripts/codegen/utils.ts) and use it for the check. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 2fc79f3 commit 71534dd

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

java/scripts/codegen/java.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,24 @@ function fixBrandRef(ref: string): string {
8686
return `${ref.slice(0, lastSlash + 1)}${fixBrandCasing(ref.slice(lastSlash + 1))}`;
8787
}
8888

89+
function stableStringify(value: unknown): string {
90+
if (Array.isArray(value)) {
91+
return `[${value.map((item) => stableStringify(item)).join(",")}]`;
92+
}
93+
94+
if (value && typeof value === "object") {
95+
const entries = Object.entries(value as Record<string, unknown>).sort(([a], [b]) => a.localeCompare(b));
96+
return `{${entries.map(([key, entryValue]) => `${JSON.stringify(key)}:${stableStringify(entryValue)}`).join(",")}}`;
97+
}
98+
99+
return JSON.stringify(value) ?? "undefined";
100+
}
101+
89102
function renameBrandDefinitionKeys(defs: Record<string, unknown>): void {
90103
for (const oldKey of Object.keys(defs)) {
91104
const newKey = fixBrandCasing(oldKey);
92105
if (newKey === oldKey) continue;
93-
if (newKey in defs && JSON.stringify(defs[newKey]) !== JSON.stringify(defs[oldKey])) {
106+
if (newKey in defs && stableStringify(defs[newKey]) !== stableStringify(defs[oldKey])) {
94107
throw new Error(
95108
`Brand-casing normalization collision: "${oldKey}" -> "${newKey}" but a different definition already exists under "${newKey}".`
96109
);

0 commit comments

Comments
 (0)