Skip to content

fix: Cast null to 0 in arithmetic expressions#4209

Open
TomasEng wants to merge 3 commits into
mainfrom
cast-null-to-null-in-arithmetic-expressions
Open

fix: Cast null to 0 in arithmetic expressions#4209
TomasEng wants to merge 3 commits into
mainfrom
cast-null-to-null-in-arithmetic-expressions

Conversation

@TomasEng
Copy link
Copy Markdown
Contributor

@TomasEng TomasEng commented May 15, 2026

Description

This pull request changes the bahaviour of the arithmetic functions in our expression language when null is given as an argument. Instead of always returning null when one of the arguments is null, the functions will cast null to the number 0.

Here is the corresponding fix in backend: Altinn/app-lib-dotnet#1751

Related Issue(s)

Verification/QA

  • Manual functionality testing
    • I have tested these changes manually
  • Automated tests
    • Unit test(s) have been added/updated
  • UU/WCAG (follow these guidelines until we have our own)
    • No testing done/necessary (no DOM/visual changes)
  • User documentation @ altinn-studio-docs
  • Support in Altinn Studio
    • This change/feature does not require any changes to Altinn Studio
  • Sprint board
    • The original issue (or this PR itself) has been added to the Team Apps project and to the current sprint board
  • Labels
    • I have added a kind/* and backport* label to this PR for proper release notes grouping

Summary by CodeRabbit

  • Bug Fixes
    • Arithmetic operations now treat null or missing values as 0 during calculations instead of propagating null as the result, so plus/minus/multiply/divide produce numeric results when given null operands.
    • Division by zero now raises a runtime error with a descriptive message instead of returning null, ensuring invalid divisions are reported immediately.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 15, 2026

Warning

Rate limit exceeded

@TomasEng has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 39 minutes and 26 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 90ad52dd-6617-4135-9f53-2c4f0042eded

📥 Commits

Reviewing files that changed from the base of the PR and between 0a8f009 and 8869a51.

📒 Files selected for processing (1)
  • src/features/expressions/expression-functions.ts
📝 Walkthrough

Walkthrough

Arithmetic expression functions now coerce null operands to 0 via applyNullableBinaryOperation; divide was refactored to delegate divide-by-zero validation through a local helper. Shared test fixtures for plus, minus, multiply, and divide were updated to match the new null-handling behavior.

Changes

Arithmetic Functions Null-Handling Refactor

Layer / File(s) Summary
Core null-coercion in applyNullableBinaryOperation and divide refactor
src/features/expressions/expression-functions.ts
applyNullableBinaryOperation now coerces null operands to 0 using a || 0, b || 0. The divide function is refactored with a local divideNumbers helper that validates divide-by-zero and delegates to the shared helper, removing prior explicit null/zero branching.
Test fixture updates for arithmetic functions
src/features/expressions/shared-tests/functions/divide/divide.json, src/features/expressions/shared-tests/functions/minus/minus.json, src/features/expressions/shared-tests/functions/multiply/multiply.json, src/features/expressions/shared-tests/functions/plus/plus.json
Test cases for plus, minus, multiply, and divide updated: all-null inputs now return 0; mixed null/value inputs return the non-null operand (or its negation for minus); divide with effective divisor 0 throws divide-by-zero error.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: casting null to 0 in arithmetic expressions, which is the core refactoring reflected across all modified test files and the expression-functions implementation.
Description check ✅ Passed The PR description is comprehensive and addresses all required sections: it explains the behavioral change, links the related issue, documents manual testing, test updates, and covers all verification/QA checklist items including labels and sprint board.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch cast-null-to-null-in-arithmetic-expressions

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@TomasEng TomasEng moved this to 👷 In progress in Team Altinn Studio May 15, 2026
@TomasEng TomasEng added squad/data Issues that belongs to the named squad. kind/breaking-change Issue/pull request containing a breaking change backport-ignore This PR is a new feature and should not be cherry-picked onto release branches labels May 15, 2026
@TomasEng TomasEng marked this pull request as ready for review May 15, 2026 06:40
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/features/expressions/expression-functions.ts (1)

1035-1040: 🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win

Update return type to reflect that null is never returned.

The function signature declares a return type of number | null, but with the new implementation on line 1039 that coerces null operands to 0 using a || 0, b || 0, the function now always returns number and never returns null. This type inconsistency may mislead callers about the actual behavior.

♻️ Update the return type
 function applyNullableBinaryOperation(
   operation: (a: number, b: number) => number,
   [a, b]: [number | null, number | null],
-): number | null {
+): number {
   return operation(a || 0, b || 0);
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/features/expressions/expression-functions.ts` around lines 1035 - 1040,
The function applyNullableBinaryOperation currently declares a return type of
number | null but its implementation coerces null operands to 0 (using a || 0, b
|| 0) so it never returns null; update the function signature return type to
number (i.e., remove | null) to match actual behavior and keep callers' types
accurate, referencing applyNullableBinaryOperation to locate and change the
signature accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/features/expressions/expression-functions.ts`:
- Around line 1035-1040: The function applyNullableBinaryOperation currently
declares a return type of number | null but its implementation coerces null
operands to 0 (using a || 0, b || 0) so it never returns null; update the
function signature return type to number (i.e., remove | null) to match actual
behavior and keep callers' types accurate, referencing
applyNullableBinaryOperation to locate and change the signature accordingly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f466d98c-d203-4105-a9a9-287506958c90

📥 Commits

Reviewing files that changed from the base of the PR and between 354d5fe and 91ddea8.

📒 Files selected for processing (5)
  • src/features/expressions/expression-functions.ts
  • src/features/expressions/shared-tests/functions/divide/divide.json
  • src/features/expressions/shared-tests/functions/minus/minus.json
  • src/features/expressions/shared-tests/functions/multiply/multiply.json
  • src/features/expressions/shared-tests/functions/plus/plus.json

@TomasEng TomasEng added the area/logic related to logic/dynamics/expressions label May 15, 2026
@TomasEng
Copy link
Copy Markdown
Contributor Author

@coderabbitai

Update return type to reflect that null is never returned.

Good catch! I have fixed this now.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 15, 2026

Tip

For best results, initiate chat on the files or code changes.

@TomasEng: Great, thanks for the fix! 🐇 ✨


If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

@TomasEng TomasEng moved this from 👷 In progress to 🔎 In review in Team Altinn Studio May 15, 2026
@sonarqubecloud
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/logic related to logic/dynamics/expressions backport-ignore This PR is a new feature and should not be cherry-picked onto release branches kind/breaking-change Issue/pull request containing a breaking change squad/data Issues that belongs to the named squad.

Projects

Status: 🔎 In review

Development

Successfully merging this pull request may close these issues.

1 participant