Skip to content

Commit

Permalink
fix(deepMerge): deepMerge is altering parameter definitions (#1888)
Browse files Browse the repository at this point in the history
  • Loading branch information
chatelgu authored Mar 16, 2024
1 parent f0b853e commit e4ee447
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Utils/deepMerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function deepMerge(
a: { [key: string]: JSONSchema7Definition },
b: { [key: string]: JSONSchema7Definition }
): { [x: string]: JSONSchema7Definition } {
const output = { ...a, ...b };
const output = { ...structuredClone(a), ...structuredClone(b) };

for (const key in a) {
if (b.hasOwnProperty(key)) {
Expand Down
8 changes: 8 additions & 0 deletions test/valid-data-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ describe("valid-data-type", () => {
it("type-intersection-conflict", assertValidSchema("type-intersection-conflict", "MyObject"));
it("type-intersection-partial-conflict", assertValidSchema("type-intersection-partial-conflict", "MyType"));
it("type-intersection-partial-conflict-ref", assertValidSchema("type-intersection-partial-conflict", "MyType"));
it(
"type-intersection-partial-conflict-union",
assertValidSchema("type-intersection-partial-conflict-union", "MyType")
);
it(
"type-intersection-partial-conflict-union-alias",
assertValidSchema("type-intersection-partial-conflict-union-alias", "MyType")
);
it(
"type-intersection-recursive-interface",
assertValidSchema("type-intersection-recursive-interface", "Intersection")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type X = 'a' | 'b'

type Foo = {
foo: X
}

export type A = Foo & {
foo: 'a'
}

export type B = Foo & {
foo: 'b'
}

export type MyType = A | B
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/definitions/MyType",
"definitions": {
"MyType": {
"anyOf": [
{
"$ref": "#/definitions/A"
},
{
"$ref": "#/definitions/B"
}
]
},
"A": {
"type": "object",
"additionalProperties": false,
"properties": {
"foo": {
"type": "string",
"const": "a"
}
},
"required": [
"foo"
]
},
"B": {
"type": "object",
"additionalProperties": false,
"properties": {
"foo": {
"type": "string",
"const": "b"
}
},
"required": [
"foo"
]
}
}
}
13 changes: 13 additions & 0 deletions test/valid-data/type-intersection-partial-conflict-union/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type Foo = {
foo: 'a' | 'b'
}

export type A = Foo & {
foo: 'a'
}

export type B = Foo & {
foo: 'b'
}

export type MyType = A | B
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/definitions/MyType",
"definitions": {
"MyType": {
"anyOf": [
{
"$ref": "#/definitions/A"
},
{
"$ref": "#/definitions/B"
}
]
},
"A": {
"type": "object",
"additionalProperties": false,
"properties": {
"foo": {
"type": "string",
"const": "a"
}
},
"required": [
"foo"
]
},
"B": {
"type": "object",
"additionalProperties": false,
"properties": {
"foo": {
"type": "string",
"const": "b"
}
},
"required": [
"foo"
]
}
}
}

0 comments on commit e4ee447

Please sign in to comment.