|
| 1 | +using System.Linq; |
| 2 | + |
| 3 | +namespace Typewriter |
| 4 | +{ |
| 5 | + using ApiDoctor.Validation; |
| 6 | + using ApiDoctor.Validation.Error; |
| 7 | + using System; |
| 8 | + using System.Collections.Generic; |
| 9 | + using System.Linq; |
| 10 | + using System.Text; |
| 11 | + |
| 12 | + internal static class CsdlExtensionMethods |
| 13 | + { |
| 14 | + |
| 15 | + public static string RequestUriPathOnly(this MethodDefinition method, string[] baseUrlsToRemove, IssueLogger issues) |
| 16 | + { |
| 17 | + var path = method.Request.FirstLineOnly().TextBetweenCharacters(' ', '?').TrimEnd('/'); |
| 18 | + |
| 19 | + if (baseUrlsToRemove != null) |
| 20 | + { |
| 21 | + foreach (var baseUrl in baseUrlsToRemove) |
| 22 | + { |
| 23 | + if (!string.IsNullOrEmpty(baseUrl) && path.StartsWith(baseUrl, StringComparison.OrdinalIgnoreCase)) |
| 24 | + { |
| 25 | + path = path.Substring(baseUrl.Length); |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + // just in case there's a stray example that doesn't match, at least chop the domain part off. |
| 31 | + foreach (var scheme in new[] { "https://", "http://" }) |
| 32 | + { |
| 33 | + if (path.StartsWith(scheme, StringComparison.OrdinalIgnoreCase)) |
| 34 | + { |
| 35 | + int pathStartIndex = path.IndexOf('/', scheme.Length); |
| 36 | + if (pathStartIndex != -1) |
| 37 | + { |
| 38 | + path = path.Substring(pathStartIndex); |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // Normalize variables in the request path |
| 45 | + path = path.ReplaceTextBetweenCharacters('{', '}', "var"); |
| 46 | + |
| 47 | + if (method.RequestMetadata.SampleKeys != null) |
| 48 | + { |
| 49 | + foreach (var key in method.RequestMetadata.SampleKeys) |
| 50 | + { |
| 51 | + path = path.Replace("/" + key, "/{var}"); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Normalize function params |
| 56 | + var substitutions = new Dictionary<string, string>(); |
| 57 | + var parenIndex = path.IndexOf('('); |
| 58 | + for (int i = 0; i < path.Length; i++) |
| 59 | + { |
| 60 | + if (path[i] == '(') |
| 61 | + { |
| 62 | + // this is the start of a function. let's find the closing paren. |
| 63 | + var close = path.IndexOf(')', i); |
| 64 | + if (close > -1) |
| 65 | + { |
| 66 | + var inner = path.Substring(i + 1, close - i - 1); |
| 67 | + substitutions[inner] = NormalizeFunctionParameters(inner, issues); |
| 68 | + i = close; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + foreach (var sub in substitutions) |
| 74 | + { |
| 75 | + path = path.Replace(sub.Key, sub.Value); |
| 76 | + } |
| 77 | + |
| 78 | + // Rewrite path syntax into what it logically means |
| 79 | + path = path.ReplaceTextBetweenCharacters(':', ':', "/children/{var}", requireSecondChar: false, removeTargetChars: true); |
| 80 | + |
| 81 | + return path; |
| 82 | + } |
| 83 | + |
| 84 | + private static string NormalizeFunctionParameters(string funcParams, IssueLogger issues) |
| 85 | + { |
| 86 | + // foo=bar, baz ='qux',x= 9 |
| 87 | + var normalized = new StringBuilder(); |
| 88 | + var allParams = funcParams.Split(','); |
| 89 | + for (int i = 0; i < allParams.Length; i++) |
| 90 | + { |
| 91 | + var param = allParams[i].Trim(); |
| 92 | + var kvp = param.Split('='); |
| 93 | + if (kvp.Length != 2) |
| 94 | + { |
| 95 | + issues.Error(ValidationErrorCode.ParameterParserError, $"Malformed function params {funcParams}"); |
| 96 | + return funcParams; |
| 97 | + } |
| 98 | + |
| 99 | + allParams[i] = kvp[0].Trim() + "={var}"; |
| 100 | + } |
| 101 | + |
| 102 | + return string.Join(",", allParams.OrderBy(p => p)); |
| 103 | + } |
| 104 | + |
| 105 | + public static string HttpMethodVerb(this MethodDefinition method) |
| 106 | + { |
| 107 | + HttpParser parser = new HttpParser(); |
| 108 | + var request = parser.ParseHttpRequest(method.Request); |
| 109 | + return request.Method; |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + internal static void AppendWithCondition(this System.Text.StringBuilder sb, bool condition, string text, string prefixIfExistingContent = null) |
| 114 | + { |
| 115 | + if (condition) |
| 116 | + { |
| 117 | + if (sb.Length > 0 && prefixIfExistingContent != null) |
| 118 | + sb.Append(prefixIfExistingContent); |
| 119 | + sb.Append(text); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Merge two EntityFramework instances together into the first framework |
| 125 | + /// </summary> |
| 126 | + /// <param name="framework1"></param> |
| 127 | + /// <param name="framework2"></param> |
| 128 | + internal static EntityFramework MergeWith(this EntityFramework framework1, EntityFramework framework2) |
| 129 | + { |
| 130 | + ObjectGraphMerger<EntityFramework> merger = new ObjectGraphMerger<EntityFramework>(framework1, framework2); |
| 131 | + var edmx = merger.Merge(); |
| 132 | + |
| 133 | + // Clean up bindingParameters on actions and methods to be consistently the same |
| 134 | + foreach (var schema in edmx.DataServices.Schemas) |
| 135 | + { |
| 136 | + foreach (var action in schema.Actions) |
| 137 | + { |
| 138 | + foreach (var param in action.Parameters.Where(x => x.Name == "bindingParameter" || x.Name == "this")) |
| 139 | + { |
| 140 | + param.Name = "bindingParameter"; |
| 141 | + param.IsNullable = null; |
| 142 | + } |
| 143 | + } |
| 144 | + foreach (var func in schema.Functions) |
| 145 | + { |
| 146 | + foreach (var param in func.Parameters.Where(x => x.Name == "bindingParameter" || x.Name == "this")) |
| 147 | + { |
| 148 | + param.Name = "bindingParameter"; |
| 149 | + param.IsNullable = null; |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return edmx; |
| 155 | + |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | + } |
| 163 | +} |
0 commit comments