Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Improve object merging #11685

Merged
merged 6 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions packages/core/src/lib/utils/merge.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
// Source: https://stackoverflow.com/a/34749873/5364135

/** Simple object check */
function isObject(item: any): boolean {
return item && typeof item === "object" && !Array.isArray(item)
function isObject(item: unknown): item is object {
return item !== null && typeof item === "object"
}

/** Deep merge two objects */
export function merge(target: any, ...sources: any[]): any {
/** Deep merge two or more objects */
export function merge<T extends Record<string, unknown>>(
target: T,
...sources: Array<Record<string, unknown> | undefined>
): T & Record<string, unknown> {
if (!sources.length) return target
const source = sources.shift()

if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} })
merge(target[key], source[key])
} else {
Object.assign(target, { [key]: source[key] })
}
if (!isObject(target[key]))
(target as Record<string, unknown>)[key] = Array.isArray(source[key])
? []

Check warning on line 18 in packages/core/src/lib/utils/merge.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/lib/utils/merge.ts#L18

Added line #L18 was not covered by tests
: {}
merge(
(target as Record<string, unknown>)[key] as T,
source[key] as Record<string, unknown>
)
} else if (source[key] !== undefined)
(target as Record<string, unknown>)[key] = source[key]
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/lib/utils/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ export default function parseProviders(params: {

if (provider.type === "oauth" || provider.type === "oidc") {
merged.redirectProxyUrl ??= options.redirectProxyUrl
return normalizeOAuth(merged)
return normalizeOAuth(merged) as InternalProvider
}

return merged
return merged as InternalProvider
})

return {
Expand Down
111 changes: 111 additions & 0 deletions packages/core/test/merge.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { describe, it, expect } from "vitest"
import { merge } from "../src/lib/utils/merge"

describe("merge function", () => {
it("should merge objects correctly", () => {
expect(merge({ a: 1, b: { c: 2 } }, { b: { c: 3, d: 4 } })).toEqual({
a: 1,
b: { c: 3, d: 4 },
})
})

it("should override primitive values", () => {
expect(merge({ a: 1, b: "old" }, { b: "new" })).toEqual({ a: 1, b: "new" })
})

it("should override arrays", () => {
expect(merge({ a: [1, 2] }, { a: [3, 4] })).toEqual({ a: [3, 4] })
})

it("should handle nested objects", () => {
expect(merge({ a: { b: { c: 1 } } }, { a: { b: { d: 2 } } })).toEqual({
a: { b: { c: 1, d: 2 } },
})
})

it("should handle null and undefined", () => {
expect(
merge({ a: undefined, b: undefined }, { a: null, b: 2, c: 3 })
).toEqual({ a: null, b: 2, c: 3 })
})

it("should merge multiple objects", () => {
expect(merge({ a: 1 }, { b: 2 }, { b: { d: 2 }, c: "new" })).toEqual({
a: 1,
b: { d: 2 },
c: "new",
})
})

it("should handle empty objects", () => {
expect(merge({ a: 1 }, {})).toEqual({ a: 1 })
})

it("should override functions", () => {
expect(
merge(
{
func: () => "original",
nested: { func: () => "original nested" },
},
{
func: () => "overridden",
nested: { func: () => "overridden nested" },
}
).func()
).toBe("overridden")
expect(
merge(
{
func: () => "original",
nested: { func: () => "original nested" },
},
{
func: () => "overridden",
nested: { func: () => "overridden nested" },
}
).nested.func()
).toBe("overridden nested")
})

it("should override default authorization config with user provided object", () => {
expect(
merge(
{
authorization: "https://example.com/default",
},
{
authorization: {
url: "https://example.com/user-config",
params: { scope: "email,user_friends" },
},
}
)
).toEqual({
authorization: {
url: "https://example.com/user-config",
params: { scope: "email,user_friends" },
},
})
})

it("should correctly merge scopes in authorization params", () => {
expect(
merge(
{
authorization: {
url: "https://example.com/default",
params: { scope: "identify,email" },
},
defaultOptions: "",
},
{
authorization: "https://example.com/user-config",
}
)
).toEqual({
defaultOptions: "",
authorization: "https://example.com/user-config",
})
})
})
Loading