fix: fix null pointer exception on date array column comparison (#11532)

* fix: fix null pointer exception on date array column comparison

Follow up to https://github.com/typeorm/typeorm/pull/11269 which was
calling Array.map without validating if the array was null. Now only
normalize if both values (entity and database) are not null.

Closes: https://github.com/typeorm/typeorm/issues/11514

* clean test
This commit is contained in:
Mohamed Nader Baccari 2025-06-17 17:05:39 -04:00 committed by GitHub
parent 63a3b9abc1
commit 42e7cbe7da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 77 additions and 5 deletions

View File

@ -1,9 +1,9 @@
import { Subject } from "./Subject"
import { DateUtils } from "../util/DateUtils"
import { ObjectLiteral } from "../common/ObjectLiteral"
import { OrmUtils } from "../util/OrmUtils"
import { ApplyValueTransformers } from "../util/ApplyValueTransformers"
import { DateUtils } from "../util/DateUtils"
import { ObjectUtils } from "../util/ObjectUtils"
import { OrmUtils } from "../util/OrmUtils"
import { Subject } from "./Subject"
/**
* Finds what columns are changed in the subject entities.
@ -81,8 +81,8 @@ export class SubjectChangedColumnsComputer {
if (value !== null && value !== undefined) return
}
let normalizedValue = entityValue
// normalize special values to make proper comparision
if (entityValue !== null) {
// if both values are not null, normalize special values to make proper comparision
if (entityValue !== null && databaseValue !== null) {
switch (column.type) {
case "date":
normalizedValue = column.isArray

View File

@ -17,4 +17,10 @@ export class User {
default: "{}",
})
dates: Date[]
@Column("time without time zone", {
nullable: true,
array: true,
})
nullable_times: string[] | null
}

View File

@ -191,6 +191,72 @@ describe("github issues > #5967 @afterUpdate always says array/json field update
dates: valueAfter,
})
const updateQueries = logger.queries.filter((q) =>
q.startsWith("UPDATE"),
)
expect(updateQueries).to.have.length(1)
}),
))
it("should update a time array column when it goes from null to a value", () =>
Promise.all(
dataSources.map(async (dataSource) => {
const valueBefore = null
const valueAfter = ["12:00"]
const repository = dataSource.getRepository(User)
const logger = dataSource.logger as MemoryLogger
logger.clear()
const user = await repository.save({
nullable_times: valueBefore,
})
const insertQueries = logger.queries.filter((q) =>
q.startsWith("INSERT"),
)
expect(insertQueries).to.have.length(1)
logger.clear()
await repository.save({
id: user.id,
nullable_times: valueAfter,
})
const updateQueries = logger.queries.filter((q) =>
q.startsWith("UPDATE"),
)
expect(updateQueries).to.have.length(1)
}),
))
it("should update a time array column when it goes from a value to null", () =>
Promise.all(
dataSources.map(async (dataSource) => {
const valueBefore = ["12:00"]
const valueAfter = null
const repository = dataSource.getRepository(User)
const logger = dataSource.logger as MemoryLogger
logger.clear()
const user = await repository.save({
nullable_times: valueBefore,
})
const insertQueries = logger.queries.filter((q) =>
q.startsWith("INSERT"),
)
expect(insertQueries).to.have.length(1)
logger.clear()
await repository.save({
id: user.id,
nullable_times: valueAfter,
})
const updateQueries = logger.queries.filter((q) =>
q.startsWith("UPDATE"),
)