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

Update ODataTypeInfo.cs not correctly identifying entity keys by "Id" property #3050

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Microsoft.OData.Client/Metadata/ODataTypeInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//---------------------------------------------------------------------
//---------------------------------------------------------------------
// <copyright file="ODataTypeInfo.cs" company="Microsoft">
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
// </copyright>
Expand Down Expand Up @@ -396,10 +396,10 @@ private static KeyKind IsKeyProperty(PropertyInfo propertyInfo, KeyAttribute dat
order = newOrder;
keyKind = newKind;
}
else if (propertyName.EndsWith("ID", StringComparison.Ordinal))
else if (propertyName.Equals(propertyInfo.DeclaringType.Name + "Id", StringComparison.OrdinalIgnoreCase) || propertyName.Equals("Id", StringComparison.OrdinalIgnoreCase))
{
lawynn marked this conversation as resolved.
Show resolved Hide resolved
string declaringTypeName = propertyInfo.DeclaringType.Name;
if ((propertyName.Length == (declaringTypeName.Length + 2)) && propertyName.StartsWith(declaringTypeName, StringComparison.Ordinal))
if ((propertyName.Length == (declaringTypeName.Length + 2)) && propertyName.StartsWith(declaringTypeName, StringComparison.OrdinalIgnoreCase))
{
// matched "DeclaringType.Name+ID" pattern
keyKind = KeyKind.TypeNameId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//---------------------------------------------------------------------
//---------------------------------------------------------------------
// <copyright file="ClientTypeUtilTests.cs" company="Microsoft">
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
// </copyright>
Expand All @@ -17,6 +17,19 @@ namespace Microsoft.OData.Client.Tests.Metadata
/// </summary>
public class ClientTypeUtilTests
{
[Theory]
[InlineData(typeof(Giraffe), true)]
[InlineData(typeof(Hippo), true)]
[InlineData(typeof(Ferret), true)]
[InlineData(typeof(Lion), false)]
public void IfTypeProperty_HasConventionalKey_TypeIsEntity(Type entityType, bool isEntity)
{
//Act
bool actualResult = ClientTypeUtil.TypeOrElementTypeIsEntity(entityType);
//Assert
Assert.Equal(actualResult, isEntity);
}

[Fact]
public void IFTypeProperty_HasKeyAttribute_TypeIsEntity()
{
Expand Down Expand Up @@ -214,5 +227,37 @@ public struct EmployeeTypeStruct
public int EmpTypeId { get; set; }
}

public class Giraffe
{
/// <summary>
/// Conventional Id Key property
/// </summary>
public int Id { get; set; }
public string Name { get; set; }
}

public class Hippo
{
/// <summary>
/// Conventional {TypeName + Id} Key Property
/// </summary>
public int HippoId { get; set; }
public double Weight { get; set; }
}

public class Ferret
{
/// <summary>
/// Conventional {TypeName + Id} Key Property with odd casing
/// </summary>
public int FeRReTID { get; set; }
public double Weight { get; set; }
}

public class Lion
{
public int SomeId { get; set; }
public string Name { get; set; }
}
}
}
Loading