-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Don't throw and catch on validation #4526
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
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,120 +1,116 @@ | ||
| using AutoMapper.Internal.Mappers; | ||
| namespace AutoMapper.Configuration; | ||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| public readonly record struct ConfigurationValidator(IGlobalConfigurationExpression Expression) | ||
| public class ConfigurationValidator(IGlobalConfiguration config) | ||
| { | ||
| private void Validate(ValidationContext context) | ||
| { | ||
| foreach (var validator in Expression.Validators) | ||
| { | ||
| validator(context); | ||
| } | ||
| } | ||
| public void AssertConfigurationExpressionIsValid(IGlobalConfiguration config, TypeMap[] typeMaps) | ||
| IGlobalConfigurationExpression Expression => ((MapperConfiguration)config).ConfigurationExpression; | ||
| public void AssertConfigurationExpressionIsValid(TypeMap[] typeMaps) | ||
| { | ||
| var duplicateTypeMapConfigs = Expression.Profiles.Append((Profile)Expression) | ||
| .SelectMany(p => p.TypeMapConfigs, (profile, typeMap) => (profile, typeMap)) | ||
| .GroupBy(x => x.typeMap.Types) | ||
| .Where(g => g.Count() > 1) | ||
| .Select(g => (TypePair : g.Key, ProfileNames : g.Select(tmc => tmc.profile.ProfileName).ToArray())) | ||
| .Select(g => (TypePair: g.Key, ProfileNames: g.Select(tmc => tmc.profile.ProfileName).ToArray())) | ||
| .Select(g => new DuplicateTypeMapConfigurationException.TypeMapConfigErrors(g.TypePair, g.ProfileNames)) | ||
| .ToArray(); | ||
| if (duplicateTypeMapConfigs.Any()) | ||
| if(duplicateTypeMapConfigs.Length != 0) | ||
| { | ||
| throw new DuplicateTypeMapConfigurationException(duplicateTypeMapConfigs); | ||
| } | ||
| AssertConfigurationIsValid(config, typeMaps); | ||
| AssertConfigurationIsValid(typeMaps); | ||
| } | ||
| public void AssertConfigurationIsValid(IGlobalConfiguration config, TypeMap[] typeMaps) | ||
| public void AssertConfigurationIsValid(TypeMap[] typeMaps) | ||
| { | ||
| List<Exception> configExceptions = []; | ||
| var badTypeMaps = | ||
| (from typeMap in typeMaps | ||
| where typeMap.ShouldCheckForValid | ||
| let unmappedPropertyNames = typeMap.GetUnmappedPropertyNames() | ||
| let canConstruct = typeMap.PassesCtorValidation | ||
| where unmappedPropertyNames.Length > 0 || !canConstruct | ||
| select new AutoMapperConfigurationException.TypeMapConfigErrors(typeMap, unmappedPropertyNames, canConstruct) | ||
| ).ToArray(); | ||
| if (badTypeMaps.Length > 0) | ||
| where typeMap.ShouldCheckForValid | ||
| let unmappedPropertyNames = typeMap.GetUnmappedPropertyNames() | ||
| let canConstruct = typeMap.PassesCtorValidation | ||
| where unmappedPropertyNames.Length > 0 || !canConstruct | ||
| select new AutoMapperConfigurationException.TypeMapConfigErrors(typeMap, unmappedPropertyNames, canConstruct)).ToArray(); | ||
| if(badTypeMaps.Length > 0) | ||
| { | ||
| throw new AutoMapperConfigurationException(badTypeMaps); | ||
| configExceptions.Add(new AutoMapperConfigurationException(badTypeMaps)); | ||
| } | ||
| HashSet<TypeMap> typeMapsChecked = []; | ||
| List<Exception> configExceptions = []; | ||
| foreach (var typeMap in typeMaps) | ||
| foreach(var typeMap in typeMaps) | ||
| { | ||
| try | ||
| { | ||
| DryRunTypeMap(config, typeMapsChecked, typeMap.Types, typeMap, null); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| configExceptions.Add(e); | ||
| } | ||
| DryRunTypeMap(typeMap.Types, typeMap, null); | ||
| } | ||
| if (configExceptions.Count > 1) | ||
| if(configExceptions.Count > 1) | ||
| { | ||
| throw new AggregateException(configExceptions); | ||
| } | ||
| if (configExceptions.Count > 0) | ||
| if(configExceptions.Count > 0) | ||
| { | ||
| throw configExceptions[0]; | ||
| } | ||
| } | ||
| private void DryRunTypeMap(IGlobalConfiguration config, HashSet<TypeMap> typeMapsChecked, TypePair types, TypeMap typeMap, MemberMap memberMap) | ||
| { | ||
| if(typeMap == null) | ||
| void DryRunTypeMap(TypePair types, TypeMap typeMap, MemberMap memberMap) | ||
| { | ||
| if (types.ContainsGenericParameters) | ||
| if(typeMap == null) | ||
| { | ||
| return; | ||
| if(types.ContainsGenericParameters) | ||
| { | ||
| return; | ||
| } | ||
| typeMap = config.ResolveTypeMap(types.SourceType, types.DestinationType); | ||
| } | ||
| typeMap = config.ResolveTypeMap(types.SourceType, types.DestinationType); | ||
| } | ||
| if (typeMap != null) | ||
| { | ||
| if (typeMapsChecked.Contains(typeMap)) | ||
| if(typeMap != null) | ||
| { | ||
| return; | ||
| if(typeMapsChecked.Add(typeMap) && Validate(new(types, memberMap, configExceptions, typeMap)) && typeMap.ShouldCheckForValid) | ||
| { | ||
| CheckPropertyMaps(typeMap); | ||
| } | ||
| } | ||
| typeMapsChecked.Add(typeMap); | ||
| Validate(new(types, memberMap, typeMap)); | ||
| if(!typeMap.ShouldCheckForValid) | ||
| else | ||
| { | ||
| return; | ||
| var mapperToUse = config.FindMapper(types); | ||
| if(mapperToUse == null) | ||
| { | ||
| configExceptions.Add(new AutoMapperConfigurationException(memberMap.TypeMap.Types) { MemberMap = memberMap }); | ||
| return; | ||
| } | ||
| if(Validate(new(types, memberMap, configExceptions, ObjectMapper: mapperToUse)) && mapperToUse.GetAssociatedTypes(types) is TypePair newTypes && | ||
| newTypes != types) | ||
| { | ||
| DryRunTypeMap(newTypes, null, memberMap); | ||
| } | ||
| } | ||
| CheckPropertyMaps(config, typeMapsChecked, typeMap); | ||
| } | ||
| else | ||
| void CheckPropertyMaps(TypeMap typeMap) | ||
| { | ||
| var mapperToUse = config.FindMapper(types); | ||
| if (mapperToUse == null) | ||
| { | ||
| throw new AutoMapperConfigurationException(memberMap.TypeMap.Types) { MemberMap = memberMap }; | ||
| } | ||
| Validate(new(types, memberMap, ObjectMapper: mapperToUse)); | ||
| if (mapperToUse.GetAssociatedTypes(types) is TypePair newTypes && newTypes != types) | ||
| foreach(var memberMap in typeMap.MemberMaps) | ||
| { | ||
| DryRunTypeMap(config, typeMapsChecked, newTypes, null, memberMap); | ||
| if(memberMap.Ignored || (memberMap is PropertyMap && typeMap.ConstructorParameterMatches(memberMap.DestinationName))) | ||
| { | ||
| continue; | ||
| } | ||
| var sourceType = memberMap.SourceType; | ||
| // when we don't know what the source type is, bail | ||
| if(sourceType.IsGenericParameter || sourceType == typeof(object)) | ||
| { | ||
| continue; | ||
| } | ||
| DryRunTypeMap(new(sourceType, memberMap.DestinationType), null, memberMap); | ||
| } | ||
| } | ||
| } | ||
| private void CheckPropertyMaps(IGlobalConfiguration config, HashSet<TypeMap> typeMapsChecked, TypeMap typeMap) | ||
| { | ||
| foreach (var memberMap in typeMap.MemberMaps) | ||
| bool Validate(ValidationContext context) | ||
| { | ||
| if(memberMap.Ignored || (memberMap is PropertyMap && typeMap.ConstructorParameterMatches(memberMap.DestinationName))) | ||
| { | ||
| continue; | ||
| } | ||
| var sourceType = memberMap.SourceType; | ||
| // when we don't know what the source type is, bail | ||
| if (sourceType.IsGenericParameter || sourceType == typeof(object)) | ||
| foreach(var validator in Expression.Validators) | ||
| { | ||
| continue; | ||
| try | ||
| { | ||
| validator(context); | ||
| } | ||
| catch(Exception e) | ||
| { | ||
| configExceptions.Add(e); | ||
| return false; | ||
| } | ||
| } | ||
| DryRunTypeMap(config, typeMapsChecked, new(sourceType, memberMap.DestinationType), null, memberMap); | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| public readonly record struct ValidationContext(TypePair Types, MemberMap MemberMap, TypeMap TypeMap = null, IObjectMapper ObjectMapper = null); | ||
| public readonly record struct ValidationContext(TypePair Types, MemberMap MemberMap, List<Exception> Exceptions, TypeMap TypeMap = null, IObjectMapper ObjectMapper = null); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.