Skip to content
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

Aggregate on DateTime property throws exception #2776

Open
adzhiljano opened this issue Apr 20, 2023 · 0 comments
Open

Aggregate on DateTime property throws exception #2776

adzhiljano opened this issue Apr 20, 2023 · 0 comments
Assignees
Labels

Comments

@adzhiljano
Copy link
Contributor

When using a DateTime property as part of an aggregate transformation an exception is thrown. Pull request #2302 fixes the same issue but just when the property is used inside a groupby transformation.

Assemblies affected

Microsoft.AspNet.OData 7.6.5
Microsoft.OData.Core 7.14.0
Microsoft.OData.Edm 7.14.0

Reproduce steps

/SomeEntitySet?$apply=aggregate(SomeDateTimeProperty with min as MinDateTime)
/SomeEntitySet?$apply=groupby((SomeProperty), aggregate(SomeDateTimeProperty with min as MinDateTime))

Expected result

No exception is thrown

Actual result

message: "An ODataPrimitiveValue was instantiated with a value of type 'System.DateTime'. ODataPrimitiveValue can only wrap values which can be represented as primitive EDM types.",
stacktrace: "
   at Microsoft.OData.ODataPrimitiveValue..ctor(Object value) in ...\Microsoft.OData.Core\Value\ODataPrimitiveValue.cs:line 34
   at Microsoft.OData.ODataValueUtils.ToODataValue(Object objectToConvert) in ...\Microsoft.OData.Core\Value\ODataValueUtils.cs:line 49
   at Microsoft.OData.ODataProperty.set_Value(Object value) in ...\Microsoft.OData.Core\ODataProperty.cs:line 30
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.CreateODataPropertiesFromDynamicType(EdmEntityType entityType, Object graph, Dictionary`2 dynamicTypeProperties) in ...\Microsoft.AspNet.OData.Shared\Formatter\Serialization\ODataResourceSerializer.cs:line 1282
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteDynamicTypeResource(Object graph, ODataWriter writer, IEdmTypeReference expectedType, ODataSerializerContext writeContext) in ...\Microsoft.AspNet.OData.Shared\Formatter\Serialization\ODataResourceSerializer.cs:line 1302
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteResource(Object graph, ODataWriter writer, ODataSerializerContext writeContext, IEdmTypeReference expectedType) in ...\Microsoft.AspNet.OData.Shared\Formatter\Serialization\ODataResourceSerializer.cs:line 1392
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteObjectInline(Object graph, IEdmTypeReference expectedType, ODataWriter writer, ODataSerializerContext writeContext) in ...\Microsoft.AspNet.OData.Shared\Formatter\Serialization\ODataResourceSerializer.cs:line 104
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSetSerializer.WriteResourceSet(IEnumerable enumerable, IEdmTypeReference resourceSetType, ODataWriter writer, ODataSerializerContext writeContext) in ...\Microsoft.AspNet.OData.Shared\Formatter\Serialization\ODataResourceSetSerializer.cs:line 192
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSetSerializer.WriteObjectInline(Object graph, IEdmTypeReference expectedType, ODataWriter writer, ODataSerializerContext writeContext) in ...\Microsoft.AspNet.OData.Shared\Formatter\Serialization\ODataResourceSetSerializer.cs:line 117
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSetSerializer.WriteObject(Object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext) in ...\Microsoft.AspNet.OData.Shared\Formatter\Serialization\ODataResourceSetSerializer.cs:line 62
   at Microsoft.AspNet.OData.Formatter.ODataOutputFormatterHelper.WriteToStream(Type type, Object value, IEdmModel model, ODataVersion version, Uri baseAddress, MediaTypeHeaderValue contentType, IWebApiUrlHelper internaUrlHelper, IWebApiRequestMessage internalRequest, IWebApiHeaders internalRequestHeaders, Func`2 getODataMessageWrapper, Func`2 getEdmTypeSerializer, Func`2 getODataPayloadSerializer, Func`1 getODataSerializerContext) in ...\Microsoft.AspNet.OData.Shared\Formatter\ODataOutputFormatterHelper.cs:line 230
   at Microsoft.AspNet.OData.Formatter.ODataMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) in ...\Microsoft.AspNet.OData\Formatter\ODataMediaTypeFormatter.cs:line 329

Additional detail

The above mentioned pull request added conversion only when a edmProperty is found, and when using aggregate transformation seems like that is not the case:

if (edmProperty != null)
{
property = new ODataProperty
{
Name = prop.Key,
Value = ODataPrimitiveSerializer.ConvertPrimitiveValue(prop.Value, edmProperty.Type.AsPrimitive())
};
}
else
{
property = new ODataProperty
{
Name = prop.Key,
Value = prop.Value
};
}

Maybe it shouldn't matter if there is an edmProperty and always do the conversion or the aliased property should take the same type as the property which is being aggregated?
Based on how the edmProperty inside that method is retrived from the entityType.Properties I found a workaround where the alias has the same name as the property that is being aggregated on, which works, but maybe it shouldn't :D
/SomeEntitySet?$apply=aggregate(SomeDateTimeProperty with min as SomeDateTimeProperty)

Thanks!

@habbes habbes added the bug label Apr 25, 2023
@habbes habbes self-assigned this Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants