Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Microsoft.AspNetCore.OData.Nightly.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package>
<metadata>
<id>Microsoft.AspNetCore.OData</id>
<title>Microsoft ASP.NET Core 3.x and 5.x for OData v4.0</title>
<title>Microsoft ASP.NET Core 3.x, 5.x and 6.x for OData v4.0</title>
<version>$VersionFullSemantic$-Nightly$NightlyBuildVersion$</version>
<authors>OData (.NET Foundation)</authors>
<copyright>&#169; .NET Foundation and Contributors. All rights reserved.</copyright>
Expand All @@ -28,6 +28,12 @@
<dependency id="Microsoft.OData.Edm" version="$ODataLibPackageDependency$" />
<dependency id="Microsoft.Spatial" version="$ODataLibPackageDependency$" />
</group>
<group targetFramework=".net6.0">
<dependency id="Microsoft.OData.ModelBuilder" version="$ODataModelBuilderPackageDependency$" />
<dependency id="Microsoft.OData.Core" version="$ODataLibPackageDependency$" />
<dependency id="Microsoft.OData.Edm" version="$ODataLibPackageDependency$" />
<dependency id="Microsoft.Spatial" version="$ODataLibPackageDependency$" />
</group>
</dependencies>
</metadata>
<files>
Expand All @@ -37,6 +43,9 @@
<file src="$ProductRoot$\net5.0\Microsoft.AspNetCore.OData.dll" target="lib\net5.0" />
<file src="$ProductRoot$\net5.0\Microsoft.AspNetCore.OData.xml" target="lib\net5.0" />
<file src="$ProductRoot$\net5.0\Microsoft.AspNetCore.OData.pdb" target="lib\net5.0" />
<file src="$ProductRoot$\net6.0\Microsoft.AspNetCore.OData.dll" target="lib\net6.0" />
<file src="$ProductRoot$\net6.0\Microsoft.AspNetCore.OData.xml" target="lib\net6.0" />
<file src="$ProductRoot$\net6.0\Microsoft.AspNetCore.OData.pdb" target="lib\net6.0" />
<file src="$SourcesRoot$\images\odata.png" target="images\" />
</files>
</package>
13 changes: 11 additions & 2 deletions src/Microsoft.AspNetCore.OData.Release.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package>
<metadata>
<id>Microsoft.AspNetCore.OData</id>
<title>Microsoft ASP.NET Core 3.x and 5.x for OData v4.0</title>
<title>Microsoft ASP.NET Core 3.x, 5.x and 6.x for OData v4.0</title>
<version>$VersionNuGetSemantic$</version>
<authors>OData (.NET Foundation)</authors>
<copyright>&#169; .NET Foundation and Contributors. All rights reserved.</copyright>
Expand All @@ -22,7 +22,13 @@
<dependency id="Microsoft.OData.Edm" version="$ODataLibPackageDependency$" />
<dependency id="Microsoft.Spatial" version="$ODataLibPackageDependency$" />
</group>
<group targetFramework=".net5.0">
<group targetFramework=".net5.0">
<dependency id="Microsoft.OData.ModelBuilder" version="$ODataModelBuilderPackageDependency$" />
<dependency id="Microsoft.OData.Core" version="$ODataLibPackageDependency$" />
<dependency id="Microsoft.OData.Edm" version="$ODataLibPackageDependency$" />
<dependency id="Microsoft.Spatial" version="$ODataLibPackageDependency$" />
</group>
<group targetFramework=".net6.0">
<dependency id="Microsoft.OData.ModelBuilder" version="$ODataModelBuilderPackageDependency$" />
<dependency id="Microsoft.OData.Core" version="$ODataLibPackageDependency$" />
<dependency id="Microsoft.OData.Edm" version="$ODataLibPackageDependency$" />
Expand All @@ -37,6 +43,9 @@
<file src="$ProductRoot$\net5.0\Microsoft.AspNetCore.OData.dll" target="lib\net5.0" />
<file src="$ProductRoot$\net5.0\Microsoft.AspNetCore.OData.xml" target="lib\net5.0" />
<file src="$ProductRoot$\net5.0\Microsoft.AspNetCore.OData.pdb" target="lib\net5.0" />
<file src="$ProductRoot$\net6.0\Microsoft.AspNetCore.OData.dll" target="lib\net6.0" />
<file src="$ProductRoot$\net6.0\Microsoft.AspNetCore.OData.xml" target="lib\net6.0" />
<file src="$ProductRoot$\net6.0\Microsoft.AspNetCore.OData.pdb" target="lib\net6.0" />
<file src="$SourcesRoot$\images\odata.png" target="images\" />
</files>
</package>
24 changes: 24 additions & 0 deletions src/Microsoft.AspNetCore.OData/Common/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,30 @@ public static bool IsDateTime(Type clrType)
return Type.GetTypeCode(underlyingTypeOrSelf) == TypeCode.DateTime;
}

