Skip to content

Commit b6f76a8

Browse files
Copilotjaviercn
andcommitted
Fix recursive type model binding issue by adding missing return statement
Co-authored-by: javiercn <[email protected]>
1 parent 27f93bf commit b6f76a8

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/Components/Endpoints/src/FormMapping/HttpContextFormValueMapper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public void Map(FormValueMappingContext context)
8282
if (!CanMap(context.ValueType, context.AcceptMappingScopeName, context.AcceptFormName))
8383
{
8484
context.SetResult(null);
85+
return;
8586
}
8687

8788
var deserializer = _cache.GetOrAdd(context.ValueType, CreateDeserializer);

src/Components/Endpoints/test/HttpContextFormValueMapperTest.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.Extensions.DependencyInjection;
66
using Microsoft.Extensions.Options;
77
using Microsoft.Extensions.Primitives;
8+
using Microsoft.AspNetCore.Components.Forms.Mapping;
89

910
namespace Microsoft.AspNetCore.Components.Endpoints;
1011

@@ -58,6 +59,47 @@ public void CanMap_SimpleRecursiveModel_ReturnsTrue()
5859
var canMap = mapper.CanMap(typeof(MyModel), "", null);
5960
Assert.True(canMap);
6061
}
62+
63+
[Fact]
64+
public void Map_SetsNullResult_WhenCanMapReturnsFalse()
65+
{
66+
// This test verifies the fix for GitHub issue #61341
67+
// The Map method should return early when CanMap returns false
68+
var formData = new HttpContextFormDataProvider();
69+
// Don't set any form data so CanMap will return false due to no incoming handler name
70+
71+
var mapper = new HttpContextFormValueMapper(formData, Options.Create<RazorComponentsServiceOptions>(new()));
72+
var context = new FormValueMappingContext("", null, typeof(MyModel), "Model");
73+
74+
// Act
75+
mapper.Map(context);
76+
77+
// Assert
78+
Assert.Null(context.Result);
79+
}
80+
81+
[Fact]
82+
public void Map_DoesNotDeserializeWhenCanMapReturnsFalse()
83+
{
84+
// This test demonstrates the original bug and verifies the fix
85+
// Before the fix, Map would call deserializer even when CanMap returned false
86+
var formData = new HttpContextFormDataProvider();
87+
// Set form data but no incoming handler name, so CanMap returns false
88+
formData.SetFormData("", new Dictionary<string, StringValues>
89+
{
90+
["Name"] = "Test"
91+
}, new FormFileCollection());
92+
93+
var mapper = new HttpContextFormValueMapper(formData, Options.Create<RazorComponentsServiceOptions>(new()));
94+
var context = new FormValueMappingContext("mismatched-scope", null, typeof(MyModel), "Model");
95+
96+
// Act
97+
mapper.Map(context);
98+
99+
// Assert - The result should be null because CanMap returns false
100+
// Before the fix, this might have been a MyModel instance due to deserializer running
101+
Assert.Null(context.Result);
102+
}
61103
}
62104

63105
internal class MyModel

0 commit comments

Comments
 (0)