Skip to content

In Blazor Nullable<TimeSpan> generates error "The value '' is not valid for 'Property'." when value is empty #66607

@neoGeneva

Description

@neoGeneva

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Looks like this is related to the pull request #60498 "Limit types that get converted to null for empty/null input in NullableConverter", looks like TimeSpan should be included in the list.

I ran a script against all the NullableConverter types in WellKnownConverters.cs and it looks like there are several types defined there that won't deserialize to null.

  • Int128
  • UInt128
  • Half
  • TimeSpan
  • Guid
var types = new[]
{
    typeof(char),
    typeof(bool),
    typeof(byte),
    typeof(sbyte),
    typeof(ushort),
    typeof(uint),
    typeof(ulong),
    typeof(Int128),
    typeof(short),
    typeof(int),
    typeof(long),
    typeof(UInt128),
    typeof(Half),
    typeof(float),
    typeof(double),
    typeof(decimal),
    typeof(DateOnly),
    typeof(DateTime),
    typeof(DateTimeOffset),
    typeof(TimeSpan),
    typeof(TimeOnly),
    typeof(Guid),
};

foreach (var type in types)
{
    var isSupportedUnderlyingType = IsSupportedUnderlyingType(type);

    if (!isSupportedUnderlyingType)
        Console.WriteLine(type.Name);
}

static bool IsSupportedUnderlyingType(Type type)
{
    return Type.GetTypeCode(type) != TypeCode.Object || IsSupportedUnderlyingObjectType(type);
}

static bool IsSupportedUnderlyingObjectType(Type type)
{
    return type == typeof(DateOnly) || type == typeof(TimeOnly) || type == typeof(DateTimeOffset);
}

Expected Behavior

A form value of "" should deserialize to null.

Steps To Reproduce

  1. Create a Blazor server side app
  2. Create a model object with a property of type Nullable?
  3. Create a form that has that property in it
  4. Submit the form with the value blank

Exceptions (if any)

No response

.NET Version

10.0.203

Anything else?

No response

Metadata

Metadata

Assignees

Labels

area-blazorIncludes: Blazor, Razor ComponentsbugThis issue describes a behavior which is not expected - a bug.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions