OData / odata.net

ODataLib: Open Data Protocol - .NET Libraries and Frameworks
https://docs.microsoft.com/odata
Other
686 stars 349 forks source link

ODataTypeInfo.cs not correctly identifying entity keys by "Id" #3052

Open lawynn opened 2 weeks ago

lawynn commented 2 weeks ago

ODataTypeInfo.cs is identifying conventional Id properties using case sensitive checking on "ID" which is failing for POCO classes that use Id.

Assemblies affected

Microsoft.OData.Client

Reproduce steps

Create an OData Entity with a POCO that doesn't have [Key] attributes, but uses an "Id" property (Casing matters, ID will work, id, or Id, or god forbid iD will fail).

At runtime do a standard query against a DataServiceContext that is bound to the service.

Note that the entity is not tracked in the context.

Any attempt to track the entity will result in an exception indicating that the entity type is invalid as it does not define a key.

Defining an Edm manually, and ensuring the .HasKey() is called does not resolve the issue as it seems that the Edm is not used in this check.

Expected result

The entity would be tracked conventionally using the Id as the key as there are no other keys defined, and the entity is a valid entity type in the edm.

Actual result

An exception is thrown indicating that the entity is not a valid entity type.

Additional detail

The proposed fix is to change the check the property names using case insensitive comparison. I think a better fix would be to use the provided EDM to determine the nature of the EntityTypes as they are correct in the ClientEdmModel when configured using the fluent apis.

https://github.com/OData/odata.net/pull/3050

xuzhg commented 1 week ago

FYI: OData.ModelBuilder uses the case insensitive: https://github.com/OData/ModelBuilder/blob/main/src/Microsoft.OData.ModelBuilder/Conventions/EntityKeyConvention.cs#L51