simple-odata-client / Simple.OData.Client

MIT License
329 stars 193 forks source link

OData V2 JSON DateTime format issue #618

Open tomlowi opened 5 years ago

tomlowi commented 5 years ago

Hi, we are trying to integrate with the SAP/Successfactors OData APIs via Simple.OData.Client.

Since they are exposing OData v2 services (metadata: edmx:DataServices m:DataServiceVersion="2.0"), they are expecting JSON dates to be passed in unix-style milliseconds since 1/1/1970, i.e. formatted like /Date(1398190210000)/

My tests are getting Bad Request errors with '"Illegal datetime format 2019-05-22T00:00:00+02:00", and indeed I see that Simple.OData.Client is posting the dates in this format. Querying/receiving data does work, we receive dates in JSON-style and they are reflected correctly in our DateTime properties.

To my understanding, the date format should automatically be determined based on the OData protocol version? Is there any way I can force the JSON date format in Simple.OData.Client? Or should this just work automatically derived from the metadata?

I have attached the metadata file in case it is helpful. Please note I also had to implement the workaround for unsupported annotations as already described here: https://github.com/simple-odata-client/Simple.OData.Client/issues/75

Thanks, Iwan api2_successfactors_metadata_original.zip

object commented 5 years ago

Hi, I inspected the library code and I am not sure that this can be (easily) done because the library relies on Microsoft's ODataLib. Here's the code I use:

ODataUriUtils.ConvertToUriLiteral(x, odataVersion, (_session.Adapter as ODataAdapter).Model)

where "x" is a value to be converted and odataVersion is "V2" in this case.

You can validate it yourself if you build the library from sources and set a breakpoint in Simple.OData.Client.V3.Adapter CommandFormatter.cs, method ConvertValueToUriLiteral.

object commented 5 years ago

I also tested sample V2 service offered by OData team (https://services.odata.org/V2/OData/OData.svc)

It didn't require converting datetime to milliseconds, e.g.:

https://services.odata.org/V2/(S(lk53tuiev1hxy33ds34bw5ym))/OData/OData.svc/Products?$filter=ReleaseDate%20gt%20datetime%271998-01-01T00:00:00%27

This one runs and returns results.