Skip to content

Commit 639b958

Browse files
Chris Martinezcommonsensesoftware
authored andcommitted
Define option to opt out of ad hoc model type substitution
1 parent 0b9a299 commit 639b958

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/Common/src/Common.OData.ApiExplorer/OData/DefaultModelTypeBuilder.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ namespace Asp.Versioning.OData;
2424
/// </summary>
2525
public sealed class DefaultModelTypeBuilder : IModelTypeBuilder
2626
{
27+
/* design: there is typically a 1:1 relationship between an edm and api version. odata model bound settings
28+
* are realized as an annotation in the edm. this can result in two sets of pairs where one edm is the
29+
* standard mapping and the other is ad hoc for the purposes of query option settings. aside for the bucket
30+
* they land in, there is no difference in how types will be mapped; however if the wrong edm from the
31+
* incorrect bucket is picked, then the type mapping will fail. the model type builder detects if a model
32+
* is ad hoc. if it is, then it will recursively create a private instance of itself to handle the ad hoc
33+
* bucket. normal odata cannot opt out of this process because the explored type must match the edm. a type
34+
* mapped via an ad hoc edm is not really odata so it can opt out if desired. the opt out process is more
35+
* of a failsafe and optimization. if the ad hoc edm wasn't customized, then the meta model and type should
36+
* be exactly the same, which will result in no substitution.
37+
*/
38+
2739
private static Type? ienumerableOfT;
2840
private readonly bool adHoc;
2941
private DefaultModelTypeBuilder? adHocBuilder;
@@ -38,6 +50,14 @@ public sealed class DefaultModelTypeBuilder : IModelTypeBuilder
3850
/// </summary>
3951
public DefaultModelTypeBuilder() { }
4052

53+
/// <summary>
54+
/// Gets or sets a value indicating whether types from an ad hoc Entity Data Model
55+
/// (EDM) should be excluded.
56+
/// </summary>
57+
/// <value>True if types from an ad hoc EDM are excluded; otherwise, false. The
58+
/// default value is <c>false</c>.</value>
59+
public bool ExcludeAdHocModels { get; set; }
60+
4161
/// <inheritdoc />
4262
public Type NewStructuredType( IEdmModel model, IEdmStructuredType structuredType, Type clrType, ApiVersion apiVersion )
4363
{
@@ -46,10 +66,17 @@ public Type NewStructuredType( IEdmModel model, IEdmStructuredType structuredTyp
4666
throw new ArgumentNullException( nameof( model ) );
4767
}
4868

49-
if ( !adHoc && model.IsAdHoc() )
69+
if ( model.IsAdHoc() )
5070
{
51-
adHocBuilder ??= new( adHoc: true );
52-
return adHocBuilder.NewStructuredType( model, structuredType, clrType, apiVersion );
71+
if ( ExcludeAdHocModels )
72+
{
73+
return clrType;
74+
}
75+
else if ( !adHoc )
76+
{
77+
adHocBuilder ??= new( adHoc: true );
78+
return adHocBuilder.NewStructuredType( model, structuredType, clrType, apiVersion );
79+
}
5380
}
5481

5582
if ( structuredType == null )

0 commit comments

Comments
 (0)