Skip to content

Added sum function - #1817

Merged
TomasEng merged 7 commits into
mainfrom
feature/1789-2-add-sum-expression
Jun 22, 2026
Merged

Added sum function#1817
TomasEng merged 7 commits into
mainfrom
feature/1789-2-add-sum-expression

Conversation

@olavsorl

@olavsorl olavsorl commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds sum support to the expression engine. The function takes one or multiple lists as argument.

Related Issue(s)

Verification

  • Your code builds clean without any errors or warnings
  • Manual testing done (required)
  • Relevant automated test added (if you find this hard, leave it and we'll help out)
  • All tests run green

Documentation

  • User documentation is updated with a separate linked PR in altinn-studio-docs. (if applicable)

Summary by CodeRabbit

Release Notes

  • New Features

    • Added a sum expression function to calculate the arithmetic total of numeric values in a list. Supports integers, floating-point numbers, negative values, scientific notation (including numeric strings), and mixed numeric types. Empty lists return 0; all-null lists return 0 (nulls are otherwise ignored).
  • Bug Fixes

    • Improved numeric coercion and arithmetic handling for expression calculations.
  • Tests

    • Added comprehensive fixture and shared test coverage for success and failure scenarios.

@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 13.33% 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 'Added sum function' directly and clearly summarizes the main change—adding a sum function to the expression engine.
Linked Issues check ✅ Passed The PR fully implements all acceptance criteria from issue #1789: adds sum function accepting a list, uses decimal-based algorithm like plus, treats nulls as zero, and returns zero for empty lists.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing the sum function and its integration into the expression engine as specified in issue #1789.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/1789-2-add-sum-expression

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 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.

Inline comments:
In `@src/Altinn.App.Core/Internal/Expressions/ExpressionEvaluator.cs`:
- Around line 1040-1050: The validation condition in the Sum method uses AND
(&&) when it should use OR (||), which allows invalid arguments to pass
validation. The condition should ensure that both the argument count is exactly
one AND the argument is an array type. Change the && operator to || in the if
statement so that the exception is correctly thrown when either args.Length is
not equal to 1 OR expressionValue.ValueKind is not JsonValueKind.Array,
preventing non-array values from reaching the .Array property access.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e5b24669-1993-4591-8827-4755164870eb

📥 Commits

Reviewing files that changed from the base of the PR and between 6e40a61 and 183d09e.

📒 Files selected for processing (4)
  • src/Altinn.App.Core/Internal/Expressions/ExpressionEvaluator.cs
  • src/Altinn.App.Core/Models/Expressions/ExpressionFunction.cs
  • test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/TestFunctions.cs
  • test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/shared-tests/functions/sum/sum.json

@olavsorl
olavsorl force-pushed the feature/1789-2-add-sum-expression branch from 183d09e to 30b7fa2 Compare June 18, 2026 18:39
@Altinn Altinn deleted a comment from coderabbitai Bot Jun 18, 2026
@olavsorl olavsorl added squad/data Issues that belongs to the named squad. feature Label Pull requests with new features. Used when generation releasenotes backport-ignore This PR is a new feature and should not be cherry-picked onto release branches labels Jun 18, 2026
@olavsorl olavsorl moved this to 👷 In progress in Team Altinn Studio Jun 18, 2026
@olavsorl olavsorl moved this from 👷 In progress to 🔎 In review in Team Altinn Studio Jun 18, 2026
@TomasEng TomasEng self-assigned this Jun 22, 2026

@TomasEng TomasEng left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Har ikke noe særlig å utsette på dette, bare noen spørmål om potensielle forenklinger. Husk å markere sjekkboksene i beskrivelsen. Beskrivelsen sier også "the function takes one or multiple lists as arguments", men den tar vel bare én?

Comment thread src/Altinn.App.Core/Internal/Expressions/ExpressionEvaluator.cs
Comment thread src/Altinn.App.Core/Internal/Expressions/ExpressionEvaluator.cs Outdated
@TomasEng TomasEng assigned olavsorl and unassigned TomasEng Jun 22, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/Altinn.App.Core/Internal/Expressions/ExpressionEvaluator.cs (1)

1050-1056: 🧹 Nitpick | 🔵 Trivial | 💤 Low value

Consider reordering: validate argument count before accessing element.

The current code calls FirstOrDefault() before validating the argument count. While functionally correct, accessing the element after validation is cleaner and avoids the unnecessary default-value path.

♻️ Suggested reorder
 private static double? Sum(ExpressionValue[] args)
 {
-    var expressionValue = args.FirstOrDefault();
     if (args.Length != 1)
     {
         throw new ExpressionEvaluatorTypeErrorException($"Expected 1 argument(s), got {args.Length}");
     }
+    var expressionValue = args[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/Altinn.App.Core/Internal/Expressions/ExpressionEvaluator.cs` around lines
1050 - 1056, In the Sum method, reorder the logic to validate the argument count
before accessing array elements. Move the length validation check (args.Length
!= 1) to occur before the FirstOrDefault() call on the args array. This ensures
that we validate the preconditions first and only then access the
expressionValue, making the code cleaner and avoiding unnecessary default-value
path execution.
🤖 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.

Nitpick comments:
In `@src/Altinn.App.Core/Internal/Expressions/ExpressionEvaluator.cs`:
- Around line 1050-1056: In the Sum method, reorder the logic to validate the
argument count before accessing array elements. Move the length validation check
(args.Length != 1) to occur before the FirstOrDefault() call on the args array.
This ensures that we validate the preconditions first and only then access the
expressionValue, making the code cleaner and avoiding unnecessary default-value
path execution.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1a583011-c510-47b5-afed-cf698c2a6aaf

📥 Commits

Reviewing files that changed from the base of the PR and between 183d09e and b0a1f28.

📒 Files selected for processing (5)
  • src/Altinn.App.Core/Internal/Expressions/ExpressionEvaluator.cs
  • src/Altinn.App.Core/Models/Expressions/ExpressionFunction.cs
  • test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/TestFunctions.cs
  • test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/shared-tests/functions/sum/sum.json
  • test/Altinn.App.Core.Tests/PublicApiTests.PublicApi_ShouldNotChange_Unintentionally.verified.txt
✅ Files skipped from review due to trivial changes (1)
  • test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/shared-tests/functions/sum/sum.json
🚧 Files skipped from review as they are similar to previous changes (2)
  • test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/TestFunctions.cs
  • src/Altinn.App.Core/Models/Expressions/ExpressionFunction.cs

@sonarqubecloud

Copy link
Copy Markdown

@olavsorl olavsorl assigned TomasEng and unassigned olavsorl Jun 22, 2026
@olavsorl
olavsorl requested a review from TomasEng June 22, 2026 12:52
@TomasEng
TomasEng merged commit e63ff50 into main Jun 22, 2026
19 checks passed
@TomasEng
TomasEng deleted the feature/1789-2-add-sum-expression branch June 22, 2026 13:09
@TomasEng TomasEng removed their assignment Jun 22, 2026
@coderabbitai coderabbitai Bot mentioned this pull request Jun 23, 2026
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-ignore This PR is a new feature and should not be cherry-picked onto release branches feature Label Pull requests with new features. Used when generation releasenotes squad/data Issues that belongs to the named squad.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add sum expression in backend

2 participants