Skip to content

Commit b4b4749

Browse files
committed
Merge branch 'releases/8.x.x'
2 parents 27f56c8 + 45e79e9 commit b4b4749

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Copyright>(c) $([System.DateTime]::Now.Year), Pawel Gerr. All rights reserved.</Copyright>
5-
<VersionPrefix>9.0.0</VersionPrefix>
5+
<VersionPrefix>9.0.1</VersionPrefix>
66
<Authors>Pawel Gerr</Authors>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<PackageProjectUrl>https://github.com/PawelGerr/Thinktecture.Runtime.Extensions</PackageProjectUrl>

src/Thinktecture.Runtime.Extensions.AspNetCore/AspNetCore/ModelBinding/ThinktectureModelBinderBase.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
3737
ArgumentNullException.ThrowIfNull(bindingContext);
3838

3939
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
40+
41+
if (valueProviderResult == ValueProviderResult.None)
42+
return Task.CompletedTask;
43+
4044
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);
4145

4246
try

test/Thinktecture.Runtime.Extensions.AspNetCore.Tests/AspNetCore/ModelBinding/ThinktectureModelBinderTests/BindModelAsync.cs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,49 +22,46 @@ public class BindModelAsync
2222
public void Should_try_bind_enum_when_value_is_null_or_default()
2323
{
2424
// class - int
25-
Bind<SmartEnum_IntBased>(null).Should().Be(null);
25+
BindNot<SmartEnum_IntBased>(null);
2626
FluentActions.Invoking(() => Bind<SmartEnum_IntBased>("0")).Should().Throw<Exception>().WithMessage("There is no item of type 'SmartEnum_IntBased' with the identifier '0'.");
2727

2828
// class - string
29-
Bind<SmartEnum_StringBased>(null).Should().Be(null);
29+
BindNot<SmartEnum_StringBased>(null);
3030

3131
// class - class
32-
Bind<SmartEnum_ClassBased>(null).Should().Be(null);
32+
BindNot<SmartEnum_ClassBased>(null);
3333
}
3434

3535
[Fact]
3636
public void Should_try_bind_keyed_value_object_when_value_is_null_or_default()
3737
{
3838
// class - int
39-
Bind<IntBasedReferenceValueObject>(null).Should().Be(null);
39+
BindNot<IntBasedReferenceValueObject>(null);
4040

4141
// class - string
42-
Bind<StringBasedReferenceValueObject>(null).Should().Be(null);
42+
BindNot<StringBasedReferenceValueObject>(null);
4343

4444
// class - class
45-
Bind<ClassBasedReferenceValueObject>(null).Should().Be(null);
45+
BindNot<ClassBasedReferenceValueObject>(null);
4646

4747
// nullable struct - int
48-
Bind<IntBasedStructValueObject?>(null).Should().Be(null);
48+
BindNot<IntBasedStructValueObject?>(null);
4949
Bind<IntBasedStructValueObject?>("0").Should().Be(IntBasedStructValueObject.Create(0));
5050

5151
// struct - int
5252
Bind<IntBasedStructValueObject>("0").Should().Be(IntBasedStructValueObject.Create(0)); // AllowDefaultStructs = true
53-
FluentActions.Invoking(() => Bind<IntBasedStructValueObject>(null))
54-
.Should().Throw<Exception>().WithMessage("Cannot convert null to type \"IntBasedStructValueObject\".");
53+
BindNot<IntBasedStructValueObject>(null);
5554

5655
Bind<IntBasedStructValueObjectDoesNotAllowDefaultStructs>("0").Should().Be(IntBasedStructValueObjectDoesNotAllowDefaultStructs.Create(0)); // AllowDefaultStructs = true
5756

5857
// nullable struct - string
59-
Bind<StringBasedStructValueObject?>(null).Should().Be(null);
58+
BindNot<StringBasedStructValueObject?>(null);
6059

6160
// struct - string
62-
FluentActions.Invoking(() => Bind<StringBasedStructValueObject>(null)) // AllowDefaultStructs = false
63-
.Should().Throw<Exception>().WithMessage("Cannot convert null to type \"StringBasedStructValueObject\" because it doesn't allow default values.");
61+
BindNot<StringBasedStructValueObject>(null); // AllowDefaultStructs = false;
6462

6563
// struct - class
66-
FluentActions.Invoking(() => Bind<ReferenceTypeBasedStructValueObjectDoesNotAllowDefaultStructs>(null)) // AllowDefaultStructs = false
67-
.Should().Throw<Exception>().WithMessage("Cannot convert a string to type \"ReferenceTypeBasedStructValueObjectDoesNotAllowDefaultStructs\".");
64+
BindNot<ReferenceTypeBasedStructValueObjectDoesNotAllowDefaultStructs>(null); // AllowDefaultStructs = false
6865
}
6966

7067
[Fact]
@@ -137,12 +134,13 @@ public async Task Should_bind_string_based_value_type_with_NullInFactoryMethodsY
137134
}
138135

139136
[Fact]
140-
public async Task Should_bind_null_with_NullInFactoryMethodsYieldsNull()
137+
public async Task Should_not_set_model_when_trying_to_bind_null()
141138
{
139+
// ASP.NET or rather IQueryCollection doesn't distinguish between null and none
142140
var ctx = await BindAsync<StringBasedReferenceValueObjectWithNullInFactoryMethodsYieldsNull>(null);
143141

144142
ctx.ModelState.ErrorCount.Should().Be(0);
145-
ctx.Result.IsModelSet.Should().BeTrue();
143+
ctx.Result.IsModelSet.Should().BeFalse();
146144
ctx.Result.Model.Should().BeNull();
147145
}
148146

@@ -179,10 +177,11 @@ public async Task Should_bind_string_based_value_type_with_StringBasedReferenceV
179177
[Fact]
180178
public async Task Should_bind_null_with_StringBasedReferenceValueObjectWithEmptyStringInFactoryMethodsYieldsNull()
181179
{
180+
// ASP.NET or rather IQueryCollection doesn't distinguish between null and none
182181
var ctx = await BindAsync<StringBasedReferenceValueObjectWithEmptyStringInFactoryMethodsYieldsNull>(null);
183182

184183
ctx.ModelState.ErrorCount.Should().Be(0);
185-
ctx.Result.IsModelSet.Should().BeTrue();
184+
ctx.Result.IsModelSet.Should().BeFalse();
186185
ctx.Result.Model.Should().BeNull();
187186
}
188187

@@ -299,6 +298,21 @@ private static T Bind<T>(string value)
299298
return (T)context.Result.Model;
300299
}
301300

301+
private static void BindNot<T>(string value)
302+
{
303+
// ASP.NET or rather IQueryCollection doesn't distinguish between null and none
304+
var context = BindAsync<T>(value).GetAwaiter().GetResult();
305+
306+
if (!context.ModelState.IsValid)
307+
throw new Exception(context.ModelState["name"]!.Errors[0].ErrorMessage);
308+
309+
if (context.Result.IsModelSet)
310+
throw new Exception("Model msut not be set");
311+
312+
if (context.Result.Model is not null)
313+
throw new Exception("Model must be null");
314+
}
315+
302316
private static async Task<DefaultModelBindingContext> BindAsync<T>(string value)
303317
{
304318
var query = new Dictionary<string, StringValues> { { "name", value } };

0 commit comments

Comments
 (0)