Skip to content

Commit 7bd1b81

Browse files
committed
nullable annotations for the runtime public API
1 parent 653679a commit 7bd1b81

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

src/AutoMapper/Configuration/MapperConfigurationExpression.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ private void AddMapsCore(IEnumerable<Assembly> assembliesToScan)
184184
{
185185
foreach (var memberConfigurationProvider in memberInfo.GetCustomAttributes().OfType<IMemberConfigurationProvider>())
186186
{
187-
mappingExpression.ForMember(memberInfo, cfg => memberConfigurationProvider.ApplyConfiguration(cfg));
187+
mappingExpression.ForMember(memberInfo, memberConfigurationProvider.ApplyConfiguration);
188188
}
189189
}
190190

@@ -195,5 +195,5 @@ private void AddMapsCore(IEnumerable<Assembly> assembliesToScan)
195195
AddProfile(autoMapAttributeProfile);
196196
}
197197

198-
public void ConstructServicesUsing(Func<Type, object> constructor) => _serviceCtor = constructor;
198+
public void ConstructServicesUsing(Func<Type, object> constructor) => _serviceCtor = constructor ?? throw new ArgumentNullException(nameof(constructor));
199199
}

src/AutoMapper/Mapper.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace AutoMapper;
2-
32
using IObjectMappingOperationOptions = IMappingOperationOptions<object, object>;
43
using Factory = Func<Type, object>;
4+
#nullable enable
55
public interface IMapperBase
66
{
77
/// <summary>
@@ -11,15 +11,15 @@ public interface IMapperBase
1111
/// <typeparam name="TDestination">Destination type to create</typeparam>
1212
/// <param name="source">Source object to map from</param>
1313
/// <returns>Mapped destination object</returns>
14-
TDestination Map<TDestination>(object source);
14+
TDestination? Map<TDestination>(object? source);
1515
/// <summary>
1616
/// Execute a mapping from the source object to a new destination object.
1717
/// </summary>
1818
/// <typeparam name="TSource">Source type to use, regardless of the runtime type</typeparam>
1919
/// <typeparam name="TDestination">Destination type to create</typeparam>
2020
/// <param name="source">Source object to map from</param>
2121
/// <returns>Mapped destination object</returns>
22-
TDestination Map<TSource, TDestination>(TSource source);
22+
TDestination? Map<TSource, TDestination>(TSource? source);
2323
/// <summary>
2424
/// Execute a mapping from the source object to the existing destination object.
2525
/// </summary>
@@ -28,15 +28,15 @@ public interface IMapperBase
2828
/// <param name="source">Source object to map from</param>
2929
/// <param name="destination">Destination object to map into</param>
3030
/// <returns>The mapped destination object, same instance as the <paramref name="destination"/> object</returns>
31-
TDestination Map<TSource, TDestination>(TSource source, TDestination destination);
31+
TDestination? Map<TSource, TDestination>(TSource? source, TDestination? destination);
3232
/// <summary>
3333
/// Execute a mapping from the source object to a new destination object with explicit <see cref="System.Type"/> objects
3434
/// </summary>
3535
/// <param name="source">Source object to map from</param>
3636
/// <param name="sourceType">Source type to use</param>
3737
/// <param name="destinationType">Destination type to create</param>
3838
/// <returns>Mapped destination object</returns>
39-
object Map(object source, Type sourceType, Type destinationType);
39+
object? Map(object? source, Type? sourceType, Type destinationType);
4040
/// <summary>
4141
/// Execute a mapping from the source object to existing destination object with explicit <see cref="System.Type"/> objects
4242
/// </summary>
@@ -45,7 +45,7 @@ public interface IMapperBase
4545
/// <param name="sourceType">Source type to use</param>
4646
/// <param name="destinationType">Destination type to use</param>
4747
/// <returns>Mapped destination object, same instance as the <paramref name="destination"/> object</returns>
48-
object Map(object source, object destination, Type sourceType, Type destinationType);
48+
object? Map(object? source, object? destination, Type? sourceType, Type? destinationType);
4949
}
5050
public interface IMapper : IMapperBase
5151
{
@@ -56,7 +56,7 @@ public interface IMapper : IMapperBase
5656
/// <param name="source">Source object to map from</param>
5757
/// <param name="opts">Mapping options</param>
5858
/// <returns>Mapped destination object</returns>
59-
TDestination Map<TDestination>(object source, Action<IMappingOperationOptions<object, TDestination>> opts);
59+
TDestination? Map<TDestination>(object? source, Action<IMappingOperationOptions<object, TDestination>> opts);
6060
/// <summary>
6161
/// Execute a mapping from the source object to a new destination object with supplied mapping options.
6262
/// </summary>
@@ -65,7 +65,7 @@ public interface IMapper : IMapperBase
6565
/// <param name="source">Source object to map from</param>
6666
/// <param name="opts">Mapping options</param>
6767
/// <returns>Mapped destination object</returns>
68-
TDestination Map<TSource, TDestination>(TSource source, Action<IMappingOperationOptions<TSource, TDestination>> opts);
68+
TDestination? Map<TSource, TDestination>(TSource? source, Action<IMappingOperationOptions<TSource, TDestination>> opts);
6969
/// <summary>
7070
/// Execute a mapping from the source object to the existing destination object with supplied mapping options.
7171
/// </summary>
@@ -75,7 +75,7 @@ public interface IMapper : IMapperBase
7575
/// <param name="destination">Destination object to map into</param>
7676
/// <param name="opts">Mapping options</param>
7777
/// <returns>The mapped destination object, same instance as the <paramref name="destination"/> object</returns>
78-
TDestination Map<TSource, TDestination>(TSource source, TDestination destination, Action<IMappingOperationOptions<TSource, TDestination>> opts);
78+
TDestination? Map<TSource, TDestination>(TSource? source, TDestination? destination, Action<IMappingOperationOptions<TSource, TDestination>> opts);
7979
/// <summary>
8080
/// Execute a mapping from the source object to a new destination object with explicit <see cref="System.Type"/> objects and supplied mapping options.
8181
/// </summary>
@@ -84,7 +84,7 @@ public interface IMapper : IMapperBase
8484
/// <param name="destinationType">Destination type to create</param>
8585
/// <param name="opts">Mapping options</param>
8686
/// <returns>Mapped destination object</returns>
87-
object Map(object source, Type sourceType, Type destinationType, Action<IObjectMappingOperationOptions> opts);
87+
object? Map(object? source, Type? sourceType, Type destinationType, Action<IObjectMappingOperationOptions> opts);
8888
/// <summary>
8989
/// Execute a mapping from the source object to existing destination object with supplied mapping options and explicit <see cref="System.Type"/> objects
9090
/// </summary>
@@ -94,7 +94,7 @@ public interface IMapper : IMapperBase
9494
/// <param name="destinationType">Destination type to use</param>
9595
/// <param name="opts">Mapping options</param>
9696
/// <returns>Mapped destination object, same instance as the <paramref name="destination"/> object</returns>
97-
object Map(object source, object destination, Type sourceType, Type destinationType, Action<IObjectMappingOperationOptions> opts);
97+
object? Map(object? source, object? destination, Type? sourceType, Type? destinationType, Action<IObjectMappingOperationOptions> opts);
9898
/// <summary>
9999
/// Configuration provider for performing maps
100100
/// </summary>
@@ -108,7 +108,7 @@ public interface IMapper : IMapperBase
108108
/// <param name="parameters">Optional parameter object for parameterized mapping expressions</param>
109109
/// <param name="membersToExpand">Explicit members to expand</param>
110110
/// <returns>Queryable result, use queryable extension methods to project and execute result</returns>
111-
IQueryable<TDestination> ProjectTo<TDestination>(IQueryable source, object parameters = null, params Expression<Func<TDestination, object>>[] membersToExpand);
111+
IQueryable<TDestination> ProjectTo<TDestination>(IQueryable source, object? parameters = null, params Expression<Func<TDestination, object>>[] membersToExpand);
112112
/// <summary>
113113
/// Project the input queryable.
114114
/// </summary>
@@ -117,7 +117,7 @@ public interface IMapper : IMapperBase
117117
/// <param name="parameters">Optional parameter object for parameterized mapping expressions</param>
118118
/// <param name="membersToExpand">Explicit members to expand</param>
119119
/// <returns>Queryable result, use queryable extension methods to project and execute result</returns>
120-
IQueryable<TDestination> ProjectTo<TDestination>(IQueryable source, IDictionary<string, object> parameters, params string[] membersToExpand);
120+
IQueryable<TDestination> ProjectTo<TDestination>(IQueryable source, IDictionary<string, object>? parameters, params string[] membersToExpand);
121121
/// <summary>
122122
/// Project the input queryable.
123123
/// </summary>
@@ -126,11 +126,12 @@ public interface IMapper : IMapperBase
126126
/// <param name="parameters">Optional parameter object for parameterized mapping expressions</param>
127127
/// <param name="membersToExpand">Explicit members to expand</param>
128128
/// <returns>Queryable result, use queryable extension methods to project and execute result</returns>
129-
IQueryable ProjectTo(IQueryable source, Type destinationType, IDictionary<string, object> parameters = null, params string[] membersToExpand);
129+
IQueryable ProjectTo(IQueryable source, Type destinationType, IDictionary<string, object>? parameters = null, params string[] membersToExpand);
130130
}
131131
public interface IRuntimeMapper : IMapperBase
132132
{
133133
}
134+
#nullable disable
134135
internal interface IInternalRuntimeMapper : IRuntimeMapper
135136
{
136137
TDestination Map<TSource, TDestination>(TSource source, TDestination destination, ResolutionContext context, Type sourceType = null, Type destinationType = null, MemberMap memberMap = null);

src/AutoMapper/QueryableExtensions/Extensions.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
namespace AutoMapper.QueryableExtensions;
2-
32
using MemberPaths = IEnumerable<MemberInfo[]>;
43
using ParameterBag = IDictionary<string, object>;
54
/// <summary>
@@ -20,7 +19,8 @@ static IQueryable Select(IQueryable source, LambdaExpression lambda) => source.P
2019
/// <param name="parameters">Optional parameter object for parameterized mapping expressions</param>
2120
/// <param name="membersToExpand">Explicit members to expand</param>
2221
/// <returns>Expression to project into</returns>
23-
public static IQueryable<TDestination> ProjectTo<TDestination>(this IQueryable source, IConfigurationProvider configuration, object parameters, params Expression<Func<TDestination, object>>[] membersToExpand) =>
22+
#nullable enable
23+
public static IQueryable<TDestination> ProjectTo<TDestination>(this IQueryable source, IConfigurationProvider configuration, object? parameters, params Expression<Func<TDestination, object>>[] membersToExpand) =>
2424
source.ToCore<TDestination>(configuration, parameters, membersToExpand.Select(MemberVisitor.GetMemberPath));
2525
/// <summary>
2626
/// Extension method to project from a queryable using the provided mapping engine
@@ -43,7 +43,7 @@ public static IQueryable<TDestination> ProjectTo<TDestination>(this IQueryable s
4343
/// <param name="parameters">Optional parameter object for parameterized mapping expressions</param>
4444
/// <param name="membersToExpand">Explicit members to expand</param>
4545
/// <returns>Queryable result, use queryable extension methods to project and execute result</returns>
46-
public static IQueryable<TDestination> ProjectTo<TDestination>(this IQueryable source, IConfigurationProvider configuration, ParameterBag parameters, params string[] membersToExpand) =>
46+
public static IQueryable<TDestination> ProjectTo<TDestination>(this IQueryable source, IConfigurationProvider configuration, ParameterBag? parameters, params string[] membersToExpand) =>
4747
source.ToCore<TDestination>(configuration, parameters, membersToExpand.Select(memberName => ReflectionHelper.GetMemberPath(typeof(TDestination), memberName)));
4848
/// <summary>
4949
/// Extension method to project from a queryable using the provided mapping engine
@@ -64,8 +64,9 @@ public static IQueryable ProjectTo(this IQueryable source, Type destinationType,
6464
/// <param name="parameters">Optional parameter object for parameterized mapping expressions</param>
6565
/// <param name="membersToExpand">Explicit members to expand</param>
6666
/// <returns>Queryable result, use queryable extension methods to project and execute result</returns>
67-
public static IQueryable ProjectTo(this IQueryable source, Type destinationType, IConfigurationProvider configuration, ParameterBag parameters, params string[] membersToExpand) =>
67+
public static IQueryable ProjectTo(this IQueryable source, Type destinationType, IConfigurationProvider configuration, ParameterBag? parameters, params string[] membersToExpand) =>
6868
source.ToCore(destinationType, configuration, parameters, membersToExpand.Select(memberName => ReflectionHelper.GetMemberPath(destinationType, memberName)));
69+
#nullable disable
6970
static IQueryable<TResult> ToCore<TResult>(this IQueryable source, IConfigurationProvider configuration, object parameters, MemberPaths memberPathsToExpand) =>
7071
(IQueryable<TResult>)source.ToCore(typeof(TResult), configuration, parameters, memberPathsToExpand);
7172
static IQueryable ToCore(this IQueryable source, Type destinationType, IConfigurationProvider configuration, object parameters, MemberPaths memberPathsToExpand) =>

0 commit comments

Comments
 (0)