Skip to content

Commit 23ab8f6

Browse files
Copilotjaviercn
andcommitted
Add specific test case reproducing exact GitHub issue #61341 scenario
Co-authored-by: javiercn <[email protected]>
1 parent f62f9e1 commit 23ab8f6

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/Components/Endpoints/test/HttpContextFormValueMapperTest.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,57 @@ public void Map_DoesNotDeserializeWhenCanMapReturnsFalse()
100100
// Before the fix, this might have been a MyModel instance due to deserializer running
101101
Assert.Null(context.Result);
102102
}
103+
104+
[Fact]
105+
public void SupplyParameterFromForm_WithRecursiveModel_ExactGitHubIssueScenario()
106+
{
107+
// Arrange - Reproduce the EXACT scenario from GitHub issue #61341
108+
var formData = new Dictionary<string, StringValues>()
109+
{
110+
["Name"] = "Test Name"
111+
// Note: No data for Parent property, but it should still bind the Name
112+
};
113+
114+
var httpContext = new DefaultHttpContext();
115+
httpContext.Request.Method = "POST";
116+
httpContext.Request.ContentType = "application/x-www-form-urlencoded";
117+
118+
// Set up form data provider to simulate the exact scenario
119+
var formDataProvider = new HttpContextFormDataProvider();
120+
formDataProvider.SetFormData("RequestForm", formData, new FormFileCollection());
121+
122+
// Create the mapper (this will use our fix)
123+
var options = Options.Create(new RazorComponentsServiceOptions());
124+
var mapper = new HttpContextFormValueMapper(formDataProvider, options);
125+
126+
// Act - Try to map the recursive model type (the exact case that was failing)
127+
var context = new FormValueMappingContext("", "RequestForm", typeof(MyModel), "Model");
128+
mapper.Map(context);
129+
130+
// Assert - The model should be created successfully, not null
131+
// Before the fix, this would be null due to the missing return statement
132+
Assert.NotNull(context.Result);
133+
var result = Assert.IsType<MyModel>(context.Result);
134+
Assert.Equal("Test Name", result.Name);
135+
Assert.Null(result.Parent); // Parent should be null since no data was provided
136+
}
137+
138+
[Fact]
139+
public void CanMap_WithRecursiveModel_ShouldReturnTrue()
140+
{
141+
// Arrange - Test the CanMap method specifically for recursive types
142+
var formDataProvider = new HttpContextFormDataProvider();
143+
formDataProvider.SetFormData("RequestForm", new Dictionary<string, StringValues>(), new FormFileCollection());
144+
145+
var options = Options.Create(new RazorComponentsServiceOptions());
146+
var mapper = new HttpContextFormValueMapper(formDataProvider, options);
147+
148+
// Act
149+
var canMap = mapper.CanMap(typeof(MyModel), "", "RequestForm");
150+
151+
// Assert - Should be able to map recursive model types
152+
Assert.True(canMap, "Should be able to map recursive model types");
153+
}
103154
}
104155

105156
internal class MyModel

0 commit comments

Comments
 (0)