#if NET6_0

@julealgon julealgon Jan 20, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered moving NET6-specific members into a partial file? That might make it easier to manage in the future. Something like TypeHelper.Net6.cs for example. #WontFix

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there's only two new methods added, i'd like to create a new file until we have more .NET 6 specify methods added. Thanks for your suggestion.

/// <summary>
/// Determine if a type is a <see cref="DateOnly"/>.
/// </summary>
/// <param name="clrType">The type to test.</param>
/// <returns>True if the type is a DateOnly; false otherwise.</returns>
public static bool IsDateOnly(this Type clrType)
{
Type underlyingTypeOrSelf = GetUnderlyingTypeOrSelf(clrType);
return underlyingTypeOrSelf == typeof(DateOnly);
}

/// <summary>
/// Determine if a type is a <see cref="TimeOnly"/>.
/// </summary>
/// <param name="clrType">The type to test.</param>
/// <returns>True if the type is a TimeOnly; false otherwise.</returns>
public static bool IsTimeOnly(this Type clrType)
{
Type underlyingTypeOrSelf = GetUnderlyingTypeOrSelf(clrType);
return underlyingTypeOrSelf == typeof(TimeOnly);
}
#endif

@julealgon julealgon Jan 20, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to leave an extra blank line after this. #Resolved


/// <summary>
/// Determine if a type is a TimeSpan.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions src/Microsoft.AspNetCore.OData/Edm/DefaultODataTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ static DefaultODataTypeMapper()
BuildTypeMapping<char>(EdmPrimitiveTypeKind.String, isStandard: false);
BuildTypeMapping<DateTime?>(EdmPrimitiveTypeKind.DateTimeOffset, isStandard: false);
BuildTypeMapping<DateTime>(EdmPrimitiveTypeKind.DateTimeOffset, isStandard: false);

#if NET6_0
BuildTypeMapping<DateOnly?>(EdmPrimitiveTypeKind.Date, isStandard: false);

@julealgon julealgon Jan 20, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume there is no behavior difference in the order we add these mappings, but I'd still suggest adding the nullable versions prior to the non-nullable ones for consistency: all other types in this method are following that pattern.

FAKE EDIT: Actually, scratch that: the order does seem to matter, as per the comment at the top of the method:

// Do not change the order for the nullable or non-nullable. Put nullable ahead of non-nullable.
// By design: non-nullable will overwrite the item1.

I'd even go as far as to suggest a small method to encapsulate this, something like:

        public void BuildStructTypeMapping<T>(EdmPrimitiveTypeKind edmPrimitiveTypeKind, bool isStandard)
            where T : struct
        {
            BuildTypeMapping<T?>(edmPrimitiveTypeKind, isStandard);
            BuildTypeMapping<T>(edmPrimitiveTypeKind, isStandard);
        }

Using this for all structs in the method would simplify it quite a bit and centralize the order concern (the comment could be even moved over into this method). #Resolved

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind sharing with me a contribution for this? I'd love to take and merge it for you.

@julealgon julealgon Jan 20, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure. I'll wait until you merge this and then refactor the whole block so as to avoid you having to rebase it.

