| | | 1 | | // Copyright DotNet API Diff Project Contributors - SPDX Identifier: MIT |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | |
| | | 4 | | namespace DotNetApiDiff.Models.Configuration; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Configuration for mapping namespaces and types between assemblies |
| | | 8 | | /// </summary> |
| | | 9 | | public class MappingConfiguration |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Dictionary mapping source namespaces to one or more target namespaces |
| | | 13 | | /// </summary> |
| | | 14 | | [JsonPropertyName("namespaceMappings")] |
| | 501 | 15 | | public Dictionary<string, List<string>> NamespaceMappings { get; set; } = new Dictionary<string, List<string>>(); |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Dictionary mapping source type names to target type names |
| | | 19 | | /// </summary> |
| | | 20 | | [JsonPropertyName("typeMappings")] |
| | 410 | 21 | | public Dictionary<string, string> TypeMappings { get; set; } = new Dictionary<string, string>(); |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Whether to automatically map types with the same name but different namespaces |
| | | 25 | | /// </summary> |
| | | 26 | | [JsonPropertyName("autoMapSameNameTypes")] |
| | 337 | 27 | | public bool AutoMapSameNameTypes { get; set; } = false; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Whether to ignore case when mapping types and namespaces |
| | | 31 | | /// </summary> |
| | | 32 | | [JsonPropertyName("ignoreCase")] |
| | 356 | 33 | | public bool IgnoreCase { get; set; } = false; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Creates a default mapping configuration |
| | | 37 | | /// </summary> |
| | | 38 | | /// <returns>A default mapping configuration</returns> |
| | | 39 | | public static MappingConfiguration CreateDefault() |
| | 65 | 40 | | { |
| | 65 | 41 | | return new MappingConfiguration |
| | 65 | 42 | | { |
| | 65 | 43 | | NamespaceMappings = new Dictionary<string, List<string>>(), |
| | 65 | 44 | | TypeMappings = new Dictionary<string, string>(), |
| | 65 | 45 | | AutoMapSameNameTypes = false, |
| | 65 | 46 | | IgnoreCase = false |
| | 65 | 47 | | }; |
| | 65 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Validates the mapping configuration |
| | | 52 | | /// </summary> |
| | | 53 | | /// <returns>True if valid, false otherwise</returns> |
| | | 54 | | public bool IsValid() |
| | 37 | 55 | | { |
| | | 56 | | // Check for empty keys or values in namespace mappings |
| | 71 | 57 | | if (NamespaceMappings.Any(kvp => string.IsNullOrWhiteSpace(kvp.Key) || |
| | 71 | 58 | | kvp.Value == null || |
| | 71 | 59 | | kvp.Value.Count == 0 || |
| | 71 | 60 | | kvp.Value.Any(string.IsNullOrWhiteSpace))) |
| | 4 | 61 | | { |
| | 4 | 62 | | return false; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | // Check for empty keys or values in type mappings |
| | 57 | 66 | | if (TypeMappings.Any(kvp => string.IsNullOrWhiteSpace(kvp.Key) || |
| | 57 | 67 | | string.IsNullOrWhiteSpace(kvp.Value))) |
| | 2 | 68 | | { |
| | 2 | 69 | | return false; |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | // Check for circular references in namespace mappings |
| | 31 | 73 | | if (HasCircularReferences()) |
| | 2 | 74 | | { |
| | 2 | 75 | | return false; |
| | | 76 | | } |
| | | 77 | | |
| | 29 | 78 | | return true; |
| | 37 | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Checks if there are circular references in namespace mappings |
| | | 83 | | /// </summary> |
| | | 84 | | /// <returns>True if circular references exist, false otherwise</returns> |
| | | 85 | | private bool HasCircularReferences() |
| | 31 | 86 | | { |
| | 31 | 87 | | var visited = new HashSet<string>(); |
| | 31 | 88 | | var recursionStack = new HashSet<string>(); |
| | | 89 | | |
| | 147 | 90 | | foreach (var source in NamespaceMappings.Keys) |
| | 28 | 91 | | { |
| | 28 | 92 | | if (DetectCircular(source, visited, recursionStack)) |
| | 2 | 93 | | { |
| | 2 | 94 | | return true; |
| | | 95 | | } |
| | 26 | 96 | | } |
| | | 97 | | |
| | 29 | 98 | | return false; |
| | 31 | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Helper method for circular reference detection using DFS |
| | | 103 | | /// </summary> |
| | | 104 | | private bool DetectCircular(string current, HashSet<string> visited, HashSet<string> recursionStack) |
| | 32 | 105 | | { |
| | 32 | 106 | | if (!visited.Contains(current)) |
| | 30 | 107 | | { |
| | 30 | 108 | | visited.Add(current); |
| | 30 | 109 | | recursionStack.Add(current); |
| | | 110 | | |
| | | 111 | | // If the current namespace maps to other namespaces |
| | 30 | 112 | | if (NamespaceMappings.TryGetValue(current, out var targets)) |
| | 30 | 113 | | { |
| | 148 | 114 | | foreach (var target in targets) |
| | 31 | 115 | | { |
| | | 116 | | // If target is in recursion stack, we have a cycle |
| | 31 | 117 | | if (recursionStack.Contains(target)) |
| | 2 | 118 | | { |
| | 2 | 119 | | return true; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | // If target is a source namespace and we detect a cycle in its mappings |
| | 29 | 123 | | if (NamespaceMappings.ContainsKey(target) && DetectCircular(target, visited, recursionStack)) |
| | 2 | 124 | | { |
| | 2 | 125 | | return true; |
| | | 126 | | } |
| | 27 | 127 | | } |
| | 26 | 128 | | } |
| | 26 | 129 | | } |
| | | 130 | | |
| | 28 | 131 | | recursionStack.Remove(current); |
| | 28 | 132 | | return false; |
| | 32 | 133 | | } |
| | | 134 | | } |