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

Added CollectionGenericType to ClientPropertyAnnotation #3170

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lawynn
Copy link
Contributor

@lawynn lawynn commented Jan 23, 2025

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)

  • Test cases added
  • Build and test with one-click build and test script passed

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.

Modified EntryValueMaterializationPolicy to ensure the correct collection element type is used if a POCO does not have a collection initializer for collection properties.
@WanjohiSammy
Copy link
Member

@lawynn Could you please add tests

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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OData.Client 8.2.1 does not materialize data service responses
2 participants