For now, just make sure the order is as per the comment and we should be good.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right for the order. I forget what i said. OMG

BuildTypeMapping<DateOnly>(EdmPrimitiveTypeKind.Date, isStandard: false);
BuildTypeMapping<TimeOnly?>(EdmPrimitiveTypeKind.TimeOfDay, isStandard: false);
BuildTypeMapping<TimeOnly>(EdmPrimitiveTypeKind.TimeOfDay, isStandard: false);
#endif
}
#endregion

Expand Down
20 changes: 20 additions & 0 deletions src/Microsoft.AspNetCore.OData/Edm/EdmPrimitiveHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,26 @@ public static object ConvertPrimitiveValue(object value, Type type, TimeZoneInfo

throw new ValidationException(Error.Format(SRResources.PropertyMustBeBoolean));
}
#if NET6_0
else if (type == typeof(DateOnly))

@julealgon julealgon Jan 20, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice if this could be simplified using a switch expression instead of all the else ifs.

I understand it is a bit unrelated though, so up to you. #WontFix

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion, i'd like to leave for other PR.

{
if (value is Date dt)
{
return new DateOnly(dt.Year, dt.Month, dt.Day);
}

throw new ValidationException(Error.Format(SRResources.PropertyMustBeDateTimeOffsetOrDate));
}
else if (type == typeof(TimeOnly))
{
if (value is TimeOfDay tod)
{
return new TimeOnly(tod.Hours, tod.Minutes, tod.Seconds, (int)tod.Milliseconds);

@julealgon julealgon Jan 20, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed the restriction to int and took a look just to make sure...

Man this is really wrong, the EDM type property really shouldn't have been long in the first place here:
image
#WontFix

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, It's ODL problem and now i have to cast it.

}

throw new ValidationException(Error.Format(SRResources.PropertyMustBeTimeOfDay));
}
#endif
else
{
if (TypeHelper.TryGetInstance(type, value, out var result))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -102,6 +104,7 @@ private static ODataInnerError ToODataInnerError(this Dictionary<string, object>

// Convert the model state errors in to a string (for debugging only).
// This should be improved once ODataError allows more details.
[SuppressMessage("Globalization", "CA1305:Specify IFormatProvider", Justification = "The default format provider is fine here.")]
private static string ConvertModelStateErrors(this IReadOnlyDictionary<string, object> errors)
{
StringBuilder builder = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,22 @@ internal static object ConvertPrimitiveValue(object value, IEdmPrimitiveTypeRefe
return tod;
}

#if NET6_0
// Since ODL doesn't support "DateOnly", we have to use Date defined in ODL.
if (primitiveType != null && primitiveType.IsDate() && TypeHelper.IsDateOnly(type))
{
DateOnly dateOnly = (DateOnly)value;
return new Date(dateOnly.Year, dateOnly.Month, dateOnly.Day);
}

// Since ODL doesn't support "TimeOnly", we have to use TimeOfDay defined in ODL.
if (primitiveType != null && primitiveType.IsTimeOfDay() && TypeHelper.IsTimeOnly(type))
{
TimeOnly timeOnly = (TimeOnly)value;
return new TimeOfDay(timeOnly.Hour, timeOnly.Minute, timeOnly.Second, timeOnly.Millisecond);
}
#endif

return ConvertUnsupportedPrimitives(value, timeZoneInfo);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<RootNamespace>Microsoft.AspNetCore.OData</RootNamespace>
<DocumentationFile>$(OutputPath)$(AssemblyName).xml</DocumentationFile>
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
Expand Down
14 changes: 14 additions & 0 deletions src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,20 @@
<param name="clrType">The type to test.</param>
<returns>True if the type is a DateTime; false otherwise.</returns>
</member>
<member name="M:Microsoft.AspNetCore.OData.Common.TypeHelper.IsDateOnly(System.Type)">
<summary>
Determine if a type is a <see cref="T:System.DateOnly"/>.
</summary>
<param name="clrType">The type to test.</param>
<returns>True if the type is a DateOnly; false otherwise.</returns>
</member>
<member name="M:Microsoft.AspNetCore.OData.Common.TypeHelper.IsTimeOnly(System.Type)">
<summary>
Determine if a type is a <see cref="T:System.TimeOnly"/>.
</summary>
<param name="clrType">The type to test.</param>
<returns>True if the type is a TimeOnly; false otherwise.</returns>
</member>
<member name="M:Microsoft.AspNetCore.OData.Common.TypeHelper.IsTimeSpan(System.Type)">
<summary>
Determine if a type is a TimeSpan.
Expand Down
19 changes: 19 additions & 0 deletions src/Microsoft.AspNetCore.OData/Query/ClrCanonicalFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ internal class ClrCanonicalFunctions
new KeyValuePair<string, PropertyInfo>(MillisecondFunctionName, typeof(TimeOfDay).GetProperty("Milliseconds")),
}.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

#if NET6_0
// DateOnly properties
public static readonly Dictionary<string, PropertyInfo> DateOnlyProperties = new Dictionary<string, PropertyInfo>
{
{ YearFunctionName, typeof(DateOnly).GetProperty(nameof(DateOnly.Year)) },
{ MonthFunctionName, typeof(DateOnly).GetProperty(nameof(DateOnly.Month)) },
{ DayFunctionName, typeof(DateOnly).GetProperty(nameof(DateOnly.Day)) }
};

// TimeOnly properties
public static readonly Dictionary<string, PropertyInfo> TimeOnlyProperties = new Dictionary<string, PropertyInfo>
{
{ HourFunctionName, typeof(TimeOnly).GetProperty(nameof(TimeOnly.Hour)) },
{ MinuteFunctionName, typeof(TimeOnly).GetProperty(nameof(TimeOnly.Minute)) },
{ SecondFunctionName, typeof(TimeOnly).GetProperty(nameof(TimeOnly.Second)) },
{ MillisecondFunctionName, typeof(TimeOnly).GetProperty(nameof(TimeOnly.Millisecond)) }
};
#endif

// TimeSpan properties
public static readonly Dictionary<string, PropertyInfo> TimeSpanProperties = new[]
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ public static Expression CreateBinaryExpression(BinaryOperatorKind binaryOperato
right = CreateTimeBinaryExpression(right, querySettings);
}

#if NET6_0
if ((IsType<DateOnly>(leftUnderlyingType) && IsDate(rightUnderlyingType)) ||
(IsDate(leftUnderlyingType) && IsType<DateOnly>(rightUnderlyingType)))
{
left = CreateDateBinaryExpression(left, querySettings);
right = CreateDateBinaryExpression(right, querySettings);
}
else if((IsType<TimeOnly>(leftUnderlyingType) && IsTimeOfDay(rightUnderlyingType)) ||
(IsTimeOfDay(leftUnderlyingType) && IsType<TimeOnly>(rightUnderlyingType)))
{
left = CreateTimeBinaryExpression(left, querySettings);
right = CreateTimeBinaryExpression(right, querySettings);
}
#endif

if (left.Type != right.Type)
{
// one of them must be nullable and the other is not.
Expand Down Expand Up @@ -381,6 +396,16 @@ private static Expression GetProperty(Expression source, string propertyName, OD
{
return MakePropertyAccess(ClrCanonicalFunctions.TimeSpanProperties[propertyName], source, querySettings);
}
#if NET6_0
else if (IsType<DateOnly>(source.Type))
{
return MakePropertyAccess(ClrCanonicalFunctions.DateOnlyProperties[propertyName], source, querySettings);
}
else if (IsType<TimeOnly>(source.Type))
{
return MakePropertyAccess(ClrCanonicalFunctions.TimeOnlyProperties[propertyName], source, querySettings);
}
#endif

return source;
}
Expand Down Expand Up @@ -413,9 +438,9 @@ private static Expression CreateTimeBinaryExpression(Expression source, ODataQue
Expression second = GetProperty(source, ClrCanonicalFunctions.SecondFunctionName, querySettings);
Expression milliSecond = GetProperty(source, ClrCanonicalFunctions.MillisecondFunctionName, querySettings);

Expression hourTicks = Expression.Multiply(Expression.Convert(hour, typeof(long)), Expression.Constant(TimeOfDay.TicksPerHour));
Expression minuteTicks = Expression.Multiply(Expression.Convert(minute, typeof(long)), Expression.Constant(TimeOfDay.TicksPerMinute));
Expression secondTicks = Expression.Multiply(Expression.Convert(second, typeof(long)), Expression.Constant(TimeOfDay.TicksPerSecond));
Expression hourTicks = Expression.Multiply(Expression.Convert(hour, typeof(long)), Expression.Constant(TimeSpan.TicksPerHour, typeof(long)));
Expression minuteTicks = Expression.Multiply(Expression.Convert(minute, typeof(long)), Expression.Constant(TimeSpan.TicksPerMinute, typeof(long)));
Expression secondTicks = Expression.Multiply(Expression.Convert(second, typeof(long)), Expression.Constant(TimeSpan.TicksPerSecond, typeof(long)));

// return (hour * TicksPerHour + minute * TicksPerMinute + second * TicksPerSecond + millisecond)
Expression result = Expression.Add(hourTicks, Expression.Add(minuteTicks, Expression.Add(secondTicks, Expression.Convert(milliSecond, typeof(long)))));
Expand Down Expand Up @@ -451,6 +476,18 @@ private static Expression ConvertToDateTimeRelatedConstExpression(Expression sou
{
return Expression.Constant(timeOfDay.Value, typeof(TimeOfDay));
}

#if NET6_0
if (parameterizedConstantValue is DateOnly dateOnly)
{
return Expression.Constant(dateOnly, typeof(DateOnly));
}

else if (parameterizedConstantValue is TimeOnly timeOnly)
{
return Expression.Constant(timeOnly, typeof(TimeOnly));
}
#endif
}

return source;
Expand All @@ -468,21 +505,34 @@ public static bool IsDoubleOrDecimal(Type type)

public static bool IsDateAndTimeRelated(Type type)
{
return IsType<Date>(type) ||
IsType<DateTime>(type) ||
IsType<DateTimeOffset>(type) ||
IsType<TimeOfDay>(type) ||
IsType<TimeSpan>(type);
return IsType<Date>(type)
|| IsType<DateTime>(type)
|| IsType<DateTimeOffset>(type)
|| IsType<TimeOfDay>(type)
|| IsType<TimeSpan>(type)
#if NET6_0
|| IsType<DateOnly>(type)
|| IsType<TimeOnly>(type)
#endif
;
}

public static bool IsDateRelated(Type type)
{
#if NET6_0
return IsType<Date>(type) || IsType<DateTime>(type) || IsType<DateTimeOffset>(type) || IsType<DateOnly>(type);
#else
return IsType<Date>(type) || IsType<DateTime>(type) || IsType<DateTimeOffset>(type);
#endif
}

public static bool IsTimeRelated(Type type)
{
#if NET6_0
return IsType<TimeOfDay>(type) || IsType<DateTime>(type) || IsType<DateTimeOffset>(type) || IsType<TimeSpan>(type) || IsType<TimeOnly>(type);
#else
return IsType<TimeOfDay>(type) || IsType<DateTime>(type) || IsType<DateTimeOffset>(type) || IsType<TimeSpan>(type);
#endif
}

public static bool IsDateOrOffset(Type type)
Expand Down Expand Up @@ -510,6 +560,18 @@ public static bool IsDate(Type type)
return IsType<Date>(type);
}

#if NET6_0
public static bool IsDateOnly(this Type type)
{
return IsType<DateOnly>(type);
}

public static bool IsTimeOnly(this Type type)
{
return IsType<TimeOnly>(type);
}
#endif

public static bool IsInteger(Type type)
{
return IsType<short>(type) || IsType<int>(type) || IsType<long>(type);
Expand Down
Loading