Skip to content

Commit e32fc81

Browse files
Switch ad hoc models to be complex types instead of entities; skips validation
1 parent 518fec0 commit e32fc81

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

src/AspNetCore/OData/src/Asp.Versioning.OData.ApiExplorer/Conventions/ImplicitModelBoundSettingsConvention.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
namespace Asp.Versioning.Conventions;
44

5-
using Asp.Versioning;
6-
using Asp.Versioning.OData;
75
using Microsoft.AspNetCore.Mvc.ApiExplorer;
8-
using Microsoft.OData.ModelBuilder;
96

107
/// <content>
118
/// Provides additional implementation specific to ASP.NET Core.

src/Common/src/Common.OData.ApiExplorer/Conventions/ImplicitModelBoundSettingsConvention.cs

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Asp.Versioning.Conventions;
88
using Microsoft.AspNet.OData.Builder;
99
#else
1010
using Microsoft.OData.ModelBuilder;
11+
using System.Buffers;
1112
#endif
1213

1314
/// <summary>
@@ -56,28 +57,66 @@ public void Apply( ODataModelBuilder builder, ApiVersion apiVersion, string? rou
5657

5758
private static HashSet<Type>? GetExistingTypes( ODataModelBuilder builder )
5859
{
59-
var types = default( HashSet<Type> );
60+
HashSet<Type> types;
6061

61-
foreach ( var entitySet in builder.EntitySets )
62+
if ( builder.StructuralTypes is ICollection<StructuralTypeConfiguration> collection )
6263
{
63-
types ??= new();
64-
types.Add( entitySet.ClrType );
64+
var count = collection.Count;
65+
66+
if ( count == 0 )
67+
{
68+
return default;
69+
}
70+
71+
#if NETFRAMEWORK
72+
var array = new StructuralTypeConfiguration[count];
73+
types = new();
74+
#else
75+
var pool = ArrayPool<StructuralTypeConfiguration>.Shared;
76+
var array = pool.Rent( count );
77+
78+
types = new( capacity: count );
79+
#endif
80+
81+
collection.CopyTo( array, 0 );
82+
83+
for ( var i = 0; i < count; i++ )
84+
{
85+
types.Add( array[i].ClrType );
86+
}
87+
88+
#if !NETFRAMEWORK
89+
pool.Return( array, clearArray: true );
90+
#endif
91+
92+
return types;
93+
}
94+
95+
using var structuralTypes = builder.StructuralTypes.GetEnumerator();
96+
97+
if ( !structuralTypes.MoveNext() )
98+
{
99+
return default;
65100
}
66101

67-
foreach ( var singleton in builder.Singletons )
102+
types = new HashSet<Type>()
103+
{
104+
structuralTypes.Current.ClrType,
105+
};
106+
107+
while ( structuralTypes.MoveNext() )
68108
{
69-
types ??= new();
70-
types.Add( singleton.ClrType );
109+
types.Add( structuralTypes.Current.ClrType );
71110
}
72111

73112
return types;
74113
}
75114

76115
private void OnModelCreating( ODataModelBuilder builder )
77116
{
78-
foreach ( var entityType in types.Select( builder.AddEntityType ) )
117+
foreach ( var type in types )
79118
{
80-
builder.AddEntitySet( entityType.Name, entityType );
119+
builder.AddComplexType( type );
81120
}
82121
}
83122
}

0 commit comments

Comments
 (0)