Skip to content

Commit

Permalink
dont include Merge props in the json
Browse files Browse the repository at this point in the history
  • Loading branch information
adrum committed Dec 21, 2024
1 parent 52ad4b8 commit 7860eea
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
4 changes: 4 additions & 0 deletions InertiaCore/Models/Page.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Text.Json.Serialization;

namespace InertiaCore.Models;

internal class Page
Expand All @@ -6,5 +8,7 @@ internal class Page
public string Component { get; set; } = default!;
public string? Version { get; set; }
public string Url { get; set; } = default!;

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public List<string>? MergeProps { get; set; }
}
5 changes: 5 additions & 0 deletions InertiaCore/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ public Response WithViewData(IDictionary<string, object> viewData)
.Where(resolvedProps.Contains) // Filter only the props that are in the resolved props
.ToList();

if (mergeProps.Count == 0)
{
return null;
}

// Return the result
return mergeProps;
}
Expand Down
5 changes: 3 additions & 2 deletions InertiaCoreTests/UnitTestMergeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void TestMergeAsyncPartialDataOmitted()
{ "errors", new Dictionary<string, string>(0) }
}));

Assert.That(page?.MergeProps, Is.EqualTo(new List<string> { }));
Assert.That(page?.MergeProps, Is.EqualTo(null));
}

public void TestNoMergeProps()
Expand All @@ -201,6 +201,7 @@ public void TestNoMergeProps()
{ "testFunc", "Func" },
{ "errors", new Dictionary<string, string>(0) }
}));
Assert.That(page?.MergeProps, Is.EqualTo(new List<string> { }));
Assert.That(page?.MergeProps, Is.EqualTo(null));
}

}
57 changes: 57 additions & 0 deletions InertiaCoreTests/UnitTestResult.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using InertiaCore.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;

namespace InertiaCoreTests;

Expand Down Expand Up @@ -40,6 +41,62 @@ public void TestJsonResult()
{ "test", "Test" },
{ "errors", new Dictionary<string, string>(0) }
}));

// Check the serialized JSON
var jsonString = JsonSerializer.Serialize(json);
var dictionary = JsonSerializer.Deserialize<Dictionary<string, object?>>(jsonString);

Assert.That(dictionary, Is.Not.Null);
Assert.That(dictionary!.ContainsKey("MergeProps"), Is.False);
});
}
[
Test]
[Description("Test if the JSON result with merged data is created correctly.")]
public void TestJsonMergedResult()
{
var response = _factory.Render("Test/Page", new
{
Test = "Test",
TestMerged = _factory.Merge(() => "Merged")
});

var headers = new HeaderDictionary
{
{ "X-Inertia", "true" }
};

var context = PrepareContext(headers);

response.SetContext(context);
response.ProcessResponse();

var result = response.GetResult();

Assert.Multiple(() =>
{
Assert.That(result, Is.InstanceOf(typeof(JsonResult)));

var json = (result as JsonResult)?.Value;
Assert.That(json, Is.InstanceOf(typeof(Page)));

Assert.That((json as Page)?.Component, Is.EqualTo("Test/Page"));
Assert.That((json as Page)?.Props, Is.EqualTo(new Dictionary<string, object?>
{
{ "test", "Test" },
{ "testMerged", "Merged" },
{ "errors", new Dictionary<string, string>(0) }
}));
Assert.That((json as Page)?.MergeProps, Is.EqualTo(new List<string> {
"testMerged"
}));

// Check the serialized JSON
var jsonString = JsonSerializer.Serialize(json);
var dictionary = JsonSerializer.Deserialize<Dictionary<string, object?>>(jsonString);

Assert.That(dictionary, Is.Not.Null);
Assert.That(dictionary!.ContainsKey("MergeProps"), Is.True);
});
}

Expand Down

0 comments on commit 7860eea

Please sign in to comment.