|  | 
| 5 | 5 | using Microsoft.Extensions.DependencyInjection; | 
| 6 | 6 | using Microsoft.Extensions.Options; | 
| 7 | 7 | using Microsoft.Extensions.Primitives; | 
|  | 8 | +using Microsoft.AspNetCore.Components.Forms.Mapping; | 
| 8 | 9 | 
 | 
| 9 | 10 | namespace Microsoft.AspNetCore.Components.Endpoints; | 
| 10 | 11 | 
 | 
| @@ -58,6 +59,47 @@ public void CanMap_SimpleRecursiveModel_ReturnsTrue() | 
| 58 | 59 |         var canMap = mapper.CanMap(typeof(MyModel), "", null); | 
| 59 | 60 |         Assert.True(canMap); | 
| 60 | 61 |     } | 
|  | 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 | +    } | 
| 61 | 103 | } | 
| 62 | 104 | 
 | 
| 63 | 105 | internal class MyModel | 
|  | 
0 commit comments