Skip to content

Commit ebec2f7

Browse files
committed
fix: preserve non-finite composite hydration keys
1 parent 5646793 commit ebec2f7

4 files changed

Lines changed: 99 additions & 0 deletions

File tree

‎packages/typeorm/src/query-builder/transformer/RawSqlResultsToEntityTransformer.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ export class RawSqlResultsToEntityTransformer {
144144
return JSON.stringify(keyValue)
145145
}
146146

147+
if (
148+
typeof keyValue === "number" &&
149+
!Number.isFinite(keyValue)
150+
) {
151+
// JSON turns non-finite numbers into null; a nested tuple
152+
// also keeps them distinct from literal string values.
153+
return [keyValue.toString()]
154+
}
155+
147156
return typeof keyValue === "bigint"
148157
? keyValue.toString()
149158
: keyValue
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { expect } from "chai"
2+
import type { DataSource } from "../../../../src"
3+
import {
4+
closeTestingConnections,
5+
createTestingConnections,
6+
reloadTestingDatabases,
7+
} from "../../../utils/test-utils"
8+
import { FloatingRecord } from "./entity/FloatingRecord"
9+
10+
// Regression for #12902: JSON must not collapse non-finite keys to null.
11+
describe("query builder > composite floating key hydration", () => {
12+
let dataSources: DataSource[]
13+
before(async () => {
14+
dataSources = await createTestingConnections({
15+
enabledDrivers: ["postgres"],
16+
entities: [FloatingRecord],
17+
})
18+
})
19+
beforeEach(() => reloadTestingDatabases(dataSources))
20+
after(() => closeTestingConnections(dataSources))
21+
22+
it("should keep non-finite root and joined keys distinct", () =>
23+
Promise.all(
24+
dataSources.map(async (dataSource) => {
25+
const repository = dataSource.getRepository(FloatingRecord)
26+
const parent = repository.create({
27+
namespace: "parent",
28+
key: 0,
29+
})
30+
await repository.insert(parent)
31+
const keys = [NaN, Infinity, -Infinity]
32+
for (const key of keys) {
33+
const child = repository.create({
34+
namespace: "child",
35+
key,
36+
parent,
37+
})
38+
await repository.insert(child)
39+
await repository.insert({
40+
namespace: "leaf",
41+
key,
42+
parent: child,
43+
})
44+
}
45+
46+
const children = await repository.find({
47+
where: { namespace: "child" },
48+
relations: { children: true },
49+
})
50+
expect(
51+
children.map((child) => ({
52+
key: String(child.key),
53+
children: child.children.map((leaf) =>
54+
String(leaf.key),
55+
),
56+
})),
57+
).to.have.deep.members(
58+
keys.map((key) => ({
59+
key: String(key),
60+
children: [String(key)],
61+
})),
62+
)
63+
64+
const loadedParent = await repository.findOneOrFail({
65+
where: { namespace: parent.namespace, key: parent.key },
66+
relations: { children: true },
67+
})
68+
expect(
69+
loadedParent.children.map((child) => String(child.key)),
70+
).to.have.members(keys.map(String))
71+
}),
72+
))
73+
})

‎packages/typeorm/test/functional/query-builder/composite-key-hydration/composite-key-hydration.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "../../../utils/test-utils"
88
import { Record } from "./entity/Record"
99

10+
// Regression for #12902: delimiters in keys must not merge distinct entities.
1011
describe("query builder > composite key hydration", () => {
1112
let dataSources: DataSource[]
1213
before(async () => {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Entity, ManyToOne, OneToMany, PrimaryColumn } from "../../../../../src"
2+
3+
@Entity()
4+
export class FloatingRecord {
5+
@PrimaryColumn()
6+
namespace: string
7+
8+
@PrimaryColumn("double precision")
9+
key: number
10+
11+
@ManyToOne(() => FloatingRecord, (record) => record.children)
12+
parent: FloatingRecord | null
13+
14+
@OneToMany(() => FloatingRecord, (record) => record.parent)
15+
children: FloatingRecord[]
16+
}

0 commit comments

Comments
 (0)