Skip to content

ODataException when entity has DateOnly or TimeOnly member #1294

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

Closed
andygjp opened this issue Aug 6, 2024 · 7 comments
Closed

ODataException when entity has DateOnly or TimeOnly member #1294

andygjp opened this issue Aug 6, 2024 · 7 comments
Labels
bug Something isn't working

Comments

@andygjp
Copy link
Contributor

andygjp commented Aug 6, 2024

Assemblies affected
Microsoft.AspNetCore.OData 9.0.0-rc.1
Microsoft.OData.Core 8.0.0-rc.1

Describe the bug
Cannot GET or POST entities that contain DateOnly and TimeOnly types after upgrading to v9 rc1

Error

Here is the error stacktrace:

An unhandled exception has occurred while executing the request.
      Microsoft.OData.ODataException: An ODataPrimitiveValue was instantiated with a value of type 'System.DateOnly'. ODataPrimitiveValue can only wrap values which can be represented as primitive EDM types.
         at Microsoft.OData.ODataPrimitiveValue..ctor(Object value)
         at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataPrimitiveSerializer.CreatePrimitive(Object value, IEdmPrimitiveTypeReference primitiveType, ODataSerializerContext writeContext)
         at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataPrimitiveSerializer.CreateODataPrimitiveValue(Object graph, IEdmPrimitiveTypeReference primitiveType, ODataSerializerContext writeContext)
         at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataPrimitiveSerializer.CreateODataValue(Object graph, IEdmTypeReference expectedType, ODataSerializerContext writeContext)
         at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataSerializerPropertyHelper.CreateProperty(IODataEdmTypeSerializer serializer, Object graph, IEdmTypeReference expectedType, String elementName, ODataSerializerContext writeContext)
         at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataResourceSerializer.CreateStructuralProperty(IEdmStructuralProperty structuralProperty, ResourceContext resourceContext)
         at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataResourceSerializer.CreateStructuralPropertyBag(SelectExpandNode selectExpandNode, ResourceContext resourceContext)
         at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataResourceSerializer.CreateResource(SelectExpandNode selectExpandNode, ResourceContext resourceContext)
         at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataResourceSerializer.WriteResourceAsync(Object graph, ODataWriter writer, ODataSerializerContext writeContext, IEdmTypeReference expectedType)
         at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataResourceSerializer.WriteObjectInlineAsync(Object graph, IEdmTypeReference expectedType, ODataWriter writer, ODataSerializerContext writeContext)
         at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataResourceSetSerializer.WriteResourceSetItemAsync(Object item, IEdmStructuredTypeReference elementType, Boolean isUntypedCollection, IEdmTypeReference resourceSetType, ODataWriter writer, IODataEdmTypeSerializer resourceSerializer, ODataSerializerContext writeContext)
         at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataResourceSetSerializer.WriteResourceSetAsync(IEnumerable enumerable, IEdmTypeReference resourceSetType, ODataWriter writer, ODataSerializerContext writeContext)
         at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataResourceSetSerializer.WriteObjectInlineAsync(Object graph, IEdmTypeReference expectedType, ODataWriter writer, ODataSerializerContext writeContext)
         at Microsoft.AspNetCore.OData.Formatter.Serialization.ODataResourceSetSerializer.WriteObjectAsync(Object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
         at Microsoft.AspNetCore.OData.Formatter.ODataOutputFormatterHelper.WriteToStreamAsync(Type type, Object value, IEdmModel model, ODataVersion version, Uri baseAddress, MediaTypeHeaderValue contentType, HttpRequest request, IHeaderDictionary requestHeaders, IODataSerializerProvider serializerProvider)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeResultAsync>g__Logged|22_0(ResourceInvoker invoker, IActionResult result)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeResultFilters>g__Awaited|28_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
         at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

Reproduce steps
I've created a sample repo: https://github.com/andygjp/DateOnlySerializationError

Expected behavior
I should be able to GET and POST entities that have DateOnly and TimeOnly types

@andygjp andygjp added the bug Something isn't working label Aug 6, 2024
@julealgon
Copy link
Contributor

Duplicate:

@andygjp
Copy link
Contributor Author

andygjp commented Aug 7, 2024

I don't think they are quite the same.

Serialization of DateOnly members has been working for me using Microsoft.AspNetCore.OData 8.2.5.

I've included an image of the response I get when I query my test environment:

image

And an image of the response I receive when I get (minus the $select) my test environment:

image

When I upgrade my development environment to 9 rc1 and execute the same requests, I get:

image

And:

image

I think the error might be related to this piece of code in Microsoft.AspNetCore.OData.Edm.EdmPrimitiveHelper.ConvertPrimitiveValue:

image

Changing #if NET6_0 to #if NET6_0_OR_GREATER might fix it:

image

There are many places that check #if NET6_0, they should probably be checked too:

image

@andygjp
Copy link
Contributor Author

andygjp commented Aug 7, 2024

If I change my sample project to reference my fork of AspNetCoreOData, with all instances of #if NET6_0 on the dev-9.x branch changed to #if NET6_0_OR_GREATER:

image

Then the requests work as expected:

image

@julealgon
Copy link
Contributor

Changing #if NET6_0 to #if NET6_0_OR_GREATER might fix it:

Damn.... those #if checks are indeed really bad. Nice find.

@xuzhg / @habbes , isn't this a low-hanging-fruit type of update the team can do? As @andygjp pointed out, most likely all such checks would need to be updated.

@andygjp
Copy link
Contributor Author

andygjp commented Aug 7, 2024

Or merge my PR: #1296

@MichalWeczorek
Copy link

i vote for fixing this problem and Full Support of DateOnly and TimeOnly

@andygjp
Copy link
Contributor Author

andygjp commented Sep 4, 2024

The PR was merged and I can confirm that my issue has been resolved.

@andygjp andygjp closed this as completed Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants