add hideAppNameInPdf functionality to footer in the pdf#1753
Conversation
📝 WalkthroughWalkthroughPdfService now reads layoutSets.uiSettings.hideAppNameInPdf (literal or expression) to decide whether to include the app name in PDF footers; it accepts IAppResources and uses JSON deserialization and expression evaluation via the instance/unit-of-work initializer. Several instance data accessor APIs were tightened to return non-null LayoutEvaluatorState. ChangesPDF Footer Customization with App Name Hiding
🎯 4 (Complex) | ⏱️ ~45 minutes
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
test/Altinn.App.Core.Tests/Internal/Pdf/PdfServiceTests.cs (1)
37-37: ⚡ Quick winAdd focused tests for the new footer branches.
The fixture now wires
ILayoutEvaluatorStateInitializer, but there are still no assertions arounduiSettings.hideAppNameInPdfas a literal boolean, as an expression, or when the config is malformed. Those are the new branches most likely to regress here.Also applies to: 538-539
🤖 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/Pdf/PdfService.cs`:
- Around line 409-447: Wrap the whole hideAppNameInPdf evaluation in a try/catch
that returns false on any exception so PDF generation fails closed; when parsing
layoutSetsString call JsonDocument.Parse with an explicit JsonDocumentOptions
(set AllowTrailingCommas = true and CommentHandling = JsonCommentHandling.Skip)
instead of relying on _jsonSerializerOptions; after obtaining hideAppNameElement
ensure ValueKind is True/False or safely call
hideAppNameElement.Deserialize<Expression>(_jsonSerializerOptions) and if that
returns null return false; guard against null instance.Data before using
instance.Data.Find; and catch exceptions from
_instanceDataUnitOfWorkInitializer.Init, _layoutStateInit.Init and
ExpressionEvaluator.EvaluateExpression and return false on error.
🪄 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: ddfdd5b6-cadf-40d7-9e23-9d1f72286dec
📒 Files selected for processing (4)
src/Altinn.App.Core/Internal/Pdf/PdfService.cstest/Altinn.App.Api.Tests/Controllers/PdfControllerTests.cstest/Altinn.App.Core.Tests/Internal/Pdf/PdfServiceTests.cstest/Altinn.App.Core.Tests/PublicApiTests.PublicApi_ShouldNotChange_Unintentionally.verified.txt
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
test/Altinn.App.Core.Tests/Internal/Pdf/PdfServiceTests.cs (1)
37-37: ⚡ Quick winConsider adding setup for the layout state initializer mock or verifying it's not needed.
The
_layoutStateInitmock is instantiated but never configured with any behavior. While Moq allows this, it's worth confirming whether:
- The mock should be set up to support expression-based
hideAppNameInPdftests (see comment on lines 480-629), or- The current test scenarios don't require the initializer to be invoked
🤖 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 `@test/Altinn.App.Core.Tests/Internal/Pdf/PdfServiceTests.cs` at line 37, The _layoutStateInit mock (Mock<ILayoutEvaluatorStateInitializer>) is created but never configured; either add explicit setups for the calls used in PdfServiceTests — e.g., configure _layoutStateInit.Setup(s => s.InitializeState(It.IsAny<...>())).Returns(...) or Setup for any expression-based behavior used by the hideAppNameInPdf-related tests — or add assertions that the initializer is never invoked; update tests referencing hideAppNameInPdf to call the initializer mock or remove the unused field if it is truly unnecessary so the test intent is explicit.
🤖 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 `@test/Altinn.App.Core.Tests/Internal/Pdf/PdfServiceTests.cs`:
- Around line 480-629: Add a unit test that covers expression-based
hideAppNameInPdf by supplying layoutSets JSON with an expression (e.g.
{"sets":[],"uiSettings":{"hideAppNameInPdf":["equals",["dataModel","someField"],"someValue"]}})
when constructing the PdfService via SetupPdfService, and mock the
ILayoutEvaluatorStateInitializer / its evaluator to evaluate that expression to
true and false respectively so you can Verify GeneratePdf called with footer
that omits the app name when the expression evaluates true and includes it when
false; target the GenerateAndStorePdf method and reuse the existing Instance
setup and _pdfGeneratorClient.Verify pattern to assert behavior.
---
Nitpick comments:
In `@test/Altinn.App.Core.Tests/Internal/Pdf/PdfServiceTests.cs`:
- Line 37: The _layoutStateInit mock (Mock<ILayoutEvaluatorStateInitializer>) is
created but never configured; either add explicit setups for the calls used in
PdfServiceTests — e.g., configure _layoutStateInit.Setup(s =>
s.InitializeState(It.IsAny<...>())).Returns(...) or Setup for any
expression-based behavior used by the hideAppNameInPdf-related tests — or add
assertions that the initializer is never invoked; update tests referencing
hideAppNameInPdf to call the initializer mock or remove the unused field if it
is truly unnecessary so the test intent is explicit.
🪄 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: c681f9d8-14df-4a3f-ab85-bfe2243b5910
📒 Files selected for processing (2)
src/Altinn.App.Core/Internal/Pdf/PdfService.cstest/Altinn.App.Core.Tests/Internal/Pdf/PdfServiceTests.cs
🚧 Files skipped from review as they are similar to previous changes (1)
- src/Altinn.App.Core/Internal/Pdf/PdfService.cs
Also make IInstanceDataAccessor.GetLayoutEvaluatorState not return nullable
|
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/Altinn.App.Core/Internal/Pdf/PdfService.cs (1)
400-461:⚠️ Potential issue | 🟠 Major | ⚡ Quick winGuard against null expression after deserialization.
Line 433 deserializes
hideAppNameinto anExpression, butDeserialize<T>can returnnullif the JSON value is literallynullor in certain deserialization failure modes. Line 448 then evaluates the expression without checking, which will throw an unhandled exception (likelyArgumentNullExceptionorNullReferenceException) that is not caught by theJsonException/InvalidOperationExceptionhandlers below.🛡️ Proposed fix
var expression = hideAppName.Deserialize<Expression>(_jsonSerializerOptions); + if (expression is null) + { + _logger.LogWarning("hideAppNameInPdf expression deserialized to null, defaulting to showing app name"); + return false; + } var dataAccessor = await _instanceDataUnitOfWorkInitializer.Init(instance, taskId, language);🤖 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/Pdf/PdfService.cs` around lines 400 - 461, In GetHideAppNameInPdf, guard against null after deserializing hideAppName into an Expression: after calling hideAppName.Deserialize<Expression>(_jsonSerializerOptions) (variable expression), check if expression is null and if so log a warning (including context like "hideAppName expression was null") and return false (default) before calling ExpressionEvaluator.EvaluateExpression; keep existing exception handlers unchanged.
🧹 Nitpick comments (1)
test/Altinn.App.Core.Tests/Internal/Pdf/PdfServiceTests.cs (1)
479-518: ⚡ Quick winAdd test coverage for expression evaluating to false.
The test at line 479-518 verifies an expression that evaluates to
true(["equals","a","a"]), and line 558-594 tests a boolean literalfalse. Consider adding a test for an expression that evaluates tofalse(e.g.,["equals","a","b"]) to ensure the expression evaluation path correctly handles both outcomes, not just boolean literals.✅ Suggested test case
[Fact] public async Task GenerateAndStorePdf_WithDisplayFooter_HideAppNameInPdfExpression_EvaluatesToFalse_FooterShouldContainAppName() { // Arrange _appResources .Setup(s => s.GetLayoutSets()) .Returns("""{"sets":[],"uiSettings":{"hideAppNameInPdf":["equals","a","b"]}}"""); _pdfGeneratorClient.Setup(s => s.GeneratePdf(It.IsAny<Uri>(), It.IsAny<string?>(), It.IsAny<CancellationToken>()) ); _generalSettingsOptions.Value.ExternalAppBaseUrl = "https://{org}.apps.{hostName}/{org}/{app}"; var target = SetupPdfService( pdfGeneratorClient: _pdfGeneratorClient, generalSettingsOptions: _generalSettingsOptions, pdfGeneratorSettingsOptions: Options.Create(new PdfGeneratorSettings { DisplayFooter = true }) ); Instance instance = new() { Id = $"509378/{Guid.NewGuid()}", AppId = "digdir/not-really-an-app", Org = "digdir", Data = [], }; // Act await target.GenerateAndStorePdf(instance, "Task_1", CancellationToken.None); // Assert _pdfGeneratorClient.Verify( s => s.GeneratePdf( It.IsAny<Uri>(), It.Is<string?>(footer => footer != null && footer.Contains("not-really-an-app")), It.IsAny<CancellationToken>() ), Times.Once ); }🤖 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 `@test/Altinn.App.Core.Tests/Internal/Pdf/PdfServiceTests.cs` around lines 479 - 518, The test suite lacks coverage for a layout expression that evaluates to false; add a new unit test (e.g., GenerateAndStorePdf_WithDisplayFooter_HideAppNameInPdfExpression_EvaluatesToFalse_FooterShouldContainAppName) that sets _appResources.GetLayoutSets() to return {"sets":[],"uiSettings":{"hideAppNameInPdf":["equals","a","b"]}}, create the PdfService via SetupPdfService with DisplayFooter = true, call target.GenerateAndStorePdf(instance, "Task_1", CancellationToken.None) and verify _pdfGeneratorClient.GeneratePdf(...) was called with a footer string that contains the app id ("not-really-an-app") to ensure the false-expression path is exercised; mirror the structure/assertions used in the existing true-case test to keep consistency.
🤖 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/Altinn.App.Core/Internal/Pdf/PdfService.cs`:
- Around line 400-461: In GetHideAppNameInPdf, guard against null after
deserializing hideAppName into an Expression: after calling
hideAppName.Deserialize<Expression>(_jsonSerializerOptions) (variable
expression), check if expression is null and if so log a warning (including
context like "hideAppName expression was null") and return false (default)
before calling ExpressionEvaluator.EvaluateExpression; keep existing exception
handlers unchanged.
---
Nitpick comments:
In `@test/Altinn.App.Core.Tests/Internal/Pdf/PdfServiceTests.cs`:
- Around line 479-518: The test suite lacks coverage for a layout expression
that evaluates to false; add a new unit test (e.g.,
GenerateAndStorePdf_WithDisplayFooter_HideAppNameInPdfExpression_EvaluatesToFalse_FooterShouldContainAppName)
that sets _appResources.GetLayoutSets() to return
{"sets":[],"uiSettings":{"hideAppNameInPdf":["equals","a","b"]}}, create the
PdfService via SetupPdfService with DisplayFooter = true, call
target.GenerateAndStorePdf(instance, "Task_1", CancellationToken.None) and
verify _pdfGeneratorClient.GeneratePdf(...) was called with a footer string that
contains the app id ("not-really-an-app") to ensure the false-expression path is
exercised; mirror the structure/assertions used in the existing true-case test
to keep consistency.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 88c79b58-1255-48b3-93de-8090a731fd7c
📒 Files selected for processing (11)
src/Altinn.App.Core/Features/IInstanceDataAccessor.cssrc/Altinn.App.Core/Internal/Data/CleanInstanceDataAccessor.cssrc/Altinn.App.Core/Internal/Data/InstanceDataUnitOfWork.cssrc/Altinn.App.Core/Internal/Data/PreviousDataAccessor.cssrc/Altinn.App.Core/Internal/Expressions/LayoutEvaluatorState.cssrc/Altinn.App.Core/Internal/Expressions/LayoutEvaluatorStateInitializer.cssrc/Altinn.App.Core/Internal/Pdf/PdfService.cstest/Altinn.App.Api.Tests/Controllers/PdfControllerTests.cstest/Altinn.App.Core.Tests/Internal/Pdf/PdfServiceTests.cstest/Altinn.App.Core.Tests/LayoutExpressions/TestUtilities/InstanceDataAccessorFake.cstest/Altinn.App.Core.Tests/PublicApiTests.PublicApi_ShouldNotChange_Unintentionally.verified.txt




Description
The hideAppNameInPdf property determines whether the app name is displayed in the PDF footer.
Frontend implementation: Altinn/app-frontend-react#4173
Needs to be checked for consistency and alignment with next.
Related Issue(s)
Verification
Documentation
Summary by CodeRabbit
New Features
Public API
Tests