TimeSpan? in OData
I wish to learn more about handling TimeSpan. It appears that the default is ISO 8601 duration format.
Assemblies affected
Microsoft.AspNetCore.OData 7.5.4
Reproduce steps
Create an OData Controller and regular scaffolded Web Api Controller using a class with TimeSpan and/or TimeSpan?. I am using Blazor Wasm, thus the need for the JsonTimeSpanConverter. I used Macross Json Extensions.
Expected result
I don't know if it is a feature. I wish only to know the proper way to do this.
Actual result
Without annotations
Api Controller: "standardTime": { "ticks": 900000000, "days": 0, "hours": 0, "milliseconds": 0, "minutes": 1, "seconds": 30, "totalDays": 0.0010416666666666667, "totalHours": 0.025, "totalMilliseconds": 90000, "totalMinutes": 1.5, "totalSeconds": 90 }
OData Controller: "StandardTime": "PT1M30S"
With annotations [Column(TypeName = "Time")] [JsonConverter(typeof(JsonTimeSpanConverter))]
Api Controller: "standardTime": "00:01:30"
OData Controller: "StandardTime": "00:01:30.0000000"
I also could have written my own Json converter as seen in https://github.com/dotnet/runtime/issues/29932#issuecomment-540200476, but couldn't get it to work for both nullable and non nullable TimeSpan. They mention this in https://github.com/dotnet/runtime/issues/30843#issuecomment-555395612 but I found using the previosly mentioned extensions easier.
What is the best way to approach this?
From OData ABNF:
durationValue = [ SIGN ] "P" [ 1DIGIT "D" ] [ "T" [ 1DIGIT "H" ] [ 1DIGIT "M" ] [ 1DIGIT [ "." 1*DIGIT ] "S" ] ] ; the above is an approximation of the rules for an xml dayTimeDuration.
@mikepizzo
From OData ABNF:
durationValue = [ SIGN ] "P" [ 1_DIGIT "D" ] [ "T" [ 1_DIGIT "H" ] [ 1_DIGIT "M" ] [ 1_DIGIT [ "." 1*DIGIT ] "S" ] ] ; the above is an approximation of the rules for an xml dayTimeDuration.
Hey xuzhg, from your reply, does it mean odata supports duration as data type? If yes, what EDM type people should use to define a TimeSpan property and how the TimeSpan property should be used in $filter
@cppcraze Look at the tests from this line https://github.com/OData/odata.net/blob/370cd96aa2a78aecff1b7f9ebe9bebaa7c25ab81/test/FunctionalTests/Microsoft.OData.Core.Tests/ScenarioTests/UriParser/FilterAndOrderByFunctionalTests.cs#L1619
Closing this issue due to inactivity. If this issue still persists, feel free to create a new issue.