-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Treating empty string in form post as null for nullable value types #52499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
src/Components/Endpoints/test/FormMapping/Converters/NullableConverterTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Globalization; | ||
using Microsoft.AspNetCore.Components.Endpoints.FormMapping; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace Microsoft.AspNetCore.Components.Endpoints.Tests.FormMapping; | ||
|
||
public class NullableConverterTests | ||
{ | ||
[Fact] | ||
public void TryConvertValue_ForDateOnlyReturnsTrueWithNullForEmptyString() | ||
{ | ||
var culture = CultureInfo.GetCultureInfo("en-US"); | ||
|
||
var nullableConverter = new NullableConverter<DateOnly>(new ParsableConverter<DateOnly>()); | ||
var reader = new FormDataReader(default, culture, default); | ||
|
||
var returnValue = nullableConverter.TryConvertValue(ref reader, string.Empty, out var result); | ||
|
||
Assert.True(returnValue); | ||
Assert.Null(result); | ||
} | ||
|
||
[Fact] | ||
public void TryConvertValue_ForDateOnlyReturnsTrueWithDateForRealDateValue() | ||
{ | ||
var date = new DateOnly(2023, 11, 30); | ||
var culture = CultureInfo.GetCultureInfo("en-US"); | ||
|
||
var nullableConverter = new NullableConverter<DateOnly>(new ParsableConverter<DateOnly>()); | ||
var reader = new FormDataReader(default, culture, default); | ||
|
||
var returnValue = nullableConverter.TryConvertValue(ref reader, date.ToString(culture), out var result); | ||
|
||
Assert.True(returnValue); | ||
Assert.Equal(date, result); | ||
} | ||
|
||
[Fact] | ||
public void TryConvertValue_ForDateOnlyReturnsFalseWithNullForBadDateValue() | ||
{ | ||
var culture = CultureInfo.GetCultureInfo("en-US"); | ||
|
||
var nullableConverter = new NullableConverter<DateOnly>(new ParsableConverter<DateOnly>()); | ||
var reader = new FormDataReader(default, culture, default) | ||
{ | ||
ErrorHandler = (_, __, ___) => { } | ||
}; | ||
|
||
var returnValue = nullableConverter.TryConvertValue(ref reader, "bad date", out var result); | ||
|
||
Assert.False(returnValue); | ||
Assert.Null(result); | ||
} | ||
|
||
[Fact] | ||
public void TryRead_ForDateOnlyReturnsFalseWithNullForNoValue() | ||
{ | ||
const string prefixName = "field"; | ||
var culture = CultureInfo.GetCultureInfo("en-US"); | ||
|
||
var dictionary = new Dictionary<FormKey, StringValues>(); | ||
var buffer = prefixName.ToCharArray().AsMemory(); | ||
var reader = new FormDataReader(dictionary, culture, buffer); | ||
reader.PushPrefix(prefixName); | ||
|
||
var nullableConverter = new NullableConverter<DateOnly>(new ParsableConverter<DateOnly>()); | ||
|
||
var returnValue = nullableConverter.TryRead(ref reader, typeof(DateOnly?), default, out var result, out var found); | ||
|
||
Assert.False(found); | ||
Assert.True(returnValue); | ||
Assert.Null(result); | ||
} | ||
|
||
[Fact] | ||
public void TryRead_ForDateOnlyReturnsTrueWithNullForEmptyString() | ||
{ | ||
const string prefixName = "field"; | ||
var culture = CultureInfo.GetCultureInfo("en-US"); | ||
|
||
var dictionary = new Dictionary<FormKey, StringValues>() | ||
{ | ||
{ new FormKey(prefixName.AsMemory()), (StringValues)string.Empty } | ||
}; | ||
var buffer = prefixName.ToCharArray().AsMemory(); | ||
var reader = new FormDataReader(dictionary, culture, buffer); | ||
reader.PushPrefix(prefixName); | ||
|
||
var nullableConverter = new NullableConverter<DateOnly>(new ParsableConverter<DateOnly>()); | ||
|
||
var returnValue = nullableConverter.TryRead(ref reader, typeof(DateOnly?), default, out var result, out var found); | ||
|
||
Assert.True(found); | ||
Assert.True(returnValue); | ||
Assert.Null(result); | ||
} | ||
|
||
[Fact] | ||
public void TryRead_ForDateOnlyReturnsTrueWithDateForRealDateValue() | ||
{ | ||
const string prefixName = "field"; | ||
var date = new DateOnly(2023, 11, 30); | ||
var culture = CultureInfo.GetCultureInfo("en-US"); | ||
|
||
var dictionary = new Dictionary<FormKey, StringValues>() | ||
{ | ||
{ new FormKey(prefixName.AsMemory()), (StringValues)date.ToString(culture) } | ||
}; | ||
var buffer = prefixName.ToCharArray().AsMemory(); | ||
var reader = new FormDataReader(dictionary, culture, buffer); | ||
reader.PushPrefix(prefixName); | ||
|
||
var nullableConverter = new NullableConverter<DateOnly>(new ParsableConverter<DateOnly>()); | ||
|
||
var returnValue = nullableConverter.TryRead(ref reader, typeof(DateOnly?), default, out var result, out var found); | ||
|
||
Assert.True(found); | ||
Assert.True(returnValue); | ||
Assert.Equal(date, result); | ||
} | ||
|
||
[Fact] | ||
public void TryRead_ForDateOnlyReturnsFalseWithNullForBadDateValue() | ||
{ | ||
const string prefixName = "field"; | ||
var culture = CultureInfo.GetCultureInfo("en-US"); | ||
|
||
var dictionary = new Dictionary<FormKey, StringValues>() | ||
{ | ||
{ new FormKey(prefixName.AsMemory()), (StringValues)"bad date" } | ||
}; | ||
var buffer = prefixName.ToCharArray().AsMemory(); | ||
var reader = new FormDataReader(dictionary, culture, buffer) | ||
{ | ||
ErrorHandler = (_, __, ___) => { } | ||
}; | ||
reader.PushPrefix(prefixName); | ||
|
||
var nullableConverter = new NullableConverter<DateOnly>(new ParsableConverter<DateOnly>()); | ||
|
||
var returnValue = nullableConverter.TryRead(ref reader, typeof(DateOnly?), default, out var result, out var found); | ||
|
||
Assert.True(found); | ||
Assert.False(returnValue); | ||
Assert.Null(result); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...stassets/Components.TestServer/RazorComponents/Pages/Forms/FormWithNullableDateTime.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
@page "/forms/with-nullable-datetime" | ||
@using Microsoft.AspNetCore.Components.Forms | ||
<h3>Edit Form With Nullable DateTime</h3> | ||
<EditForm Enhance Model="@this.Model" method="post" id="nullable" FormName="nullable-datetime-testform" OnValidSubmit="this.HandleSubmit"> | ||
<ValidationSummary /> | ||
<div> | ||
<label for="Model.NullableDateTime"> | ||
Date: | ||
<input type="date" id="datetime" name="Model.NullableDateTime" @bind-value="this.Model.NullableDateTime" /> | ||
</label> | ||
<ValidationMessage For="() => Model.NullableDateTime" /> | ||
</div> | ||
<button id="send" type="submit">Submit</button> | ||
</EditForm> | ||
@code { | ||
[SupplyParameterFromForm] | ||
public FormObject Model | ||
{ | ||
get; | ||
set; | ||
} | ||
|
||
protected override void OnInitialized() | ||
{ | ||
base.OnInitialized(); | ||
|
||
this.Model ??= new FormObject(); | ||
} | ||
|
||
private void HandleSubmit() | ||
{ | ||
} | ||
|
||
public class FormObject | ||
{ | ||
public DateTime? NullableDateTime | ||
{ | ||
get; | ||
set; | ||
} | ||
}; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using a null suppression here, perhaps we should add [NotNullWhen] annotations to the
FormDataReader.TryGetValue
method.