Skip to content

Fix exception when passing model: null to the api/pdf endpoint#29

Merged
marcominerva merged 3 commits intomasterfrom
copilot/fix-28
Jul 24, 2025
Merged

Fix exception when passing model: null to the api/pdf endpoint#29
marcominerva merged 3 commits intomasterfrom
copilot/fix-28

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jul 24, 2025

The API was throwing an InvalidOperationException when a user passed model: null in the JSON request body to the /api/pdf endpoint. This occurred because the JsonDocumentExtensions.ConvertElement method only handled JsonValueKind.Object and JsonValueKind.Array types, but not JsonValueKind.Null at the root level.

Issue

When sending a request like this:

{
  "template": "My Template!",
  "model": null,
  "options": {
    "pageSize": "A4",
    "orientation": "Portrait", 
    "margin": null
  },
  "templateEngine": "razor",
  "fileName": null
}

The following exception was thrown:

System.InvalidOperationException: Unsupported JSON ValueKind: Null
   at PdfSmith.BusinessLayer.Extensions.JsonDocumentExtensions.ConvertElement(JsonElement element, TimeZoneInfo timeZoneInfo)

Solution

Modified the ConvertElement method to delegate non-Object/Array cases to the existing ConvertValue method, which already properly handles all JsonValueKind values including Null. This is a minimal change that reuses existing logic and maintains backward compatibility.

Changes

  • Updated ConvertElement to return object? instead of throwing for unsupported value kinds
  • Updated ToExpandoObject return type to object? to properly reflect nullable results
  • Added delegation to ConvertValue for handling all non-Object/Array cases

The fix ensures that model: null is properly handled by returning null instead of throwing an exception, while preserving all existing functionality for object and array models.

Fixes #28.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
Copilot AI changed the title [WIP] Exception when passing model: null to the api/pdf endpoint Fix exception when passing model: null to the api/pdf endpoint Jul 24, 2025
Copilot AI requested a review from marcominerva July 24, 2025 14:31
- Updated `JsonDocumentExtensions.cs` for comment clarity.
- Enhanced null checks and error handling in `PdfService.cs` for template engine and time zone management.
- Allowed nullable model parameters in `HandlebarsTemplateEngine.cs`, `RazorTemplateEngine.cs`, and `ScribanTemplateEngine.cs`.
- Updated `ITemplateEngine.cs` interface for consistency with nullable model parameters.
- Renamed constant in `ScribanTemplateEngine.cs` for improved clarity.
@marcominerva marcominerva marked this pull request as ready for review July 24, 2025 14:52
Copilot AI review requested due to automatic review settings July 24, 2025 14:52
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request fixes an exception that occurred when passing model: null to the /api/pdf endpoint. The main issue was that the JSON deserialization logic in JsonDocumentExtensions.ConvertElement only handled Object and Array types, throwing an InvalidOperationException for null values.

Key changes made:

  • Modified ConvertElement method to delegate non-Object/Array cases to the existing ConvertValue method instead of throwing exceptions
  • Updated template engine interfaces and implementations to accept nullable model parameters
  • Simplified PDF generation logic to handle null models consistently

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
JsonDocumentExtensions.cs Fixed the core issue by delegating null/primitive value handling to existing ConvertValue method
ITemplateEngine.cs Updated interface to accept nullable model parameter
ScribanTemplateEngine.cs Updated implementation to handle nullable models and fixed constant name
RazorTemplateEngine.cs Updated to accept nullable models and removed extra whitespace
HandlebarsTemplateEngine.cs Updated to accept nullable models
PdfService.cs Simplified logic to use null-conditional operator for model conversion
Comments suppressed due to low confidence (1)

src/PdfSmith.BusinessLayer/Templating/ScribanTemplateEngine.cs:13

  • The constant name change from 'date_time_zone' to 'datetime_withzone' appears unrelated to the null model fix. This naming change should be separated into its own PR or better justified in the current context.
    private const string DateTimeZonePlaceholder = "datetime_withzone";

string? content;
try
{
var model = request.Model?.ToExpandoObject(timeZoneInfo);
Copy link

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

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

[nitpick] The logic has been simplified but the control flow could be clearer. Consider handling the null model case explicitly rather than relying on the null-conditional operator, especially since the original code had separate paths for null vs non-null models.

Suggested change
var model = request.Model?.ToExpandoObject(timeZoneInfo);
object? model;
if (request.Model is null)
{
model = null;
}
else
{
model = request.Model.ToExpandoObject(timeZoneInfo);
}

Copilot uses AI. Check for mistakes.
@marcominerva marcominerva merged commit bf158fb into master Jul 24, 2025
5 checks passed
@marcominerva marcominerva deleted the copilot/fix-28 branch July 24, 2025 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exception when passing model: null to the api/pdf endpoint

3 participants