-
Notifications
You must be signed in to change notification settings - Fork 359
Added CollectionGenericType to ClientPropertyAnnotation #3170
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
base: main
Are you sure you want to change the base?
Conversation
Modified EntryValueMaterializationPolicy to ensure the correct collection element type is used if a POCO does not have a collection initializer for collection properties.
|
@lawynn Could you please add tests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Overview
This pull request fixes an issue with POCO collection property materialization by introducing a new property, CollectionGenericType, to properly identify the element type.
- Adds the CollectionGenericType property to ClientPropertyAnnotation.
- Replaces EntityCollectionItemType with CollectionGenericType in EntryValueMaterializationPolicy when creating collection instances.
Reviewed Changes
| File | Description |
|---|---|
| src/Microsoft.OData.Client/Metadata/ClientPropertyAnnotation.cs | Introduces CollectionGenericType to expose the generic collection type. |
| src/Microsoft.OData.Client/Materialization/EntryValueMaterializationPolicy.cs | Updates the creation of List<> and Collection<> instances to use CollectionGenericType. |
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
Comments suppressed due to low confidence (3)
src/Microsoft.OData.Client/Metadata/ClientPropertyAnnotation.cs:264
- [nitpick] Consider adding XML documentation for the new CollectionGenericType property to clarify its intended usage and assumptions regarding its initialization.
internal Type CollectionGenericType
src/Microsoft.OData.Client/Materialization/EntryValueMaterializationPolicy.cs:488
- Ensure that property.CollectionGenericType is properly initialized and non-null before its use in MakeGenericType to avoid potential runtime exceptions.
Type listCollectionType = typeof(List<>).MakeGenericType(property.CollectionGenericType);
src/Microsoft.OData.Client/Materialization/EntryValueMaterializationPolicy.cs:501
- Ensure that property.CollectionGenericType is non-null before using it to construct the generic Collection<> type; consider adding a null check or an assertion.
collectionType = typeof(System.Collections.ObjectModel.Collection<>).MakeGenericType(property.CollectionGenericType);
|
@lawynn Thanks for taking some time to contribute on this. Please add some tests. |
thank you, unfortunately I don't have a lot of time left to work on this at the moment. It looks as though there may be some test coverage already. I'll see if I can carve out some time in the next few weeks to get back to this issue. |
|
Any progress on this? Can we help in any way? We are seeing the exact same problem when updating to later OData Client versions. |
Apologies, I got swamped and have not had time to get back to this issue. This PR corrected the issue for my particular case, but I see similar patterns of code elsewhere. Depending on which logic path is taken, it may incorrectly use the EntityCollectionItemType (EntityType), rather than the CollectionGenericType (Collection). I only addressed the issue that I was seeing, @leoerlandsson, were you able to build this branch, and did it resolve your issue? If not, then your EDM setup may be different than mine, and you may be running into some of the other logic branches. |
Modified EntryValueMaterializationPolicy to ensure the correct collection element type is used if a POCO does not have a collection initializer for collection properties.
Issues
This pull request fixes #3124.
Description
In cases where POCO classes are used, and contain collection properties, the materializer does not correctly identify the element type. The property.EntityCollectionItemType erroneously returns a null for complex types, which then fails on the MakeGenericType call.
This fix surfaces the CollectionGenericType, and uses that in the 2 logic branches that will fail if the value is null.
Checklist (Uncheck if it is not completed)
Additional work necessary
If documentation update is needed, please add "Docs Needed" label to the issue and provide details about the required document change in the issue.