Skip to content

Fixes Issue 996 #997

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

Merged
merged 2 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<VersionPrefix>7.0.2</VersionPrefix>
<VersionPrefix>7.0.3</VersionPrefix>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<TargetFrameworks>net45;net472</TargetFrameworks>
<RootNamespace>Asp.Versioning</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Fix: `EnableQueryAttribute` should override _Model Bound_ settings (Related to [#928](https://github.com/dotnet/aspnet-api-versioning/issues/928))
Fix: Explore model per EDM + API version ([#996](https://github.com/dotnet/aspnet-api-versioning/issues/996))
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<VersionPrefix>7.0.2</VersionPrefix>
<VersionPrefix>7.0.1</VersionPrefix>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>Asp.Versioning</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Fix: `EnableQueryAttribute` should override _Model Bound_ settings (Related to [#928](https://github.com/dotnet/aspnet-api-versioning/issues/928))
Fix: Explore model per EDM + API version ([#996](https://github.com/dotnet/aspnet-api-versioning/issues/996))
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal sealed class ClassSignature : IEquatable<ClassSignature>
private static readonly ConstructorInfo newOriginalType = typeof( OriginalTypeAttribute ).GetConstructors()[0];
private int? hashCode;

internal ClassSignature( Type originalType, IEnumerable<ClassProperty> properties, ApiVersion apiVersion )
internal ClassSignature( string name, Type originalType, IEnumerable<ClassProperty> properties, ApiVersion apiVersion )
{
var attributeBuilders = new List<CustomAttributeBuilder>()
{
Expand All @@ -21,7 +21,7 @@ internal ClassSignature( Type originalType, IEnumerable<ClassProperty> propertie

attributeBuilders.AddRange( originalType.DeclaredAttributes() );

Name = originalType.FullName!;
Name = name;
Attributes = attributeBuilders.ToArray();
Properties = properties.ToArray();
ApiVersion = apiVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public sealed class DefaultModelTypeBuilder : IModelTypeBuilder
private readonly bool adHoc;
private readonly bool excludeAdHocModels;
private DefaultModelTypeBuilder? adHocBuilder;
private ConcurrentDictionary<ApiVersion, ModuleBuilder>? modules;
private ConcurrentDictionary<ApiVersion, IDictionary<EdmTypeKey, Type>>? generatedEdmTypesPerVersion;
private ConcurrentDictionary<ApiVersion, ConcurrentDictionary<EdmTypeKey, Type>>? generatedActionParamsPerVersion;
private ConcurrentDictionary<EdmModelKey, ModuleBuilder>? modules;
private ConcurrentDictionary<EdmModelKey, IDictionary<EdmTypeKey, Type>>? generatedEdmTypesPerVersion;
private ConcurrentDictionary<EdmModelKey, ConcurrentDictionary<EdmTypeKey, Type>>? generatedActionParamsPerVersion;

private DefaultModelTypeBuilder( bool excludeAdHocModels, bool adHoc )
{
Expand Down Expand Up @@ -96,7 +96,7 @@ public Type NewStructuredType( IEdmModel model, IEdmStructuredType structuredTyp

generatedEdmTypesPerVersion ??= new();

var edmTypes = generatedEdmTypesPerVersion.GetOrAdd( apiVersion, key => GenerateTypesForEdmModel( model, key ) );
var edmTypes = generatedEdmTypesPerVersion.GetOrAdd( new( model, apiVersion ), key => GenerateTypesForEdmModel( model, key.ApiVersion ) );

return edmTypes[new( structuredType, apiVersion )];
}
Expand Down Expand Up @@ -132,15 +132,15 @@ public Type NewActionParameters( IEdmModel model, IEdmAction action, string cont

generatedActionParamsPerVersion ??= new();

var paramTypes = generatedActionParamsPerVersion.GetOrAdd( apiVersion, _ => new() );
var paramTypes = generatedActionParamsPerVersion.GetOrAdd( new( model, apiVersion ), _ => new() );
var fullTypeName = $"{controllerName}.{action.Namespace}.{controllerName}{action.Name}Parameters";
var key = new EdmTypeKey( fullTypeName, apiVersion );
var type = paramTypes.GetOrAdd( key, _ =>
{
var context = new TypeSubstitutionContext( model, this, apiVersion );
var properties = action.Parameters.Where( p => p.Name != "bindingParameter" ).Select( p => new ClassProperty( p, context ) );
var signature = new ClassSignature( fullTypeName, properties, apiVersion );
var moduleBuilder = ( modules ??= new() ).GetOrAdd( apiVersion, CreateModuleForApiVersion );
var moduleBuilder = ( modules ??= new() ).GetOrAdd( new( model, apiVersion ), CreateModuleForApiVersion );

return CreateTypeFromSignature( moduleBuilder, signature );
} );
Expand All @@ -150,7 +150,7 @@ public Type NewActionParameters( IEdmModel model, IEdmAction action, string cont

private IDictionary<EdmTypeKey, Type> GenerateTypesForEdmModel( IEdmModel model, ApiVersion apiVersion )
{
ModuleBuilder NewModuleBuilder() => ( modules ??= new() ).GetOrAdd( apiVersion, CreateModuleForApiVersion );
ModuleBuilder NewModuleBuilder() => ( modules ??= new() ).GetOrAdd( new( model, apiVersion ), CreateModuleForApiVersion );

var context = new BuilderContext( model, apiVersion, NewModuleBuilder );

Expand Down Expand Up @@ -336,7 +336,7 @@ private static Type ResolveType(
return type;
}

var signature = new ClassSignature( clrType, properties, apiVersion );
var signature = new ClassSignature( typeKey.FullName, clrType, properties, apiVersion );

if ( hasUnfinishedTypes )
{
Expand Down Expand Up @@ -518,9 +518,9 @@ private static AssemblyName NewAssemblyName( ApiVersion apiVersion, bool adHoc )
}

[MethodImpl( MethodImplOptions.AggressiveInlining )]
private ModuleBuilder CreateModuleForApiVersion( ApiVersion apiVersion )
private ModuleBuilder CreateModuleForApiVersion( EdmModelKey key )
{
var assemblyName = NewAssemblyName( apiVersion, adHoc );
var assemblyName = NewAssemblyName( key.ApiVersion, adHoc );
#if NETFRAMEWORK
var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly( assemblyName, Run );
#else
Expand Down
26 changes: 26 additions & 0 deletions src/Common/src/Common.OData.ApiExplorer/OData/EdmModelKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.

namespace Asp.Versioning.OData;

using Microsoft.OData.Edm;

internal readonly struct EdmModelKey : IEquatable<EdmModelKey>
{
private readonly int hashCode;

public readonly IEdmModel EdmModel;
public readonly ApiVersion ApiVersion;

internal EdmModelKey( IEdmModel model, ApiVersion apiVersion ) =>
hashCode = HashCode.Combine( ( EdmModel = model ).GetHashCode(), ApiVersion = apiVersion );

public static bool operator ==( EdmModelKey obj, EdmModelKey other ) => obj.Equals( other );

public static bool operator !=( EdmModelKey obj, EdmModelKey other ) => !obj.Equals( other );

public override int GetHashCode() => hashCode;

public override bool Equals( object? obj ) => obj is EdmModelKey other && Equals( other );

public bool Equals( EdmModelKey other ) => hashCode == other.hashCode;
}
9 changes: 6 additions & 3 deletions src/Common/src/Common.OData.ApiExplorer/OData/EdmTypeKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ namespace Asp.Versioning.OData;
{
private readonly int hashCode;

public readonly string FullName;
public readonly ApiVersion ApiVersion;

internal EdmTypeKey( IEdmStructuredType type, ApiVersion apiVersion ) =>
hashCode = HashCode.Combine( type.FullTypeName(), apiVersion );
hashCode = HashCode.Combine( FullName = type.FullTypeName(), ApiVersion = apiVersion );

internal EdmTypeKey( IEdmTypeReference type, ApiVersion apiVersion ) =>
hashCode = HashCode.Combine( type.FullName(), apiVersion );
hashCode = HashCode.Combine( FullName = type.FullName(), ApiVersion = apiVersion );

internal EdmTypeKey( string fullTypeName, ApiVersion apiVersion ) =>
hashCode = HashCode.Combine( fullTypeName, apiVersion );
hashCode = HashCode.Combine( FullName = fullTypeName, ApiVersion = apiVersion );

public static bool operator ==( EdmTypeKey obj, EdmTypeKey other ) => obj.Equals( other );

Expand Down