Skip to content

fix(sap): QueryBuilder parameter of type JS Date not escaped correctly - #11867

Merged
alumni merged 1 commit into
typeorm:v0.3from
alumni:fix/11414
Dec 30, 2025
Merged

alumni merged 1 commit into
typeorm:v0.3from
alumni:fix/11414

Conversation

@alumni

@alumni alumni commented Dec 22, 2025

Copy link
Copy Markdown
Collaborator

Description of change

Before, JS Date parameters were embedded in SQL. Now, they will be passed to the client as parameters.

Fixes #11414.

Pull-Request Checklist

  • Code is up-to-date with the v0.3 branch
  • This pull request links relevant issues as Fixes #00000
  • There are new or updated tests validating the change (tests/**.test.ts)
  • Documentation has been updated to reflect this change (docs/docs/**.md)

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Date Handling Verification

The PR removes explicit Date-to-string conversion logic, relying on the SAP HANA client to handle Date parameters natively. This should be validated to ensure the client properly handles Date objects across different scenarios (timezones, null dates, edge cases) and that the parameterized approach doesn't introduce any compatibility issues with the SAP HANA driver version being used.

const escapedParameters: any[] = Object.keys(nativeParameters).map(
    (key) => nativeParameters[key],
)

if (!parameters || !Object.keys(parameters).length)
    return [sql, escapedParameters]

sql = sql.replace(
    /:(\.\.\.)?([A-Za-z0-9_.]+)/g,
    (full, isArray: string, key: string): string => {
        if (!parameters.hasOwnProperty(key)) {
            return full
        }

        const value: any = parameters[key]

        if (isArray) {
            return value
                .map((v: any) => {
                    escapedParameters.push(v)
                    return this.createParameter(
                        key,
                        escapedParameters.length - 1,
                    )
                })
                .join(", ")
        }

        if (typeof value === "function") {
            return value()
        }

        escapedParameters.push(value)
        return this.createParameter(key, escapedParameters.length - 1)
Test Coverage

The test only covers a basic date comparison scenario with greater-than-or-equal operator. Consider adding tests for other date operations (equality, less-than, between ranges), edge cases (null dates, timezone handling), and different date formats to ensure comprehensive coverage of the fix.

it("should be escaped correctly", () =>
    Promise.all(
        dataSources.map(async (dataSource) => {
            const productRepository = dataSource.getRepository(Product)
            await productRepository.save([
                {
                    name: "Product A",
                    createdAt: new Date("2025-12-22T00:00:00Z"),
                },
                {
                    name: "Product B",
                    createdAt: new Date("2025-12-23T00:00:00Z"),
                },
                {
                    name: "Product C",
                    createdAt: new Date("2025-12-24T00:00:00Z"),
                },
            ])

            const newProducts = await productRepository
                .createQueryBuilder("product")
                .select("product.name")
                .where("product.createdAt >= :afterDate", {
                    afterDate: new Date("2025-12-22T12:00:00Z"),
                })
                .getMany()

            expect(newProducts).to.deep.equal([
                { name: "Product B" },
                { name: "Product C" },
            ])
        }),
    ))

@pkg-pr-new

pkg-pr-new Bot commented Dec 22, 2025

Copy link
Copy Markdown
  • typeorm-sql-js-example

    npm i https://pkg.pr.new/typeorm/typeorm@11867
    

commit: a0d6cf4

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Refactor test to prevent unhandled rejections

Refactor the test case to use an async function with a for...of loop instead of
Promise.all to iterate through data sources, improving test reliability and
error handling.

test/functional/query-builder/parameters/date-parameters.test.ts [25-57]

-it("should be escaped correctly", () =>
-    Promise.all(
-        dataSources.map(async (dataSource) => {
-            const productRepository = dataSource.getRepository(Product)
-            await productRepository.save([
-                {
-                    name: "Product A",
-                    createdAt: new Date("2025-12-22T00:00:00Z"),
-                },
-                {
-                    name: "Product B",
-                    createdAt: new Date("2025-12-23T00:00:00Z"),
-                },
-                {
-                    name: "Product C",
-                    createdAt: new Date("2025-12-24T00:00:00Z"),
-                },
-            ])
+it("should be escaped correctly", async () => {
+    for (const dataSource of dataSources) {
+        const productRepository = dataSource.getRepository(Product)
+        await productRepository.save([
+            {
+                name: "Product A",
+                createdAt: new Date("2025-12-22T00:00:00Z"),
+            },
+            {
+                name: "Product B",
+                createdAt: new Date("2025-12-23T00:00:00Z"),
+            },
+            {
+                name: "Product C",
+                createdAt: new Date("2025-12-24T00:00:00Z"),
+            },
+        ])
 
-            const newProducts = await productRepository
-                .createQueryBuilder("product")
-                .select("product.name")
-                .where("product.createdAt >= :afterDate", {
-                    afterDate: new Date("2025-12-22T12:00:00Z"),
-                })
-                .getMany()
+        const newProducts = await productRepository
+            .createQueryBuilder("product")
+            .select("product.name")
+            .where("product.createdAt >= :afterDate", {
+                afterDate: new Date("2025-12-22T12:00:00Z"),
+            })
+            .getMany()
 
-            expect(newProducts).to.deep.equal([
-                { name: "Product B" },
-                { name: "Product C" },
-            ])
-        }),
-    ))
+        expect(newProducts).to.deep.equal([
+            { name: "Product B" },
+            { name: "Product C" },
+        ])
+    }
+})
  • Apply / Chat
Suggestion importance[1-10]: 4

__

Why: The suggestion improves test robustness by replacing Promise.all with a for...of loop, which is a safer and more standard pattern for handling asynchronous operations and assertions within a test case.

Low
  • More

@gioboa gioboa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this fix @alumni

@alumni
alumni requested review from naorpeled and sgarner December 30, 2025 21:59
@alumni
alumni merged commit 5153436 into typeorm:v0.3 Dec 30, 2025
gioboa pushed a commit to gioboa/typeorm that referenced this pull request Jan 13, 2026
gioboa pushed a commit to gioboa/typeorm that referenced this pull request Jan 13, 2026
mgohin pushed a commit to mgohin/typeorm that referenced this pull request Jan 15, 2026
kranners pushed a commit to kranners/typeorm that referenced this pull request Mar 1, 2026
@pkuczynski pkuczynski added this to the 1.0 milestone Mar 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants