Skip to content

Commit

Permalink
Fix to #30115 - IndexOutOfRangeException in CreateNavigationExpansion…
Browse files Browse the repository at this point in the history
…Expression(Expression sourceExpression, IEntityType entityType) / get_Chars(Int32 index) when EF class named "<>f__AnonymousType01Child" in 7.0.2 version

Adjusting ShortName to trim "<>" from the start of the type name.

Fixes #30115
  • Loading branch information
maumar committed Mar 28, 2023
1 parent f50cc97 commit 0333eab
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/EFCore/Metadata/IReadOnlyTypeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ string ShortName()
if (!HasSharedClrType)
{
var name = ClrType.ShortDisplayName();
if (name.StartsWith("<>", StringComparison.Ordinal))
{
name = name[2..];
}

var lessIndex = name.IndexOf("<", StringComparison.Ordinal);
if (lessIndex == -1)
{
Expand Down
51 changes: 51 additions & 0 deletions test/EFCore.Tests/Metadata/Internal/EntityTypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.ComponentModel;
using System.Globalization;
using System.Reflection.Emit;

// ReSharper disable CollectionNeverUpdated.Local
// ReSharper disable UnusedAutoPropertyAccessor.Local
Expand Down Expand Up @@ -2959,6 +2960,56 @@ public void Collections_dont_have_relationship_indexes_when_full_notifications_w
Assert.Equal(3, entityType.RelationshipPropertyCount());
}

[ConditionalFact]
public void ShortName_on_compiler_generated_type1()
{
var model = CreateModel();

var typeName = "<>f__AnonymousType01Child";
model.AddEntityType(typeName);
var entityType = model.FinalizeModel().FindEntityType(typeName);

Assert.Equal(typeName, entityType.ShortName());
}

[ConditionalFact]
public void ShortName_on_compiler_generated_type2()
{
var model = CreateModel();

var typeName = "<>f__AnonymousType01Child";
var assemblyName = new AssemblyName("DynamicEntityClrTypeAssembly");
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
var moduleBuilder = assemblyBuilder.DefineDynamicModule("MyModule");
var typeBuilder = moduleBuilder.DefineType(typeName);
var type = typeBuilder.CreateType();

model.AddEntityType(type);

var entityType = model.FinalizeModel().FindEntityType(typeName);

Assert.Equal(typeName[2..], entityType.ShortName());
}

[ConditionalFact]
public void ShortName_on_compiler_generated_type3()
{
var model = CreateModel();

var typeName = "<>__AnonymousType01Child<int>";
var assemblyName = new AssemblyName("DynamicEntityClrTypeAssembly");
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
var moduleBuilder = assemblyBuilder.DefineDynamicModule("MyModule");
var typeBuilder = moduleBuilder.DefineType(typeName);
var type = typeBuilder.CreateType();

model.AddEntityType(type);

var entityType = model.FinalizeModel().FindEntityType(typeName);

Assert.Equal("__AnonymousType01Child", entityType.ShortName());
}

private readonly IMutableModel _model = BuildModel();

private IMutableEntityType DependentType
Expand Down

0 comments on commit 0333eab

Please sign in to comment.